嵌套的Android ViewPager,刷卡ListItems的一个ListView内水平 [英] Nesting Android ViewPager, Swiping ListItems inside a ListView horizontally
问题描述
我有一个活动的根水平ViewPager。
I have a ViewPager on the root-level of an activity.
寻呼机的每个页面都包含一个ListFragment(由FragmentPagerAdapter支持)。
Each page of the pager contains a ListFragment (backed by a FragmentPagerAdapter).
一些列表视图中的项目应包含额外ViewPagers支持刷卡这些项目的内容(列表项内,例如水平画廊)。
Some of the list view items should contain additionally ViewPagers to support swiping the content of those items (e. g. a horizontal gallery inside a list item).
我怎么能窝观点寻呼机? ViewPager - > ListView控件(在页面) - > ViewPager(列表项中)
How can I nest view pagers? ViewPager -> ListView (in a page) -> ViewPager (inside a list item)
我可以在ListFragments之间水平刷卡,我可以轻扫整个列表垂直,但我不能刷卡内部列表项。
I can swipe between the ListFragments horizontally and I can swipe the whole list vertically, but I cannot swipe inside list items.
推荐答案
我添加了一个 OnTouchListener
的内部 ViewPager
:
private OnTouchListener mSuppressInterceptListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(
event.getAction() == MotionEvent.ACTION_DOWN &&
v instanceof ViewGroup
) {
((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
}
return false;
}
};
这只是检测ACTION_DOWN触摸事件在内 ViewPager
和prevents截获它的外层。因为它返回false,只有ACTION_DOWN事件应该受到打击;所有其他事件将被忽略。您可以添加这个监听到要保护从外 ViewPager
的滚动的每一个元素,但很明显,如果你想拿起这些元素任何其他的触摸行为你需要对付他们触摸监听器里,并有可能实现一个更好的倾听者。
This just detects ACTION_DOWN touch events on the inner ViewPager
and prevents the outer one from intercepting it. Because it returns false, only the ACTION_DOWN event should be hit; all the other events will be ignored. You can add this listener to every element you want to "protect" from the outer ViewPager
's scrolling, though obviously if you want to pick up any other touch behaviour on those elements you'll need to deal with them inside the touch listener and possibly implement a better listener.
感谢@Rodja谁给我的想法摆在首位。
Credit to @Rodja who gave me the idea in the first place.
这篇关于嵌套的Android ViewPager,刷卡ListItems的一个ListView内水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!