Android 抽屉布局-异常调度输入事件
大约一周前,我开始在一个新项目中使用抽屉布局,它工作得很好。除了偶尔用我的手指将它来回滑动外,它还会崩溃 不过,如果我使用ActionBar Up/Home菜单项,它每次都可以正常工作 在LogCat中,我得到了两个StackTrace,它们似乎都指向android内部的位置,所以我不知道该怎么解决这个问题 我只是试着创建一个新的应用程序,看看这是否是我之前所做的事情导致的,但它也发生在新的应用程序中 我在“new Android application”(新Android应用程序)窗口中使用主/详细流模板创建了新应用程序,将所有内容保留为默认值,并将activity_item_list.xml更改为包含以下内容:Android 抽屉布局-异常调度输入事件,android,android-layout,android-fragments,android-support-library,drawerlayout,Android,Android Layout,Android Fragments,Android Support Library,Drawerlayout,大约一周前,我开始在一个新项目中使用抽屉布局,它工作得很好。除了偶尔用我的手指将它来回滑动外,它还会崩溃 不过,如果我使用ActionBar Up/Home菜单项,它每次都可以正常工作 在LogCat中,我得到了两个StackTrace,它们似乎都指向android内部的位置,所以我不知道该怎么解决这个问题 我只是试着创建一个新的应用程序,看看这是否是我之前所做的事情导致的,但它也发生在新的应用程序中 我在“new Android application”(新Android应用程序)窗口中使用主
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_list"
android:name="com.navdrawertest.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".ItemListActivity"
tools:layout="@android:layout/list_content" />
<!-- Navigation Drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
这里有我遗漏的东西吗
编辑:10天后,我仍然无法解决这个问题,我至少可以得到确认,这不仅仅是我得到的吗?我想我已经通过从支持库源代码创建我自己需要的文件副本,并在
isContentView()
中捕获空视图,解决了这个问题
我需要的源文件包括:
- ActionBarDrawerToggle.java
- ActionBarDrawerToggleHoneycomb.java
- DrawerLayout.java
DrawerLayout
重命名为FixedDrawerLayout
。(因此我可以很容易地分辨出我使用的抽屉布局的副本)
然后我将isContentView()
更改为:
boolean isContentView(View child) {
if(child == null){
return false;
}
return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
}
将我的活动中对DrawerLayout
的所有引用切换到FixedDrawerLayout
后,它似乎已将其修复
不确定只需在isContentView()
中返回false
是否会发生任何事情,但它不再崩溃,即使child
为空,它仍然可用。我也收到了此错误消息,但起初我无法找出是什么原因导致了它。
在做了一些测试之后,我发现删除视图上的所有边距(在您的示例中是片段)似乎可以解决问题
因此,在您的情况下,它将是:
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_list"
android:name="com.navdrawertest.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemListActivity"
tools:layout="@android:layout/list_content" />
您可以在这个片段中的所有视图上设置边距(但我还没有测试它)。
希望它能帮助别人。这个问题似乎并不常见 我在使用导航抽屉时遇到了同样的问题,我只需从片段的根布局中删除android:focusableInTouchMode=“true”
就解决了这个问题。我遇到了类似的问题
我用了一个假的onclick方法来进行碎片布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f1f2f6"
android:onClick="fragmentClick"
android:orientation="vertical" >
问题已解决。基于Mike的回答和用户1050133的评论:
1) 在你的应用程序中创建包android.support.v4.widget
2) 添加名为“FixedDrawerLayout”的新文件,该文件包含以下内容:
public class FixedDrawerLayout extends DrawerLayout {
public FixedDrawerLayout(Context context) {
super(context);
}
public FixedDrawerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FixedDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
boolean isContentView(View child) {
if (child == null) {
return false;
}
return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
}
}
3) 用FixedDrawerLayout替换xml中DrawerLayout的所有用法这感觉像是一个奇怪的解决方案,但它实际上帮助了我,谢谢!很高兴它有帮助,关于这一点没有太多的文档,所以我想这是一个相当新的“bug”(至少听起来是个bug)。它工作得非常好,碎片上没有任何空白。所以这也一定是我的问题的根源。所以我会把它设为可接受的答案。只是一点提示。您不需要复制任何文件。由于isContentView是包私有的,因此只需在与原始包结构等效的包结构中为DrawerLayout子类化。最终得到一个类android.support.v4.widget.RepairedDrawerLayout,该类重写该方法,如果子级为null,则返回false。顺便说一句:如果子项为空,您也可以在此处调用closeAllDrawers()来关闭导航抽屉。谢谢,很高兴知道!我不知道我可以这样子类化。@user1050133子类化DrawerLayout不是一个好主意,因为应用程序项目的包结构(您希望修复的地方)不会是android.support.v4.widget.RepairedDrawerLayout所暗示的包结构。这意味着您将无法在项目中对其进行子类化,除非您更改整个项目结构以匹配包结构。迈克的解决方案是阻力最小的道路。但是我喜欢你将closeAllDrawers()添加到isContentView()的空检查中的想法。太棒了!这是解决此错误的唯一有效方法。A试过其他人,但没有结果。非常好的研究和解决方案,谢谢。谢谢,看起来不错,但我在实现方面遇到了问题:如何实例化FixedDrawerLayout?这种向下投射不适用于我:(FixedDrawerLayout)findViewById(R.id.drawer\U布局);
public void fragmentClick(View v){
Log.i("fragmentClick", "fragmentClick");
return;
}
public class FixedDrawerLayout extends DrawerLayout {
public FixedDrawerLayout(Context context) {
super(context);
}
public FixedDrawerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FixedDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
boolean isContentView(View child) {
if (child == null) {
return false;
}
return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
}
}