在导航栏中添加2个列表视图,只有一个可以使用

waw*_*los 7 android android-listview navigation-drawer

我尝试在里面创建一个带有2个单独列表视图的导航抽屉.

第一个名为"mDrawerList"的列表视图很好地显示出来. - 此列表中只有一个项目.

永远不会显示名为"mListProcheDeChezVous" 的第二个列表视图. - 此列表视图中有3个项目.

当我输入评论时,第一个listview,第二个没有显示,所以我认为创建第二个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/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>
Run Code Online (Sandbox Code Playgroud)

这是我的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());
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助,

BR

Tan*_*.7x 4

根据创建导航抽屉指南DrawerLayouts应该只有两个子项。第一个是主要内容视图,第二个是抽屉视图。

idFrameLayout为“content_frame”的视图将被解释为包含主要内容的视图,而ListViewid 为“dernieres_news”的视图将被解释为抽屉布局。

ListView因此第三个被忽略了。

如果您需要两者ListView作为抽屉的一部分,则应该将它们包装在另一种布局中,例如LinearLayout.

  • 谢谢,按照你的解释就很清楚了。我尝试将 2 个列表视图分组为线性布局,但启动应用程序时出现此错误:`无法启动活动 ComponentInfo{com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.MainActivity}: java .lang.ClassCastException:android.widget.LinearLayout$LayoutParams 无法转换为 android.support.v4.widget.DrawerLayout$LayoutParams` (2认同)