Multiple containers for navigation boxes and best practices

I am relatively new to Android UI development. So I need help from more experienced developers.
I am building my application with Navigation Drawer , I have encountered many problems.
Firstly, here is a sample code for the navigation box.

 <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 --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#111"/> </android.support.v4.widget.DrawerLayout> 

This is a simple example from the official site.

The main kind of content (FrameLayout above) should be the first child in DrawerLayout, since the XML order implies a z-order and the box must be on top of the content.

In this example, there are two nested views: FrameLayout , as mentioned earlier, for storing the main content, and ListView for presenting the NavigationDrawer menu.

So here are my questions.

  • A real application does not have only one view for all activities. Basically, the activity has many different views and several fragments to make the interface more responsive and provide the user with a better UX. My actions are, of course, different views and fragments (what is the purpose of the same activity? :)). One activity has only one fragment, the other has several fragments, so they are ALL different ... In the previous code example, there is ONLY ONE FrameLayout container for storing the fragment, only one fragment, which I need to do if my activity has two, three or more fragment? What is the best solution in this case.
  • As I described, my application has many actions, so my navigation box should (in accordance with the practice of Android UI) for each action, so that the user can go to the top-level elements of the application. In this case, it is better to have one BaseActivity (here I read the answers to stackoverflow), which will contain all the configuration files for NavigationDrawer . But since DrawerLayout is required in the navigation box in the activity XML layout, BaseActivity must call the setContentView method. And all the actions inherited from BaseActivity must do some tricks to set their own layout, or it makes no sense to redefine this method, because it will break all the logic. What to do in this case?
  • What is the most efficient way to create a custom list of navigation boxes, for example on Google + or YouTube, without using a complex hierarchy of views that makes the GPU work and can bring the user a bad experience.

For the second problem, I have some ideas on how I can solve this, but I don’t know if this is right and which approach is better.
The first idea is to implement the TemplateMethod template. Create a method in BaseActivity something like setupCustomViewGroup() and redefine it in all assets to inflate the desired group of views from xml and return it from this method, and then use it to inflate the whole view.

Or explicitly add a view to the action, use the merge and include tag.

But again, this can only be useful for whole activities or a single fragment, what if we have more fragments, use nested fragments, I think this is a bad idea.

In addition, my goal is to optimize the hierarchy of views to make it fast and efficient, to ensure smoothness even on older devices.

I will be very very grateful to anyone who can describe or suggest the most effective and correct way to follow in this case, to make the user interface responsive and readable.

Thanks.

+6
source share
3 answers

1) Drag from NavigationDrawer to SlideMenu (available on github).

2) Make sure BaseActivity has no layout at all.

3) Inflate your list item through an ArrayAdapter.

Hope this helps.

+2
source

Decision:

1. I can present here 2 cases:

  • Several fragments, single activity. In this case, the best way is to replace fragments in FrameLayout content_frame while you switch between fragments. navigation drawer remains the same for them
  • Several fragments, several actions. In this case, you must define different navigation drawer content navigation drawer and use them when calling setHasOptionsMenu(true) when switching between actions.

2. If you use ListView as a navigation drawer , you can easily get your work done by dynamically populating the row ListView . Avoid exceeding BaseActivity as much as possible for this scenario. If you do not use ListView as a navigation drawer content, you can define a different layout for different activities, as described above. 1

3. I think the Google+ app uses ListView as the navigation drawer its custom design. It depends on the needs of your application, either you can go with List or create simple custom layouts for your application

I hope my decision will resolve your doubts to some extent.

+1
source

you can use fragments and call them from mainActivity like this:

 MainActivity.java public class MainActivity extends Activity { .. .. @Override protected void onCreate(Bundle savedInstanceState) { .. mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); } /** * Slide menu item click listener * */ private class SlideMenuClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // display view for selected nav drawer item displayView(position); } } /** * Diplaying fragment view for selected nav drawer list item * */ private void displayView(int position) { // update the main content by replacing fragments Fragment fragment = null; switch (position) { case 0: fragment = new HomeFragment(); break; case 1: fragment = new FindPeopleFragment(); break; case 2: fragment = new PhotosFragment(); break; case 3: fragment = new CommunityFragment(); break; case 4: fragment = new PagesFragment(); break; case 5: fragment = new WhatsHotFragment(); break; default: break; } if (fragment != null) { FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); mDrawerList.setSelection(position); setTitle(navMenuTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } else { // error in creating fragment Log.e("MainActivity", "Error in creating fragment"); } } 

}

check out this tutorial, it can help you more

0
source

Source: https://habr.com/ru/post/986032/


All Articles