嵌套Android ViewPager,水平滑动ListView内的ListItems [英] Nesting Android ViewPager, Swiping ListItems inside a ListView horizontally

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

问题描述

我在活动的根级别有一个 ViewPager.

I have a ViewPager on the root-level of an activity.

pager 的每个页面都包含一个 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.

推荐答案

我在内部添加了一个OnTouchListenerViewPager:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆