0

I'm working on NavigationDrawer that has a FrameLayout as the first child, and a ListView as the second. I've successfully populated a list of items I want programmatically. Now, I want a little bit of modification. I want another list populated inside the navigation drawer but I want it to be bottom-aligned. I looked at a couple of examples but couldn't make it work. Can you tell me how can I make it work? Here's my main Navigation layout (activity_navigation.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:layout_width="match_parent"
        android:layout_height="match_parent" />

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:background="#111"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

My individual list itmes file (listview_item_row.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/navigation_drawer_list_item_selector"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/imageViewIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingRight="10dp" />

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageViewIcon"
        android:paddingRight="10dp"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="#ffffff" />

</RelativeLayout>

In my activity class, I'm programatically setting layout this way:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];
drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_speedometer, mNavigationDrawerItemTitles[0]);
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_place, mNavigationDrawerItemTitles[1]);
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_price, mNavigationDrawerItemTitles[2]);

DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
mDrawerList.setAdapter(adapter);        
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

How can I make another list just the way I'm creating the current one? I want it to look like this: enter image description here (picture took from this question)

Kind of new to the Android world. Thank you in advance. Update: I was able to nest my ListView inside a LinearLayout, then created another ListView inside that LinearLayout. Now, how do I assign ListView items to the bottom ListView controller?

Community
  • 1
  • 1
Basit
  • 1,830
  • 2
  • 31
  • 50

1 Answers1

0

Use a RelativeLayout for your drawer rather than the ListView itself, embed your list view in there aligned to the top then stick another ListView in there assigned to the bottom.

For example:

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

<RelativeLayout
    android:id="@+id/left_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111" >

    <ListView
        android:id="@+id/top_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParent_left="true"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <ListView
        android:id="@+id/bottom_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParent_left="true"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • Did that. But when I passed the ListView `mDrawerList = (ListView) findViewById(R.id.left_drawer);`, I received a cast exception. – Basit Aug 02 '14 at 12:49
  • If have to see the code there to understand the exception, this approach does work. – Larry Schiefer Aug 02 '14 at 12:52
  • I got `LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams` when I added a RelativeLayout as ListView's parent and in that layout, added another RelativeLayout with `android:layout_alignParentBottom="true"` – Basit Aug 02 '14 at 12:55
  • Could you provide a sample XML layout with your solution? And how can I add that to the drawer without it causing exceptions? – Basit Aug 02 '14 at 12:58
  • Check the view id you assigned to the list. This sounds like you (correctly) named the new relative layout R.id.left_drawer, but you are retrieving it as a list. You'd need to retrieve the list using the correct id. – Larry Schiefer Aug 02 '14 at 12:58
  • But, `left_drawer` is a ListView. – Basit Aug 02 '14 at 13:01
  • I think you are missing what I'm saying: your to level container for the left drawer (alignment=start) needs to be a layout. You put your lists inside it. Right now you are setup so your left drawer is only a single list. – Larry Schiefer Aug 02 '14 at 16:51
  • I'm getting your point, I'm just having a hard time implementing it. Could you provide some form of sample as to how it would work? – Basit Aug 02 '14 at 16:56
  • 1
    Just revised my answer with an example. RelativeLayout is your "left drawer" container. Your lists are within it and put at top and bottom. – Larry Schiefer Aug 02 '14 at 18:07
  • Thank you, Larry. I was able to achieve the same with two `ListView`'s nested inside a `LinearLayout`. I just want to know how to add them in my drawer like I was able to with one ListView. – Basit Aug 02 '14 at 19:16
  • You are very welcome, I'm glad you were able to get it going. – Larry Schiefer Aug 02 '14 at 19:58
  • I would still need them to add to the drawer, though. I'm lost there. Prettttty new to Android. – Basit Aug 02 '14 at 21:17
  • See the layout I put in my answer. There would be 2 lists in the left drawer, one anchored at the top and one at the bottom. – Larry Schiefer Aug 02 '14 at 21:20
  • I would have to fill `bottom_list` with an adapter first like I did for the only ListView element. I can't seem to do that for both. – Basit Aug 02 '14 at 21:27
  • You can do two separate adapters, the system doesn't care how many you make. I've used this pattern before with no problems. – Larry Schiefer Aug 03 '14 at 00:11
  • I tried that without any success. Could you provide code specific to adding multiple adapters to the drawer layout? Appreciate your help. – Basit Aug 03 '14 at 04:49