How to add one section separator for Navigation Drawer in Android?

前端 未结 6 1709
粉色の甜心
粉色の甜心 2020-12-22 20:43

I have a navigation drawer like this image. I want to add a section separator (like the line separating Neptune). It seems simple but I can\'t find anything on the web that

6条回答
  •  难免孤独
    2020-12-22 21:03

    You have two choices

    1. Your items can be separated (a list at the top, and classic views at the bottom). Then instead of the listview in your main layout (android:id="@+id/left_drawer") you can have a rather complex LinearLayout including those 3 items (list, separator, and bottom views)
    2. Your items must be exactly as in your example, then you need the separator in the list, you can use some logic in your adapter to draw a view on top of the list item where you need the separator. (meaning your list item won't be a single textview anymore, but a LinearLayout with a gone separator (and visible sometimes, according to your adapter's logic).

    To help you with some sample code, can you please post all the items you need in your menu ? We need to know exactly what will be static and what will be scrollable.

    Edit: If you want that working with the exemple, get rid of the line

    mDrawerList.setAdapter(new ArrayAdapter(this, ...)
    

    You need to supply a home made adapter like this: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

    As i said in 2, in your adapter, you will have logic, and thus you can say in the getView() method

    if(myPlanet.isNeptune()) 
        holder.mSepatator.setVisibility(View.VISIBLE);
    else 
        holder.mSepatator.setVisibility(View.GONE);
    

提交回复
热议问题