Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带水平滚动视图的抽屉布局进入内容菜单_Android_Touch Event_Drawerlayout - Fatal编程技术网

Android 带水平滚动视图的抽屉布局进入内容菜单

Android 带水平滚动视图的抽屉布局进入内容菜单,android,touch-event,drawerlayout,Android,Touch Event,Drawerlayout,我的问题很简单。我可以在抽屉布局的内容菜单内使用水平滚动视图吗? 我的抽屉布局如下所示: <android.support.v4.widget.DrawerLayout android:id="@+id/pnlMenu" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- Main content view --> <

我的问题很简单。我可以在抽屉布局的内容菜单内使用水平滚动视图吗? 我的抽屉布局如下所示:

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

在片段内部,我有水平滚动视图,但当我试图触摸它时,什么也没有发生,因为抽屉布局跟随我的手指。 我认为,禁用内容菜单内的触摸事件,并使抽屉布局仅在单击主内容视图时才可关闭,这将解决我的问题。这是真的吗?如果没有,有人能告诉我我能做什么吗

谢谢。

基于此解决方案:

我已经能够使
水平滚动视图
可滚动。 创建一个类扩展抽屉布局:

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

        }
    }
并添加要拦截抽屉布局触摸事件的子id

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

最好设置
requestDisallowWinterCeptTouchEvent(true)
标志,而不是返回false。当我们有一个
HorizontalScrollView
时,在它下面有另一个视图(例如带有一些菜单项的
ListView
),我们从
HorizontalScrollView
区域到前面提到的
ListView
做出一个动态手势,应用程序将因NPE而崩溃。当您第一次打开应用程序并通过汉堡包图标进入抽屉布局时,就会发生这种情况。使用以下命令:

if (rect.contains((int) ev.getX(), (int) ev.getY())) {     
    this.requestDisallowInterceptTouchEvent(true);
}

如果您想在
DrawerLayout
中使用
HorizontalScrollView
的话,目前所有答案的替代策略是打开抽屉时锁定抽屉。这意味着用户无法用刷卡关闭抽屉,但
水平滚动视图
内的滚动将按预期工作

通过调用

mDrawerLayout.setDrawerLockMode(抽屉布局.锁定模式\锁定\打开)

此调用的方便位置是
onDrawerOpened

不幸的是,当用户轻触scrim(屏幕的暗显部分未被抽屉占用)时,锁定也会阻止抽屉关闭。你需要接住水龙头,自己关上,就像这样:

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

我还没有测试过,但我想这应该是一个有效的答案。这对我来说很有用,但是如果你的
滚动视图
像我的一样在
包含
中,一定要以包含为目标。否则它不会触发命中。我使用这个,但我得到了classCastExceptionSimple解决方案。此外,在关闭抽屉之前,您需要通过
mDrawerLayout.setDrawerLockMode(DrawerLayout.lock\u mode\u undefined)
将抽屉锁定模式设置为未定义。我发现如果没有这个,抽屉一关就不能打开