微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 使用Horizo​​ntalScrollView将DrawerLayout导入内容菜单

我的问题很简单.我可以在DrawerLayout的内容菜单中使用Horizo​​ntalScrollView吗?
我的DrawerLayout看起来像这样:

在片段内部我有Horizo​​ntalScrollView但是当我尝试触摸它时没有任何事情发生,因为抽屉布局跟随我的手指.
我认为禁用内容菜单中的触摸事件并仅在单击主内容视图时使DrawerLayout可关闭将解决我的问题.真的吗?如果没有,有人可以告诉我,我该怎么办?

谢谢.

最佳答案
基于此解决方案:https://stackoverflow.com/a/7258579/452486

我已经能够使Horizo​​ntalScrollView可滚动.
创建一个类扩展DrawerLayout:

public class AllowChildInterceptTouchEventDrawerLayout extends DrawerLayout {

    private int mInterceptTouchEventChildId;

    public void setInterceptTouchEventChildId(int id) {
       this.mInterceptTouchEventChildId = id;
    }

    public AllowChildInterceptTouchEventDrawerLayout(Context context) {
    super(context);
    }

    public AllowChildInterceptTouchEventDrawerLayout(Context context,AttributeSet attrs) {
        super(context,attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (mInterceptTouchEventChildId > 0) {
            View scroll = findViewById(mInterceptTouchEventChildId);
            if (scroll != null) {
                Rect rect = new Rect();
                scroll.getHitRect(rect);
                if (rect.contains((int) ev.getX(),(int) ev.getY())) {
                    return false;
                }
            }
        }
        return super.onInterceptTouchEvent(ev);

        }
    }

添加拦截drawerlayout的触摸事件的子ID

AllowChildInterceptTouchEventDrawerLayout drawerLayout = (AllowChildInterceptTouchEventDrawerLayout) findViewById(R.id.layoutdrawer_id);
drawerLayout.setInterceptTouchEventChildId(R.id.horizontalscrollview_id);

原文地址:https://www.jb51.cc/android/431040.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐