Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 2 listview's in navigation drawer, only one works

I try to create a navigation drawer with 2 seperate listview inside.

The first listview called "mDrawerList" is well displayed. - There is only one item in this list.

The second listview called "mListProcheDeChezVous" is never displayed. - There is 3 items in this listview.

When i put in comments, the first listview, the second is not displayed,so i think there is a problem when a create the second listview.. but i don't know where ?

Here is the code for the layout of navigation drawer :

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

And here is a piece of code of my MainActivity class :

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());

Thank you for your help,

BR

like image 280
wawanopoulos Avatar asked Jun 05 '13 16:06

wawanopoulos


People also ask

How do I customize my navigation drawer?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

How do I get navigation drawer in all activity?

The user can view the navigation drawer when they swipe the activity's screen from the left edge of the android device. A user can also find it from the activity, by tapping the app icon (also known as the “hamburger” menu) in the action bar.


2 Answers

As per the Creating a Navigation Drawer guide, DrawerLayouts should only have two children. The first is the main content view, and the second is the drawer view.

Your FrameLayout with an id of "content_frame" is being interpreted as the view with the main content, and your ListView with an id of "dernieres_news" is being interpreted as the drawer layout.

The third ListView is thus ignored.

If you need both ListView as a part of the drawer, you should wrap them in another layout such as a LinearLayout.

like image 143
Bryan Herbst Avatar answered Sep 20 '22 19:09

Bryan Herbst


Wrap the ListView in LinearLayout is a great solution. I just did this way and it works. Here is my demo 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>

and be sure to use drawer_Linearlayout when you use such as openDrawer, closeDrawer. isDrawerOpen as the param.

like image 20
Jaden Gu Avatar answered Sep 17 '22 19:09

Jaden Gu