How to create bottom navigation bar similar to instagram

若如初见. 提交于 2019-11-30 04:08:22

Should I use three activities to display the content of corresponding tabs or fragments and how can I achieve that?

Definitely you should use Fragment for each bottom navigation Item / Tab. Like FragmentHome, FragmentSearch and FragmentSettings.

To change the Fragment, add NavigationItemSelectedListener to your BottomNavigationView and change Fragment as per MenuItem selection:

    BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.bottom_navigation_view);

    bottomNavigationView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.action_item1:
                            selectedFragment = FragmentHome.newInstance();
                            break;
                        case R.id.action_item2:
                            selectedFragment = FragmentSearch.newInstance();
                            break;
                        case R.id.action_item3:
                            selectedFragment = FragmentSettings.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);
                    transaction.commit();
                    return true;
                }
            });

Here is a tutorial about: BottomNavigationView with multiple Fragments

I need a recyclerview to display appointments

In your Fragment's layout XML, add a RecyclerView to show list of appointments. In your Fragment class, initialize RecyclerView and create an ArrayList<Appointment> and pass this list to your Adapter to show on RecyclerView row items.

Here is an tutorial about: How to use RecyclerView in Fragment

How can I display the search bar only when the search icon at bottom is clicked?

You can show/hide option item programmatically from ToolBar/ActionBar as per your Fragment change.

In your FragmentSearch, do below changes to show Searchbar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragmet_search, parent, false);
    return v;
}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
   inflater.inflate(R.menu.your_search_menu_xml, menu);
   super.onCreateOptionsMenu(menu, inflater);
}

Here are some useful links:

  1. Android Toolbar Adding Menu Items for different fragments
  2. Hide/Show Action Bar Option Menu Item for different fragments
  3. Adding ActionBar Items From Within Your Fragments

Hope this will help to understand the scenario.

you put your bottom navigation in the activity and fill it with whatever you want then you use this

bottomNavigationView.setOnNavigationItemSelectedListener(this);

To add an item select listener. You also implement required method in your activity like this

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_home:
            //open home fragment
            break;

        case R.id.action_search:
            //open search or whatever
            break;

    }

    return true;
}

Now it's up to you to do whatever you want when some item is selected, for example when search item is selected you can show search section and hide it when other items are selected.

If you want a library for BottomBar here's one. You have to control Visibility of your SearchBar on the click of that bottom bar item. If you are trying to implement your own searchBar then check out this links link1 link2

The concept of tabs is that you have to have a MainActivity, which will handle let's say 3 fragments. With the code that @Alireza suggested, you will handle the change between fragments. Please look over a tutorial on how to implement Tabs ( like this one here ) . After that, in your "SearchFragment.java" you will build your XML accordingly with your Search bar on top, and recycler view below, or whatever you wish. Suggestion: When using fragments, be sure to have a Context mContext = getActivity(); defined first, as it will be easier for you to implement all the features.

*For you to achieve the bottom tab effect, you can move android.support.design.widget.TabLayout to the bottom of the screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!