Sub menu in navigation drawer

2019-07-27 05:35发布

I'm trying to find solution how to create sub menu in navigation drawer. I will be the best to show example from iOS app video

I dont see any methods in navigation drawer documentation to do such layout animations. I will appreciate any help

1条回答
看我几分像从前
2楼-- · 2019-07-27 06:12

there is not such concept called sub menu in navigation drawer on the android SDK.

but there are also good news - since Navigation Drawer is eventually layout container - nothing stops you from host inside of it any view or nested layout container you desire instead of host inside of it only ListView , and therefore - you can use also Fragments

if you'll do that - you could perform fragment transactions between them to swipe to another fragment representing the sub menu... also you'll get "for free" the fragment transaction animation, if you'll set it to use one.

each fragment would show ListView (or whatever..) representing different menu screen

add to it you own UI back button header and implement the onClick callback to perform fragment transaction to the "main menu" fragment, and you've got exactly what you want.

this is how your main activity UI xml layout file should look like:

<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">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<FrameLayout android:id="@+id/left_drawer_layout_fragment_conatiner"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:background="#111"/>

as you see, I replaces the traditional navigation drawer ListView with a FrameLayout. the left drawer can be replaced with any other view or custom component you want, and can act also as fragment container.

EDIT

@Meryl requested from me to show based on the documentation example how I use Fragment as drawer menu. so, assuming you want that your menu would look exactly like the documentation example (which is not must at all because you can make any UI you wish..) this would be the way to translate it into Fragment:

the fragment_menu.xml layout file:

<ListView android:id="@+id/list_view"
    xmlns:android="http://schemas.android.com/apk/res/android"        
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

the FragmentMenu java class:

public class FragmentMenu extends Fragment {

private ListView mListView;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {
    return inflater.inflate(R.layout.fragment_menu, null);
  }

  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);

      mListView = (ListView)view.findViewById(R.id.list_view);

      // Set the adapter for the list view
      mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles));
      // Set the list's click listener
      mListView.setOnItemClickListener(new DrawerItemClickListener());
  }
}

attaching the FragmentMenu to the Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView...


    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.left_drawer_layout_fragment_conatiner, new FragmentMenu());
                   .commit();
}

all the rest should stat almost the same.. and of course - the code in the documentation that creates the menu list and setting the adapter in the activity is not necessary anymore, because it's implemented inside the fragment instead.

查看更多
登录 后发表回答