在导航抽屉中添加两个列表视图,但只有一个起作用。

7
我尝试创建一个带有两个单独listview的导航抽屉。 第一个listview称为"mDrawerList",显示良好。 - 此列表中只有一个项目。
第二个listview称为"mListProcheDeChezVous",从未显示。 - 此列表中有3个项目。
当我注释掉第一个列表时,第二个列表不会显示,所以我认为在创建第二个列表时存在问题,但我不知道在哪里? 以下是导航抽屉布局的代码:
<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:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/dernieres_news"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#E3E9E3"
        android:dividerHeight="1dp"
        android:background="#F3F3F4"/>

    <ListView
        android:id="@+id/pres_de_chez_vous"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#E3E9E3"
        android:dividerHeight="1dp"
        android:background="#F3F3F4"/>

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

这是我的MainActivity类的一段代码:

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList, mListProcheDeChezVous;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // Declaration of the 2 listview's 
        mDrawerList = (ListView) findViewById(R.id.dernieres_news);
        mListProcheDeChezVous= (ListView) findViewById(R.id.pres_de_chez_vous);

        LayoutInflater inflater = getLayoutInflater();

        // Add header news title
        ViewGroup header_news = (ViewGroup)inflater.inflate(R.layout.header_dernieres_news, mDrawerList, false);
        mDrawerList.addHeaderView(header_news, null, false);

        // Add header "proche de chez vous title"
        ViewGroup header_pres_de_chez_vous = (ViewGroup)inflater.inflate(R.layout.header_pres_de_chez_vous, mListProcheDeChezVous, false);
        mListProcheDeChezVous.addHeaderView(header_pres_de_chez_vous, null, false);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        /*
         * FIRST ADAPTER FOR FIRST LISTVIEW
         */

        String[] names=new String[]{"Dernières News"};

        /*Array of Images*/
        int[] image = new int[] {R.drawable.ic_action_feed};

        List<HashMap<String, String>> listinfo = new ArrayList<HashMap<String, String>>();
        listinfo.clear();
        for(int i=0;i<1;i++){
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("name", names[i]);
            hm.put("image", Integer.toString(image[i]));
            listinfo.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { "image", "name" };
        int[] to = { R.id.img, R.id.txt };
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), listinfo, R.layout.drawer_list_item, from, to);
        mDrawerList.setAdapter(adapter);

        /*
         * SECOND ADAPTER FOR SECOND LISTVIEW
         */

        String[] names_pres_de_chez_vous = new String[]{"Finistère", "Morbihan", "Côtes d'Armor"};

        /*Array of Images*/
        int[] image_pres_de_chez_vous = new int[] {R.drawable.ic_action_gear, R.drawable.ic_action_gear, R.drawable.ic_action_gear};

        List<HashMap<String, String>> listinfo_pres_de_chez_vous = new ArrayList<HashMap<String, String>>();
        listinfo_pres_de_chez_vous.clear();
        for(int i=0;i<3;i++){
            HashMap<String, String> hm_pres_de_chez_vous = new HashMap<String, String>();
            hm_pres_de_chez_vous.put("name", names_pres_de_chez_vous[i]);
            hm_pres_de_chez_vous.put("image", Integer.toString(image_pres_de_chez_vous[i]));
            listinfo_pres_de_chez_vous.add(hm_pres_de_chez_vous);
        }

        // Keys used in Hashmap
        String[] from_pres_de_chez_vous = { "image", "name" };
        int[] to_pres_de_chez_vous = { R.id.img, R.id.txt };
        SimpleAdapter adapter_pres_de_chez_vous = new SimpleAdapter(getBaseContext(), listinfo_pres_de_chez_vous, R.layout.drawer_list_item_pres_de_chez_vous, from_pres_de_chez_vous, to_pres_de_chez_vous);
        mListProcheDeChezVous.setAdapter(adapter_pres_de_chez_vous);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        mListProcheDeChezVous.setOnItemClickListener(new DrawerItemClickListener());

感谢您的帮助,
此致
敬礼

DrawerLayout只支持两个子视图。你为什么需要两个ListView呢? - Ahmad
4个回答

4
根据创建导航抽屉指南DrawerLayouts应该只有两个子项。第一个是主内容视图,第二个是抽屉视图。
您的id为"content_frame"的FrameLayout被解释为具有主内容的视图,而id为"dernieres_news"的ListView被解释为抽屉布局。
因此忽略了第三个ListView
如果您需要将两个ListView作为抽屉的一部分,则应将它们包装在另一个布局中,例如LinearLayout

2
谢谢,你的解释很清楚。我尝试将这两个列表视图分组到线性布局中,但是当应用程序启动时,我遇到了这个错误:Unable to start activity ComponentInfo{com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams - wawanopoulos
嘿,wawanopoulos,我也遇到了和你一样的问题。也许这是谷歌的限制,有人能给出一些解决方案吗? - Jaden Gu
以防万一,如果有人遇到了像@wawanopoulos和我一样的“ClassCastException”,至少在我的情况下,原因是活动周围的代码仍然使用旧的单个ListView来进行openDrawer()、closeDrawer()等操作。 - lupz

3

在LinearLayout中包装ListView是个好的解决方案。我刚刚就是这样做的,而且它有效。这是我的演示xml:

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

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="240dp"
            android:layout_height="wrap_content" 
            android:text="ABC"/>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </LinearLayout>

在使用openDrawer、closeDrawer等功能时,请确保使用drawer_Linearlayout。isDrawerOpen作为参数。


1
你可以在ViewGroup中定义多个ListView / 任何自定义视图:
主DrawerLayout XML活动的代码:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawerlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

<!-- This is the fragment Layout -->

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

 <!-- this LinearLayout as a Drawerlayout inside it create two ListView With Its Items Title -->

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

            <TextView
                android:id="@+id/tvsocialsites"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="#12ccad"
                android:gravity="center"
                android:text="@string/socialsites"
                android:textSize="15sp" />

            <ListView
                android:id="@+id/drawerlistleft"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffccee"
                android:choiceMode="singleChoice"
                android:divider="@null" />

            <TextView
                android:id="@+id/tvweakdays"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="#12ccad"
                android:gravity="center"
                android:text="@string/weaksdy"
                android:textSize="15sp" />

            <ListView
                android:id="@+id/drawerlistweakdy"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#32fbdc"
                android:choiceMode="singleChoice" >
            </ListView>
        </LinearLayout>

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

0

第一个列表视图具有 android:layout_height="match_parent" 这意味着它占用整个抽屉布局... 在两个列表视图中使用 android:layout_height="wrap_content"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接