Android 如何禁用顶部透明层的触摸?
我需要定制一个日历具有折叠月视图到周视图的功能。我正在为此使用Android 如何禁用顶部透明层的触摸?,android,android-layout,android-collapsingtoolbarlayout,calendarview,Android,Android Layout,Android Collapsingtoolbarlayout,Calendarview,我需要定制一个日历具有折叠月视图到周视图的功能。我正在为此使用MaterialCalendarView库。为此,我采用了一个具有MaterialCalendarView的框架作为底层,而顶部的collavingToolbarLayout作为透明视图。现在的问题是,我无法使用我的calendarView,因为它是底层,并且禁用了触摸折叠工具栏布局 我尝试过设置CoodinatorLayout的android:clickable=“false”及其所有子元素,还尝试过使用:collavingTool
MaterialCalendarView
库。为此,我采用了一个具有MaterialCalendarView
的框架作为底层,而顶部的collavingToolbarLayout
作为透明视图。现在的问题是,我无法使用我的calendarView,因为它是底层,并且禁用了触摸折叠工具栏布局
我尝试过设置CoodinatorLayout
的android:clickable=“false”
及其所有子元素,还尝试过使用:collavingToolbarLayout.setEnabled(false)
并将touch listener设置为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>
在禁用所有视图的触摸之后,仍然会调用onClick,这就是为什么我也禁用了click
通过这样做,我能够处理MaterialCalendarView
处于折叠模式时的触摸,但当它展开时,我无法使用它,其他一些视图正在点击
请帮我解决这个问题。我想访问MaterialCalendarView
触摸并禁用触摸协调器布局中的视图
,但嵌套滚动视图
除外
提前感谢。
onInterceptTouchEvent
在将父布局触摸事件发送到子视图之前响应它们
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Decide if to intercept or not
return true;
}
谢谢大家的努力 我通过覆盖dispatchTouchEvent解决了这个问题。感谢@Abyss发布此帖子-解决方案2适合我: 在每个视图的触摸屏上都可以触摸日历
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;
});
方法
onInterceptTouchEvent()
仅在视图组上可用(因为它们可以是要求拦截触摸事件的父/容器),这些视图组可以监视事件并通过返回true来劫持事件。如果它返回false,那么发送将照常进行。请问,为什么它被否决?
@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;
});