Android DrawerLayout - 启用 children 获取焦点
Android DrawerLayout - Enable children to take focus
抽屉是否可以不阻止children接收触摸事件:
这是我的 xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- THE MENU -->
<RelativeLayout
android:id="staic_menu"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/mainDrawer">
<Button
android:layout_width="300dp"
android:layout_width="wrap_content"
android:text="Click Me"
/>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<!-- LEFT DRAWER -->
<RelativeLayout
android:id="@+id/drawerView"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:visibility="gone">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
当抽屉打开时,按钮会被忽略。但是当抽屉关闭时,按钮会起作用。
谢谢,
您需要使用自定义抽屉。
与默认的区别:
- 不会自动关闭
- 打开时不调暗屏幕
- 在抽屉外点击时不拦截触摸
下面的代码有这些disadavantages/limitations:
- 将与左侧的抽屉一起使用(因为宽度检查)
- 将假定
DrawerLayout
的第二个 child 是 ListView
(或其他一些抽屉内容)。
- 默认方式(滑动)无法关闭,只能使用后退按钮。您可以自己实现(有时需要 enable/disable 手动锁定模式)。
UnobtrusiveDrawer.java:
public class UnobtrusiveDrawer extends DrawerLayout {
public UnobtrusiveDrawer(Context context) {
super(context);
}
public UnobtrusiveDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UnobtrusiveDrawer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
View drawer = getChildAt(1);
Log.e("TAG", "Ev: " + ev.getRawX() + " " + drawer.getWidth());
if (isDrawerOpen(drawer) && ev.getRawX() > drawer.getWidth()) {
Log.e("TAG", "Drawer open, and click outside");
return false;
} else {
Log.e("TAG", "Drawer closed or click on it");
return true;
}
}
}
MainActivity.java:
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
drawer.setScrimColor(getResources().getColor(android.R.color.transparent));
drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {}
@Override
public void onDrawerClosed(View drawerView) {}
@Override
public void onDrawerStateChanged(int newState) {}
});
}
}
activity_main.xml:
<com.example.myapplication.UnobtrusiveDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_gravity="right"
android:layout_width="100dp"
android:layout_height="100dp"/>
</FrameLayout>
<ListView
android:layout_gravity="left"
android:background="#FFF"
android:layout_width="250dp"
android:layout_height="match_parent"/>
</com.example.myapplication.UnobtrusiveDrawer>
抽屉是否可以不阻止children接收触摸事件:
这是我的 xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- THE MENU -->
<RelativeLayout
android:id="staic_menu"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/mainDrawer">
<Button
android:layout_width="300dp"
android:layout_width="wrap_content"
android:text="Click Me"
/>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<!-- LEFT DRAWER -->
<RelativeLayout
android:id="@+id/drawerView"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:visibility="gone">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
当抽屉打开时,按钮会被忽略。但是当抽屉关闭时,按钮会起作用。
谢谢,
您需要使用自定义抽屉。
与默认的区别:
- 不会自动关闭
- 打开时不调暗屏幕
- 在抽屉外点击时不拦截触摸
下面的代码有这些disadavantages/limitations:
- 将与左侧的抽屉一起使用(因为宽度检查)
- 将假定
DrawerLayout
的第二个 child 是ListView
(或其他一些抽屉内容)。 - 默认方式(滑动)无法关闭,只能使用后退按钮。您可以自己实现(有时需要 enable/disable 手动锁定模式)。
UnobtrusiveDrawer.java:
public class UnobtrusiveDrawer extends DrawerLayout {
public UnobtrusiveDrawer(Context context) {
super(context);
}
public UnobtrusiveDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UnobtrusiveDrawer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
View drawer = getChildAt(1);
Log.e("TAG", "Ev: " + ev.getRawX() + " " + drawer.getWidth());
if (isDrawerOpen(drawer) && ev.getRawX() > drawer.getWidth()) {
Log.e("TAG", "Drawer open, and click outside");
return false;
} else {
Log.e("TAG", "Drawer closed or click on it");
return true;
}
}
}
MainActivity.java:
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
drawer.setScrimColor(getResources().getColor(android.R.color.transparent));
drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {}
@Override
public void onDrawerClosed(View drawerView) {}
@Override
public void onDrawerStateChanged(int newState) {}
});
}
}
activity_main.xml:
<com.example.myapplication.UnobtrusiveDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_gravity="right"
android:layout_width="100dp"
android:layout_height="100dp"/>
</FrameLayout>
<ListView
android:layout_gravity="left"
android:background="#FFF"
android:layout_width="250dp"
android:layout_height="match_parent"/>
</com.example.myapplication.UnobtrusiveDrawer>