NavigationDrawerFragment中的Horizo​​ntalScrollView

时间:2015-01-26 15:16:18

标签: android user-interface touch-event

如何划分应该滚动ScrollView的事件和应该管理NavigationDrawer的事件? 现在NavigationDrawer捕获所有TouchEvent。

fragment_navigation_drawer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_grey"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:id="@+id/hScrollView"
        android:layout_height="200dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    </HorizontalScrollView>



    <View
        android:id="@+id/id10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:gravity="bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

activity_main.xml中

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/toolbar" />

            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>


        </LinearLayout>

        <fragment
            android:id="@+id/navigation_drawer"
            android:name="com.company.example.NavigationDrawerFragment"
            android:layout_width="288dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.v4.widget.DrawerLayout>

Horizo​​ntalScrollView可能不是在NavigationDrawer中使用的最佳解决方案,但是......

2 个答案:

答案 0 :(得分:1)

如果你准备放弃&#34;滑动关闭&#34;抽屉的功能,您可以在打开时锁定抽屉。这意味着用户无法通过滑动关闭抽屉,但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;
    }
}); 

答案 1 :(得分:0)

navigationDrawer.requestDisallowInterceptTouchEvent(false);

打开导航抽屉后放置上面的代码,并在关闭导航抽屉后放置以下代码

drawerLayout.requestDisallowInterceptTouchEvent(true);
相关问题