嵌套 Android ViewPager,在 ListView 内水平滑动 ListItems

Nesting Android ViewPager, Swiping ListItems inside a ListView horizontally(嵌套 Android ViewPager,在 ListView 内水平滑动 ListItems)

本文介绍了嵌套 Android ViewPager,在 ListView 内水平滑动 ListItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在活动的根级别上有一个 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).

某些列表视图项目应包含额外的 ViewPager 以支持滑动这些项目的内容(例如,列表项目内的水平画廊).

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.

推荐答案

我在内部ViewPager添加了一个OnTouchListener:

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;
    }
};

这只是检测内部 ViewPager 上的 ACTION_DOWN 触摸事件,并防止外部拦截它.因为它返回 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,在 ListView 内水平滑动 ListItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:嵌套 Android ViewPager,在 ListView 内水平滑动 ListItems

基础教程推荐