打开导航抽屉时使片段可点击
Make fragment clickable when navigation drawer is opened
我的问题如下:我在平板电脑的横向模式下锁定了导航抽屉菜单setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN)
,但我需要右侧的片段处于活动状态,所以我总是可以用导航点击它打开。但我不知道该怎么做。请帮忙。
这是一个棘手的问题。当抽屉打开时,它会拦截触发抽屉关闭的触摸事件。为了防止这种情况,您需要将 DrawerLayout 子类化并覆盖 onInterceptTouchEvent 方法:
public class CustomDrawerLayout extends DrawerLayout
{
private View rightView;
private int mTouchSlop;
public CustomDrawerLayout (Context context)
{
this(context, null);
}
public CustomDrawerLayout (Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public CustomDrawerLayout (Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(context));
}
public void setRightView (View v)
{
this.rightView = v;
}
@Override
public boolean onInterceptTouchEvent (MotionEvent ev)
{
boolean result = super.onInterceptTouchEvent(ev);
if (rightView != null && isDrawerOpen(rightView))
{
DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) rightView.getLayoutParams();
if (layoutParams.gravity == Gravity.END)
{
// This is true when the position.x of the event is happening on the left of the drawer (with gravity END)
if (ev.getX() < rightView.getX() && ev.getX() > mTouchSlop)
{
result = false;
}
}
}
return result;
}
}
这是我使用右抽屉的代码。我相信你可以为你的左边抽屉改编这个。您可能还想禁用阴影:
mDrawerLayout.setScrimColor(Color.TRANSPARENT);
您需要做几件事:
通过设置透明颜色禁用布局淡化:
drawer.setScrimColor(Color.TRANSPARENT);
锁上抽屉
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
创建自定义抽屉class,在锁定模式下允许点击:
public class CustomDrawer extends DrawerLayout {
public CustomDrawer(Context context) {
super(context);
}
public CustomDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomDrawer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
View drawer = getChildAt(1);
if (getDrawerLockMode(drawer) == LOCK_MODE_LOCKED_OPEN && ev.getRawX() > drawer.getWidth()) {
return false;
} else {
return super.onInterceptTouchEvent(ev);
}
}
}
在xml中使用这个class:
<com.example.myapplication.CustomDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
</FrameLayout>
<ListView android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"/>
</com.example.myapplication.CustomDrawer>
我的问题如下:我在平板电脑的横向模式下锁定了导航抽屉菜单setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN)
,但我需要右侧的片段处于活动状态,所以我总是可以用导航点击它打开。但我不知道该怎么做。请帮忙。
这是一个棘手的问题。当抽屉打开时,它会拦截触发抽屉关闭的触摸事件。为了防止这种情况,您需要将 DrawerLayout 子类化并覆盖 onInterceptTouchEvent 方法:
public class CustomDrawerLayout extends DrawerLayout
{
private View rightView;
private int mTouchSlop;
public CustomDrawerLayout (Context context)
{
this(context, null);
}
public CustomDrawerLayout (Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public CustomDrawerLayout (Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(context));
}
public void setRightView (View v)
{
this.rightView = v;
}
@Override
public boolean onInterceptTouchEvent (MotionEvent ev)
{
boolean result = super.onInterceptTouchEvent(ev);
if (rightView != null && isDrawerOpen(rightView))
{
DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) rightView.getLayoutParams();
if (layoutParams.gravity == Gravity.END)
{
// This is true when the position.x of the event is happening on the left of the drawer (with gravity END)
if (ev.getX() < rightView.getX() && ev.getX() > mTouchSlop)
{
result = false;
}
}
}
return result;
}
}
这是我使用右抽屉的代码。我相信你可以为你的左边抽屉改编这个。您可能还想禁用阴影:
mDrawerLayout.setScrimColor(Color.TRANSPARENT);
您需要做几件事:
通过设置透明颜色禁用布局淡化:
drawer.setScrimColor(Color.TRANSPARENT);
锁上抽屉
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
创建自定义抽屉class,在锁定模式下允许点击:
public class CustomDrawer extends DrawerLayout { public CustomDrawer(Context context) { super(context); } public CustomDrawer(Context context, AttributeSet attrs) { super(context, attrs); } public CustomDrawer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { View drawer = getChildAt(1); if (getDrawerLockMode(drawer) == LOCK_MODE_LOCKED_OPEN && ev.getRawX() > drawer.getWidth()) { return false; } else { return super.onInterceptTouchEvent(ev); } } }
在xml中使用这个class:
<com.example.myapplication.CustomDrawer xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> </FrameLayout> <ListView android:layout_width="100dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111"/> </com.example.myapplication.CustomDrawer>