[英]ViewPager + Seekbar : when trying to slide seekbar, viewPager changes fragment instead
i have a viewPager with three fragments. 我有一个带有三个片段的viewPager。 the middle (second) fragment has a seekBar implemented in it. 中间(第二个)片段中实现了seekBar。 When i am trying to slide the seek bar, the viewPager changes the fragment instead. 当我尝试滑动搜索栏时,viewPager会更改片段。
There was one other post where the user had a similar problem and offered a solution, but that solution completely disengages the swipe function from the viewPager. 在另一篇文章中,用户遇到了类似的问题并提供了一种解决方案,但是该解决方案完全使滑动功能脱离了ViewPager。
How do i keep both swipe functions, but focus the seekbar, when the seekbar view is being used. 当使用搜索栏视图时,如何保持两个滑动功能,但将搜索栏聚焦。
I finally figured the answer. 我终于找到答案了。 If someone is still stuck with the question. 如果有人仍然坚持这个问题。 Create a custom class of SwipeRefreshlayout and add to layout. 创建SwipeRefreshlayout的自定义类,然后添加到布局。
public class CustomSwipeRefresh extends SwipeRefreshLayout {
private int mTouchSlop;
private float mPrevX;
private boolean isDisabled;
public CustomSwipeRefresh(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public void disableInterceptTouchEvent(boolean isDisabled) {
this.isDisabled = isDisabled;
getParent().requestDisallowInterceptTouchEvent(isDisabled);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPrevX = MotionEvent.obtain(event).getX();
break;
case MotionEvent.ACTION_MOVE:
if (isDisabled) {
return false;
}
float eventX = event.getX();
float xDiff = Math.abs(eventX - mPrevX);
if (xDiff > mTouchSlop) {
return false;
}
}
return super.onInterceptTouchEvent(event);
}
}
Then on your fragment class with seekbar, just add the code 然后在带有seekbar的片段类上,只需添加代码
mswiperefresh.disableInterceptTouchEvent(boolean);
to enable and disable swipe focus. 启用和禁用划动焦点。
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
mswiperefresh.disableInterceptTouchEvent(true);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mswiperefresh.disableInterceptTouchEvent(false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.