我认为您可以自定义并ViewPager
控制.ListView
canScroll
ViewPager
我尝试了一个示例,它似乎工作正常。您可以使用此自定义ViewPager
。
public class CustomViewPager extends ViewPager {
public CustomViewPager(Context context) {
super(context);
}
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 instanceof ViewPager) {
if (getChildAt(getCurrentItem()) != null) {
//get the ListView of current Fragment
EnhancedListView enhancedListView = (EnhancedListView) getChildAt(getCurrentItem()).findViewById(R.id.list);
//If the user is in first page and tries to swipe left, enable the ListView swipe
if (getCurrentItem() == 0 && dx > 0) {
enhancedListView.enableSwipeToDismiss();
}
//If the user is in second page and tries to swipe right, enable the ListView swipe
else if (getCurrentItem() == 1 && dx < 0) {
enhancedListView.enableSwipeToDismiss();
}
//Block the ListView swipe there by enabling the parent ViewPager swiping
else {
enhancedListView.disableSwipeToDismiss();
}
}
}
return super.canScroll(v, checkV, dx, x, y);
}
}
EnhancedListView
此外,您还必须在库中进行一些更改。因为canScroll
方法是在ACTION_DOWN
事件之后调用的,如果我们启用刷入canScroll
方法 - 它会跳过为ACTION_DOWN
事件定义的逻辑,并且可能会导致意外行为。因此,仅当触摸事件为 时才阻止滑动ACTION_MOVE
。这些是图书馆的onTouchEvent
变化EnhancedListView
。
//EnhancedListView class
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!mSwipeEnabled && (ev.getAction() == MotionEvent.ACTION_MOVE)) {
return super.onTouchEvent(ev);
}
.....
我不确定这是否是问题的完美解决方案,但它工作得很好。
如果问题或答案不清楚,这里是示例应用程序的一些屏幕截图。
所需解决方案:
如果用户在第一页并向右滑动,则应滑动列表项。
如果用户在第二页并向左滑动,则应滑动列表项。
在其他情况下,ViewPager
应该刷卡。
更新:要修复错误,这里是自定义代码SwipeRefreshLayout
的细微变化。ViewPager
public class ScrollLock extends ViewPager {
public ScrollLock(Context context) {
super(context);
}
public ScrollLock(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ViewPager) {
if (getChildAt(getCurrentItem()) != null) {
//set it so it does not swipe to refresh while swiping away a list item
SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
//get the ListView of current Fragment
EnhancedListView enhancedListView = (EnhancedListView) getChildAt(getCurrentItem()).findViewById(R.id.listView1);
//If the user is in first page and tries to swipe left, enable the ListView swipe
if (getCurrentItem() == 0 && dx > 0) {
enhancedListView.enableSwipeToDismiss();
swipeLayout.setEnabled(false);
return true;
}
//If the user is in second page and tries to swipe right, enable the ListView swipe
else if (getCurrentItem() == 1 && dx < 0) {
enhancedListView.enableSwipeToDismiss();
swipeLayout.setEnabled(false);
return true;
}
//Block the ListView swipe there by enabling the parent ViewPager swiping
else {
enhancedListView.disableSwipeToDismiss();
}
}
}
return super.canScroll(v, checkV, dx, x, y);
}
}