【发布时间】:2013-11-29 15:52:12
【问题描述】:
我一直在尝试按照 android here 的建议将我的代码移动到 DrawerLayout,因为 SlidingDrawer 已弃用。
我的问题是,到目前为止,DrawerLayout 似乎实现得非常糟糕,有无用的错误消息(没有防御性编程)和/或文档中解释得不够好。
isDrawerOpen() 方法描述为here:
公共布尔 isDrawerOpen(查看抽屉)
检查给定的抽屉视图当前是否处于打开状态。成为 被认为是“打开”的抽屉必须已经完全可见 状态。要检查部分可见性,请使用 isDrawerVisible(android.view.View)。
参数:drawer - 要检查的抽屉视图
如果给定的抽屉视图处于打开状态,则返回 true
isDrawerOpen(View drawer)、openDrawer(View drawer) 和 closeDrawer(View drawer) 中的每一个方法在通过时都不起作用:有问题的 DrawerLayout 或其任何一个子级。我不知道我应该为这些方法提供什么以使它们发挥作用。谁能告诉我?
有关实施的完整问题描述,请参见下文。
我有这样的布局:
<android.support.v4.widget.DrawerLayout
android:id="@+id/mainmenuPanel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/dualPane"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/menuPane"
android:layout_width="300dp"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
在我的代码中,我将以下方法连接到一个按钮:
protected void onCreate(Bundle savedInstanceState) {
...
mMenuPanel = (DrawerLayout) findViewById(R.id.mainmenuPanel);
....
}
public boolean isDrawerOpen() {
if(mMenuPanel != null) {
return mMenuPanel.isDrawerOpen(mMenuPanel);
}
return false;
}
但是,如果您将其本身作为参数(这在极端情况下是多余的),您会收到以下错误:
E/AndroidRuntime(11241): java.lang.ClassCastException:
android.widget.FrameLayout$LayoutParams cannot be cast to
android.support.v4.widget.DrawerLayout$LayoutParams
(您会注意到,它没有给您任何有关您做错了什么的信息。而是问题的症状)。
其他答案here 或here 要么难以理解,要么与问题密切相关,没有太多解释。即便如此,我还是尝试添加一个虚假的 LinearLayout 或一个 DrawerLayouts 孩子,每个孩子都给出 this 错误:
E/AndroidRuntime(11424): java.lang.IllegalArgumentException:
View android.widget.FrameLayout{420f5ea8 V.E..... ........
0,0-800,1172 #7f060038 app:id/menuPane} is not a drawer
谁能解释一下这些方法实际上需要传递给他们才能工作吗?
【问题讨论】:
标签: android navigation-drawer drawerlayout