如何禁用顶部透明层的触摸?

问题描述 投票:-1回答:2

我需要自定义具有折叠月视图到周视图功能的日历。我正在使用MaterialCalendarView图书馆。为此我采取了一个框架,其中MaterialCalendarView作为底层,CollapsingToolbarLayout在顶部作为透明视图。现在的问题是,我无法使用我的calendarView,因为它是一个底层并禁用CollapsingToolbarLayout的触摸。

我已经尝试设置android:clickable="false"及其所有子元素的CoodinatorLayout,并尝试使用:collapsingToolbarLayout.setEnabled(false);并将触摸侦听器设置为false,但没有运气。

这是我的XML文件:

-----some code-----
---
--
-
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true">

        <com.prolificinteractive.materialcalendarview.MaterialCalendarView
            android:id="@+id/calendarView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            app:mcv_allowClickDaysOutsideCurrentMonth="false"
            app:mcv_firstDayOfWeek="monday"
            app:mcv_tileHeight="36dp" />

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinator_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:focusedByDefault="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:focusedByDefault="false">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:focusedByDefault="false"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/dummy_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="72dp"
                        android:background="@android:color/transparent"
                        android:clickable="false"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:focusedByDefault="false"
                        android:visibility="invisible"
                        app:contentInsetStart="0dp"
                        app:layout_collapseMode="pin">

                        <View
                            android:id="@+id/dummy_toolbar_view"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@android:color/transparent"
                            android:clickable="false"
                            android:focusable="false"
                            android:focusableInTouchMode="false"
                            android:focusedByDefault="false" />

                    </android.support.v7.widget.Toolbar>

                    <View
                        android:id="@+id/transparent_view"
                        android:layout_width="match_parent"
                        android:layout_height="252dp"
                        android:background="@android:color/transparent"
                        android:clickable="false"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:focusedByDefault="false" />

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

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

            <android.support.v4.widget.NestedScrollView
                android:id="@+id/nested_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/bg_dark_gray"
                android:fillViewport="true"
                android:scrollbars="none"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recycler_occasions"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false" />

                    <TextView
                        android:id="@+id/txt_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginStart="@dimen/dp_50"
                        android:layout_marginTop="@dimen/dp_30"
                        android:layout_marginEnd="@dimen/dp_50"
                        android:layout_marginBottom="@dimen/dp_100"
                        android:background="@drawable/btn_gradient_bg"
                        android:fontFamily="@font/poppins_semibold"
                        android:gravity="center"
                        android:padding="@dimen/dp_16"
                        android:text="@string/next"
                        android:textAllCaps="true"
                        android:textColor="@color/white"
                        android:textSize="@dimen/btn_text_size" />
                </LinearLayout>

            </android.support.v4.widget.NestedScrollView>
        </android.support.design.widget.CoordinatorLayout>
    </RelativeLayout>

这就是我在java文件中所做的:

collapsingToolbarLayout.setEnabled(false);
dummyToolbarView.setEnabled(false);
appBarLayout.setEnabled(false);
coordinatorLayout.setEnabled(false);

collapsingToolbarLayout.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});
appBarLayout.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});
coordinatorLayout.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});
transparentView.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});
dummyToolbarView.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});


collapsingToolbarLayout.setClickable(false);
coordinatorLayout.setClickable(false);
appBarLayout.setClickable(false);
transparentView.setClickable(false);

禁用所有视图的触摸后,仍然onClick被调用,这就是为什么我也禁用了点击。

通过这样做,我能够在折叠模式下处理MaterialCalendarView的触摸,但是当它被展开然后我无法使用它时,其他一些视图正在点击。

这个你能帮我吗。我想访问MaterialCalendarView触摸并禁用CoordinatorLayout内的视图触摸,除了NestedScrollView

提前致谢。

android android-layout android-collapsingtoolbarlayout calendarview
2个回答
1
投票

onInterceptTouchEvent在将父布局触摸事件分派给子视图之前响应它们。

@Override
 public boolean onInterceptTouchEvent(MotionEvent ev) {
 // Decide if to intercept or not
 return true;
 }

Touch flow control in Android


0
投票

谢谢大家的努力。

我通过重写dispatchTouchEvent解决了这个问题。感谢@Abyss这篇文章 - 解决方案2适合我:https://stackoverflow.com/a/21971924/6384924

@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

在每个视图的触摸上发送日历触摸

collapsingToolbarLayout.setOnTouchListener((v, event) -> {
            calendarView.dispatchTouchEvent(event);
            return false;
        });
        dummyToolbarView.setOnTouchListener((v, event) -> {
            calendarView.dispatchTouchEvent(event);
            return false;
        });
        appBarLayout.setOnTouchListener((v, event) -> {
            calendarView.dispatchTouchEvent(event);
            return false;
        });
        coordinatorLayout.setOnTouchListener((v, event) -> {
            calendarView.dispatchTouchEvent(event);
            return false;
        });
        transparentView.setOnTouchListener((v, event) -> {
            calendarView.dispatchTouchEvent(event);
            return false;
        });
        dummyToolbar.setOnTouchListener((v, event) -> {
            calendarView.dispatchTouchEvent(event);
            return false;
        });
© www.soinside.com 2019 - 2024. All rights reserved.