如何在导航抽屉中放置2个ListView?

时间:2014-04-08 15:45:15

标签: android xml listview android-listview navigation-drawer

这是我的导航drawer_main的xml架构... 我必须将用户图像放在上面的列表视图和其他数据通过第二列表视图,就像soundcloud一样。但我在导航抽屉按钮中得到第二个listview,并且通过移动边缘我得到第一个抽屉..帮我合并两个 XML代码

<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" >    

    <FrameLayout
        android:id="@+id/content_frame"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/linerlayout_drawer"
        android:layout_height="match_parent"
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:layout_weight="1"/>

    <ListView
        android:id="@+id/listview_drawer"
        android:layout_height="match_parent"
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:layout_weight="1" />

</android.support.v4.widget.DrawerLayout>

如果我将它们包裹在线性布局中

,则会出错
04-08 21:08:36.605: E/AndroidRuntime(9373): FATAL EXCEPTION: main
04-08 21:08:36.605: E/AndroidRuntime(9373): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ZDevelopers.Workmania/com.ZDevelopers.Workmania.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.os.Looper.loop(Looper.java:176)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.app.ActivityThread.main(ActivityThread.java:5419)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at java.lang.reflect.Method.invoke(Method.java:525)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at dalvik.system.NativeStart.main(Native Method)
04-08 21:08:36.605: E/AndroidRuntime(9373): Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:857)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1058)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at com.ZDevelopers.Workmania.MainActivity.selectItem(MainActivity.java:203)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at com.ZDevelopers.Workmania.MainActivity.onCreate(MainActivity.java:142)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.app.Activity.performCreate(Activity.java:5372)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-08 21:08:36.605: E/AndroidRuntime(9373):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-08 21:08:36.605: E/AndroidRuntime(9373):     ... 11 more

2 个答案:

答案 0 :(得分:5)

@Zain将Listviews包装到LinearLayout中。

像这样: -

<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" >    

    <FrameLayout
        android:id="@+id/content_frame"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
<LinearLayout
        android:id="@+id/mainLinearLayout"
 android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ListView
        android:id="@+id/linerlayout_drawer"
        android:layout_height="match_parent"
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:layout_weight="1"/>

    <ListView
        android:id="@+id/listview_drawer"
        android:layout_height="match_parent"
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:layout_weight="1" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

然后改变你的

mDrawerLayout.isDrawerOpen(mainLinearLayout);

mDrawerLayout.closeDrawer(mainLinearLayout);

答案 1 :(得分:2)

我有类似的情况,我必须在导航抽屉里使用TextView和2 ListView。上面的解决方案对我有所帮助,但我不得不将其修改为

<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" >    

<FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/ll_main_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_main"
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:background="@color/list_background"
            android:gravity="center_vertical"
            android:paddingRight="40dp"
            android:text="@string/nav_cat_main"
            android:textColor="@color/list_item_title" />


        <ListView
            android:id="@+id/lv_slidermenu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/list_background"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector" />

        <ListView
            android:id="@+id/lv_gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/list_background"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector" />

</android.support.v4.widget.DrawerLayout>

可以看出,我必须使用layout_gravity =“start”进行线性布局,并从内部视图中删除相同的属性。