【发布时间】:2015-04-13 17:09:31
【问题描述】:
我的问题如下:我在平板电脑的横向模式下锁定了导航抽屉菜单setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN),但我需要右侧的片段处于活动状态,所以我可以在导航始终打开的情况下单击它。但我不知道该怎么做。请帮忙。
【问题讨论】:
标签: android android-fragments navigation-drawer
我的问题如下:我在平板电脑的横向模式下锁定了导航抽屉菜单setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN),但我需要右侧的片段处于活动状态,所以我可以在导航始终打开的情况下单击它。但我不知道该怎么做。请帮忙。
【问题讨论】:
标签: android android-fragments navigation-drawer
你需要做一些事情:
通过设置透明色来禁用布局淡化:
drawer.setScrimColor(Color.TRANSPARENT);
锁上抽屉
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
创建一个允许在锁定模式下点击的自定义抽屉类:
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中使用这个类:
<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>
【讨论】:
left 抽屉,right 抽屉需要getRawX() < getWidth() - drawer.getWidth() 之类的东西,即使这样 RTL start/end 也可能会搞砸!
这是一个棘手的问题。当抽屉打开时,它会拦截触发抽屉关闭的触摸事件。为了防止这种情况,您需要继承 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);
【讨论】: