我有一個包含ListFragment
和TouchListener
(SwipeDismissListViewTouchListener)的ViewPager。我修改了ViewPager
這樣的:ListView和ViewPager中的SwipeDismissListViewTouchListener
public class CustomViewPager extends ViewPager {
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
private boolean pagingEnabled = true;
public void setPagingEnabled(boolean enabled) {
pagingEnabled = enabled;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (!pagingEnabled) {
return false; // do not intercept
}
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!pagingEnabled) {
return false; // do not consume
}
return super.onTouchEvent(event);
}
}
我禁用ViewPager
從onOptionsItemSelected
在ListFragment
方法輕掃。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_edit) {
CustomViewPager vp = (CustomViewPager) getActivity().findViewById(R.id.pager);
vp.setPagingEnabled(false);
getListView().requestDisallowInterceptTouchEvent(true);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
getListView(),
new SwipeDismissListViewTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
removeItem(position);
}
}
}
);
getListView().setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during ListView scrolling,
// we don't look for swipes.
getListView().setOnScrollListener(touchListener.makeScrollListener());
}
的ViewPager
的刷卡被禁止,但我不能運行SwipeDismissListViewTouchListener
問題。 我已經測試了這些問題的答案,但不工作我
Nesting Android ViewPager, Swiping ListItems inside a ListView horizontally
ViewPager intercepts all x-axis onTouch events. How to disable?
Capture swipe to dismiss listview gestures in ViewPager
任何解決方案?謝謝
什麼是CustomViewPager的原因?僅適用於swipe2dismiss? – EE66
是的,CustomViewPager是禁用分頁並嘗試運行Swipe2Dismiss – pablogupi