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

时间:2014-03-07 09:32:37

标签: android touch-event drawerlayout

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

<android.support.v4.widget.DrawerLayout
    android:id="@+id/pnlMenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Main content view -->

    <ListView
        android:id="@+id/lst"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:fastScrollEnabled="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        tools:listitem="@layout/item_layout" >
    </ListView>

    <!-- Content of menu -->

    <FrameLayout
        android:id="@+id/drawerFrame"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:clickable="true"
        android:background="@color/black" >

        <fragment
            android:id="@+id/frag"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.test.TestFragment" />
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>

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

谢谢。

3 个答案:

答案 0 :(得分:4)

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

我已经能够HorizontalScrollView滚动了。 创建一个类extends 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);

答案 1 :(得分:0)

最好设置requestDisallowInterceptTouchEvent(true)标志而不是返回false。当我们有一个HorizontalScrollView及其下方时,会有另一个视图(例如。ListView带有一些菜单项),我们会从HorizontalScrollView区域到前面提到的{{{{{ 1}}应用程序将崩溃与NPE。这种情况发生在您第一次打开应用程序并通过汉堡包图标转到ListView时。使用此:

DrawerLayout

答案 2 :(得分:0)

如果你想在你的HorizontalScrollView DrawerLayout内有一个替代策略,所有答案到目前为止都是在打开时锁定抽屉。这意味着用户无法通过滑动关闭抽屉,但HorizontalScrollView内的滚动将按预期工作。

通过调用

实现锁定

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);

此次通话的便利位置是onDrawerOpened

不幸的是,当用户敲击稀松布时(屏幕的暗淡部分未被抽屉占据),锁定还可以防止抽屉关闭。你需要抓住那个水龙头并自己关闭它,如下所示:

mDrawerLayout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getX();
        int drawerWidth = (int)getResources().getDimension(R.dimen.your_drawer_width);
        if (x > drawerWidth) {
            // inside scrim
            mDrawerLayout.closeDrawer();
            return true;
        }
        return false;
    }
});