Android 防止抽屉布局处理内容区域中的触摸
通常,当按下抽屉布局的内容区域时,抽屉将关闭,触摸被消耗。有没有办法防止这种情况并将触摸事件传递到内容区域Android 防止抽屉布局处理内容区域中的触摸,android,navigation-drawer,drawerlayout,Android,Navigation Drawer,Drawerlayout,通常,当按下抽屉布局的内容区域时,抽屉将关闭,触摸被消耗。有没有办法防止这种情况并将触摸事件传递到内容区域 谢谢 我最终修改了抽屉布局 在方法onInterceptTouchEvent(MotionEvent ev)中,必须防止interceptForTap被设置为true。一种方法是删除以下条件 if (mScrimOpacity > 0 && isContentView(mLeftDragger.findTopChildUnder((int) x, (int)
谢谢 我最终修改了抽屉布局 在方法
onInterceptTouchEvent(MotionEvent ev)
中,必须防止interceptForTap
被设置为true。一种方法是删除以下条件
if (mScrimOpacity > 0 &&
isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) {
interceptForTap = true;
}
这将允许接触“通过”
要使抽屉不关闭,您可以将抽屉锁定模式设置为
lock\u mode\u LOCKED\u OPEN
我最后修改了抽屉布局
在方法onInterceptTouchEvent(MotionEvent ev)
中,必须防止interceptForTap
被设置为true。一种方法是删除以下条件
if (mScrimOpacity > 0 &&
isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) {
interceptForTap = true;
}
这将允许接触“通过”
要使抽屉不关闭,您可以将抽屉锁定模式设置为
lock\u mode\u LOCKED\u OPEN
受以下答案的启发,我修改了他的建议,并建议对抽屉布局进行以下扩展。同样,此解决方案是通过重写onInterceptTouchEvent()实现的
重写方法的逻辑相当简单:
- 每当抽屉视图(抽屉布局的可滑动部分)发生触摸事件时,我们返回false。这意味着DroperLayout既不拦截也不关心该手势以及该手势的任何进一步触摸事件,并且任何此类事件都由DroperLayout的子视图处理
- 另一方面,当抽屉视图中发生事件时,我们调用super.onInterceptTouchEvent()并让此方法决定是否拦截事件和/或如何处理事件。这使我们可以像原始抽屉布局一样,将抽屉滑入和滑出,只要滑动/触摸手势发生在抽屉视图上
public class CustomDrawerLayout extends DrawerLayout
{
@Override
public boolean onInterceptTouchEvent( MotionEvent event )
{
final View drawerView = getChildAt( 1 );
final ViewConfiguration config = ViewConfiguration.get( getContext() );
// Calculate the area on the right border of the screen on which
// the DrawerLayout should always intercept touch events.
// In case the drawer is closed, we still want the DrawerLayout
// to respond to touch/drag gestures there and reopen the drawer!
final int rightBoundary = getWidth() - 2 * config.getScaledTouchSlop();
// If the drawer is opened and the event happened
// on its surface, or if the event happened on the
// right border of the layout, then we let DrawerLayout
// decide if it wants to intercept (and properly handle)
// the event.
// Otherwise we don't let DrawerLayout to intercept,
// letting its child views handle the event.
return ( isDrawerOpen( drawerView ) && drawerView.getLeft() <= event.getX()
|| rightBoundary <= event.getX() )
&& super.onInterceptTouchEvent( event );
}
...
}
public类CustomDrawerLayout扩展了DrawerLayout
{
@凌驾
公共布尔值onInterceptTouchEvent(MotionEvent)
{
最终视图抽屉视图=getChildAt(1);
final ViewConfiguration=ViewConfiguration.get(getContext());
//计算屏幕右边框上显示的区域
//抽屉布局应始终拦截触摸事件。
//如果抽屉是关着的,我们仍然需要抽屉的布局
//响应触摸/拖动手势并重新打开抽屉!
final int rightbundary=getWidth()-2*config.getScaledTouchSlop();
//如果抽屉打开且事件发生
//表面上,或者如果事件发生在
//右边框的布局,然后我们让抽屉布局
//决定是否要拦截(并正确处理)
//这件事。
//否则我们就不会让抽屉布置来拦截,
//让其子视图处理事件。
return(isDrawerOpen(drawerView)和&drawerView.getLeft()受的启发,我修改了他的建议,并建议对DrawerLayout进行以下扩展。同样,此解决方案是通过覆盖onInterceptTouchEvent()
重写方法的逻辑相当简单:
- 每当抽屉视图(抽屉布局的可滑动部分)发生触摸事件时,我们返回false。这意味着抽屉布局既不截取也不关心该手势的该触摸事件和任何其他触摸事件,任何此类事件都由抽屉布局的子视图处理
- 另一方面,当抽屉视图中发生事件时,我们称之为super.onInterceptTouchEvent()并让此方法决定是否应拦截事件和/或如何处理事件。这样,只要抽屉视图上出现滑动/触摸手势,我们就可以像原始抽屉布局一样将抽屉滑入和滑出
下面的重写onInterceptTouchEvent()方法示例适用于抽屉视图位于屏幕右侧的抽屉布局(android:gravity=“right”)。但是,如何调整代码以适用于标准的左侧抽屉视图应该是显而易见的
public class CustomDrawerLayout extends DrawerLayout
{
@Override
public boolean onInterceptTouchEvent( MotionEvent event )
{
final View drawerView = getChildAt( 1 );
final ViewConfiguration config = ViewConfiguration.get( getContext() );
// Calculate the area on the right border of the screen on which
// the DrawerLayout should always intercept touch events.
// In case the drawer is closed, we still want the DrawerLayout
// to respond to touch/drag gestures there and reopen the drawer!
final int rightBoundary = getWidth() - 2 * config.getScaledTouchSlop();
// If the drawer is opened and the event happened
// on its surface, or if the event happened on the
// right border of the layout, then we let DrawerLayout
// decide if it wants to intercept (and properly handle)
// the event.
// Otherwise we don't let DrawerLayout to intercept,
// letting its child views handle the event.
return ( isDrawerOpen( drawerView ) && drawerView.getLeft() <= event.getX()
|| rightBoundary <= event.getX() )
&& super.onInterceptTouchEvent( event );
}
...
}
public类CustomDrawerLayout扩展了DrawerLayout
{
@凌驾
公共布尔值onInterceptTouchEvent(MotionEvent)
{
最终视图抽屉视图=getChildAt(1);
final ViewConfiguration=ViewConfiguration.get(getContext());
//计算屏幕右边框上显示的区域
//抽屉布局应始终拦截触摸事件。
//如果抽屉是关着的,我们仍然需要抽屉的布局
//响应触摸/拖动手势并重新打开抽屉!
final int rightbundary=getWidth()-2*config.getScaledTouchSlop();
//如果抽屉打开且事件发生
//表面上,或者如果事件发生在
//右边框的布局,然后我们让抽屉布局
//决定是否要拦截(并正确处理)
//这件事。
//否则我们就不会让抽屉布置来拦截,
//让其子视图处理事件。
return(isDrawerOpen(drawerView)和&drawerView.getLeft()您是如何修改DrawerLayout的?下载,修改它,并将其添加到您的项目中。您是如何修改DrawerLayout的?下载,修改它,并将其添加到您的项目中的。您能解释一下为什么这样可以解决问题吗?对不起,我键入了解释