Android:如何禁用CoordinatorLayout

时间:2017-01-31 07:19:21

标签: android android-recyclerview android-coordinatorlayout android-appbarlayout

我有这个简单的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ActionBarThemeOverlay">

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

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/theme_primary"
                android:padding="@dimen/keyline_1">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bg_search_view"
                    android:focusable="true"
                    android:focusableInTouchMode="true">

                    <com.xxxx.ui.widget.CustomSearchView
                        android:id="@+id/search_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:withBackButton="true" />

                </FrameLayout>

            </FrameLayout>

        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

            <LinearLayout
                android:id="@+id/methods_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="@dimen/keyline_5">

                <com.xxx.ui.widget.ButtonSearchBy
                    android:id="@+id/search_by_categories"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:title="@string/search_by_categories"
                    app:icon="@drawable/ic_by_categories"
                    app:iconFillColor="@color/grey"
                    android:visibility="gone"/>

                <com.xxx.ui.widget.ButtonSearchBy
                    android:id="@+id/search_by_recents"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:title="@string/search_by_recents"
                    app:icon="@drawable/ic_by_recents"
                    app:iconFillColor="@color/grey"
                    android:visibility="gone"/>                  

            </LinearLayout>

        </RelativeLayout>

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

在我的活动中,我添加了此代码以禁用用户直接滚动 CoordinatorLayout:

if (mAppBarLayout.getLayoutParams() != null) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
            AppBarLayout.Behavior appBarLayoutBehaviour = new AppBarLayout.Behavior();
            appBarLayoutBehaviour.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
                @Override
                public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
                    return false;
                }
            });
            layoutParams.setBehavior(appBarLayoutBehaviour);
        }

此代码在“fragment_container”(FrameLayout)中正常工作,,我添加了RecyclerView,如果用户将滚动到此列表,协调器也会滚动...

我想要自动折叠或展开我的CoordinatorLayout,而不是与RecyclerView滚动链接。

我也试过了,但它无法正常工作:

@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (mFragmentContainer.getVisibility() == View.VISIBLE &&
                event.getAction() == MotionEvent.ACTION_MOVE) {

            Rect fragContainerRect = new Rect();
            mFragmentContainer.getHitRect(fragContainerRect);

            if (fragContainerRect.contains((int) event.getX(), (int) event.getY())) {
                LogUtils.LOGD("XXXX", "move");
                mFragmentContainer.dispatchTouchEvent(event);
                return true;
            }
        }

        return super.dispatchTouchEvent(event);
    }

那么当列表滚动时,如何禁用CoordinatorLayout滚动?

非常感谢你们!

0 个答案:

没有答案