问题描述
我有一个嵌套的ViewPager,可以很好地工作.唯一的问题是,一旦子级ViewPager位于最后一项并且我进一步滚动,父级ViewPager就会滚动.我不要这种行为.
I have a nested ViewPager that works brilliantly. The only issue is, once the child ViewPager is at the last item and I scroll further, the parent ViewPager scrolls. I do not want this behaviour.
我该如何实现?
这里是一个示例,在我的Activity_main.xml
中,我有一个父ViewPager,其中包含三个片段页面.
Here is an example, in my Activity_main.xml
I have the parent ViewPager, which houses three fragment pages.
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
在片段布局之一fragment_categories.xml
中,我有另一个ViewPager,它是Activity_main.xml
中父viewPager的子代,
Inside one of the fragment layouts, fragment_categories.xml
, I have another ViewPager, which is a child to the parent viewPager in the Activity_main.xml
,
<android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_width="match_parent"
android:layout_height="@dimen/offer_height"
android:layout_marginTop="2dp"
android:overScrollMode="never"/>
一切正常.但是,当子级viewPager到达其最后一项,并且我滚动得更多时,父级viewPager将移至下一页.我不想这样.我尝试通过拦截父ViewPager上的触摸来做到这一点,但这没有用.我猜我做错了吗? (是的,我将xml中的标签更改为com.project.CustomViewPager
并尝试了此操作.)
It all works fine. But when the child viewPager reaches its last item, and I scroll more, the parent viewPager moves to the next page. I do not want this. I tried doing this by intercepting touch on the parent ViewPager before, and that didn't work. I'm guessing I'm doing it wrong? (Yes, I changed tags in the xml to com.project.CustomViewPager
and tried this.)
CustomViewPager.java
:
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if(v != this && v instanceof ViewPager) {
return false;
}
return super.canScroll(v, checkV, dx, x, y);
}
推荐答案
首先创建一个CustomViewPager来更改其可滑动状态,如下所示:
First create a CustomViewPager to change it's swipeable state like below:
public class CustomViewPager extends ViewPager {
private boolean canScroll = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setCanScroll(boolean canScroll) {
this.canScroll = canScroll;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return canScroll && super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return canScroll && super.onInterceptTouchEvent(ev);
}
public boolean isCanScroll() {
return canScroll;
}
}
第二次更改要禁用的视图寻呼机,将其滑动到CustomViewPager
Second change your view pager which you want to disable it's swipe propery to your CustomViewPager
获取您的查看传呼机:
CustomViewPager myCustomViewPager = (CustomViewPager) findViewById(R.id.my_custom_pager);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
在您的ViewPager中添加onTouchListener
:
Add an onTouchListener
to your ViewPager:
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
myCustomViewPager.setCanScroll(false);
break;
case MotionEvent.ACTION_UP:
myCustomViewPager.setCanScroll(true);
break;
}
return false;
}
});
如果您真的需要检查您的Viewpager当前位置:
If you really need to check if your viewpager's current position:
int pos = 0;
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
pos = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
然后您可以查看MotionEvents if pos == yourAdaptersItemCount-1
并禁用或启用customViewPager.
And you can chek in your MotionEvents if pos == yourAdaptersItemCount-1
and disable or enable your customViewPager.
祝你好运.
这篇关于当子ViewPager位于最后一项时,如何防止父ViewPager滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!