Android ViewPager 上的左右箭头指示器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25157067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Left-right arrow indicators over a ViewPager
提问by Daggepagge
I want to show left and right arrows over my ViewPager, to indicate swiping.
我想在我的 ViewPager 上显示左右箭头,以指示滑动。
I added two ImageButtons over the ViewPager-element but those areas then block the ViewPager from triggering the "swiping".
我在 ViewPager 元素上添加了两个 ImageButton,但是这些区域会阻止 ViewPager 触发“滑动”。
I also want presses on those arrows to trigger the fragment to change accordingly.
我还希望按下这些箭头以触发片段相应地改变。
In short: The ImageButtons should not interfere with swiping but they should register pressing.
简而言之:ImageButtons 不应该干扰滑动,但它们应该注册按下。
How can I achieve this? Thanks!
我怎样才能做到这一点?谢谢!
采纳答案by Daggepagge
I figured out a solution. It's very simple.
我想出了一个解决方案。这很简单。
Instead of using ImageButtons for displaying the arrows, I now use ImageViews because they pass on any touch events to the layer underneath.
我现在不使用 ImageButtons 来显示箭头,而是使用 ImageViews,因为它们将任何触摸事件传递到下面的层。
Then, I put transparent Buttons on the fragments themselves instead, that way they won't block the ViewPagers swiping behaviour but they will fire onClick Events!
然后,我将透明按钮放在片段本身上,这样它们就不会阻止 ViewPagers 滑动行为,但它们会触发 onClick 事件!
回答by Christopher Kikoti
The code below worked for me perfectly well. NB: Use FrameLayout as it allows overlapping views
下面的代码非常适合我。 注意:使用 FrameLayout 因为它允许重叠视图
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="200dp" />
<ImageButton
android:id="@+id/left_nav"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_vertical|left"
android:src="@drawable/ic_chevron_left_black_24dp" />
<ImageButton
android:id="@+id/right_nav"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_vertical|right"
android:src="@drawable/ic_chevron_right_black_24dp" />
</FrameLayout>
The following part I used to handle ImageButton's click events
下面我用来处理ImageButton的点击事件的部分
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
leftNav = (ImageButton) view.findViewById(R.id.left_nav);
rightNav = (ImageButton) view.findViewById(R.id.right_nav);
// Images left navigation
leftNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tab = viewPager.getCurrentItem();
if (tab > 0) {
tab--;
viewPager.setCurrentItem(tab);
} else if (tab == 0) {
viewPager.setCurrentItem(tab);
}
}
});
// Images right navigatin
rightNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tab = viewPager.getCurrentItem();
tab++;
viewPager.setCurrentItem(tab);
}
});
Output
输出
回答by Keshav Bansal
No need to manage the current index for this or to handle any swiping manually. Simply call method viewPager.arrowScroll(int direction)
method on the click events of left and right arrows.
无需为此管理当前索引或手动处理任何滑动。只需viewPager.arrowScroll(int direction)
在左右箭头的点击事件上调用 method方法。
In a nutshell follow these 2 simple steps:
简而言之,请遵循以下 2 个简单步骤:
- Implement 2 ImageViews/ImageButtons/Buttons for left and right arrows.
On clicking them, call:
a) if left arrow is clicked -
viewPager.arrowScroll(View.FOCUS_LEFT);
b) if right arrow is clicked -
viewPager.arrowScroll(View.FOCUS_RIGHT);
- 为左右箭头实现 2 个 ImageViews/ImageButtons/Buttons。
单击它们时,调用:
a) 如果单击左箭头 -
viewPager.arrowScroll(View.FOCUS_LEFT);
b) 如果单击右箭头 -
viewPager.arrowScroll(View.FOCUS_RIGHT);
回答by Salil Dhawan
Implement left & right arrow buttons in your fragment. Then register their onClick in your activity and call viewpager's arrowScroll method to scroll the viewPager programmatically.
在您的片段中实现左右箭头按钮。然后在您的活动中注册他们的 onClick 并调用 viewpager 的 arrowScroll 方法以编程方式滚动 viewPager。
public void onRightClick(View view) {
viewPager.arrowScroll(ViewPager.FOCUS_RIGHT);
}
public void onLeftClick(View view) {
viewPager.arrowScroll(ViewPager.FOCUS_LEFT);
}
Create a method to toggle left/right arrow visibility in your fragment.
创建一个方法来切换片段中的左/右箭头可见性。
public void toggleArrowVisibility(boolean isAtZeroIndex, boolean isAtLastIndex) {
if(isAtZeroIndex)
leftBtn.setVisibility(View.INVISIBLE);
else
leftBtn.setVisibility(View.VISIBLE);
if(isAtLastIndex)
rightBtn.setVisibility(View.INVISIBLE);
else
rightBtn.setVisibility(View.VISIBLE);
}
Now implement ViewPager.OnPageChangeListener in your activity. Use SmartFragmentStatePagerAdapterto keep track of registered fragments in memory.
现在在您的活动中实现 ViewPager.OnPageChangeListener。使用SmartFragmentStatePagerAdapter跟踪内存中已注册的片段。
@Override
public void onPageSelected(int position) {
MyFragment fragment = (MyFragment) smartAdapter.getRegisteredFragment(position);
fragment.toggleArrowVisibility(position == 0, position == list.size() - 1);
}
回答by Android is everything for me
First use relative layout as your parent layout
首先使用相对布局作为父布局
second then add view pager inside it with match parent attribute on it
第二然后在其中添加视图寻呼机,并在其上匹配父属性
third take two image buttons over the view pager but in under the hierarchy of parent layout give them center vertical as a gravity and keep their side as right and left as per your requirement
第三个在视图寻呼机上取两个图像按钮,但在父布局的层次结构下,将它们垂直居中作为重力,并根据您的要求保持它们的左右两侧
fourth write functional code for buttons
第四次编写按钮的功能代码
fifth take static counter to get current view pager page
第五采取静态计数器获取当前视图寻呼机页面
on left and right button set minus and plus the view pager counter resp. and according to that show data in view pager
在左右按钮上设置减号和加号查看寻呼机计数器。并根据在视图寻呼机中显示数据
this is the simple logic for code you can search it on google you will easily get it
这是代码的简单逻辑你可以在谷歌上搜索它你会很容易得到它