我移动到DrawerLayout
但是所述布局还包含ImageView
,其上面有onTouchListener
用于缩放/旋转功能。
当我尝试任何ImageView
函数时,问题是抽屉自己的触控处理程序崩溃我的应用程序。
我已尝试设置drawerLayout.requestDisallowInterceptTouchEvent(false);
,但这并没有真正帮助。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- map container -->
<ImageView
android:id="@+id/map_layout"
android:background="@color/divider_color"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- navigation drawer -->
<ListView
android:id="@+id/navigation_drawer"
android:choiceMode="singleChoice"
android:divider="@color/divider_color"
android:dividerHeight="1dp"
android:background="@color/button_label"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="fill_parent" />
</android.support.v4.widget.DrawerLayout>
例外:
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent(ViewDragHelper.java:1014)
at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:1140)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1859)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2072)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1519)
at android.app.Activity.dispatchTouchEvent(Activity.java:2467)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2020)
at android.view.View.dispatchPointerEvent(View.java:8017)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3977)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3856)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3466)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3435)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3542)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3443)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3599)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3466)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3435)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3443)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5565)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5545)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5516)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5645)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5618)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5664)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:542)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:4)
我发现只有简短而有效的解决方案是扩展DrawerLayout并覆盖onTouchEvent
和onInterceptTouchEvent
方法。
首先,我首先根据Layout
创建自己的自定义DrawerLayout
我称之为NavigationDrawer:
NavigationDrawer.java
import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;
public class NavigationDrawer extends DrawerLayout {
private ListView menu;
public NavigationDrawer(Context context) {
super(context);
}
public NavigationDrawer(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public NavigationDrawer(Context context, AttributeSet attributeSet, int disp) {
super(context, attributeSet, disp);
}
public void addMenu(ListView menu) {
this.menu = menu;
}
/**
* Override the method to correct the detection region for the navigation layout open/close case.
* @param ev Motion event descriptor
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
if (ev.getX() > 30 && ev.getAction() == MotionEvent.ACTION_DOWN) {
if (isDrawerOpen(menu) || isDrawerVisible(menu)) {
return true;
} else {
return false;
}
}
return false;
}
/**
* Override the method to catch ArrayIndexOutOfBounds exception thrown in conflict between
* the two touch listeners.
* @param ev Motion event descriptor
* @return
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (ArrayIndexOutOfBoundsException e ) {
return false;
}
}
}
创建它的布局:
<?xml version="1.0" encoding="utf-8"?>
<...views.NavigationDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- My own content in here most likely you will want swappable fragment. -->
</FrameLayout>
<!-- List of our menu options -->
<ListView
android:id="@+id/menu"
android:choiceMode="singleChoice"
android:divider="@color/divider_color"
android:dividerHeight="1dp"
android:background="@color/button_label"
android:clickable="true"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="fill_parent" />
</...views.NavigationDrawer>
还需要ListView
项目:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/navigation_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="20dp"
android:textSize="@dimen/main_title"
android:background="#FFFFFF"
android:textColor="@color/checkbox_label" />
然后创造一切:
// Navigation menu
private NavigationDrawer menuLayout;
private ActionBarDrawerToggle menuToggle;
private ListView menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_navigator);
// Setup the navigation drawer layout
String[] menuOptions = getResources().getStringArray(R.array.menu_options);
menu = (ListView) findViewById(R.id.menu);
menuLayout = (NavigationDrawer) findViewById(R.id.content_layout);
menuLayout.addMenu(menu);
menuLayout.requestDisallowInterceptTouchEvent(false);
// Populate the menu with options
menu.setAdapter(new ArrayAdapter<String>(this, R.layout.layout_navigator_item, menuOptions));
menu.setOnItemClickListener(new MenuItemSelected());
menuToggle = new ActionBarDrawerToggle(
this, menuLayout, null, R.string.open, R.string.closed) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
}