简体   繁体   中英

How to set a FragmentList in a NavigationDrawer?

I basically followed some tutorials on how to create a fragment list and how to make use of a navigation drawer. Here is what those elements look like.

ListFragment: https://youtu.be/GgUKTzMmxIs

NavigationDrawer: https://youtu.be/6SrKOBV_hx8

The problem is that I don't want a set list of elements on my navigation drawer I want it to be dinamic so I can retrieve objects from an API, list those objects on my navigation drawer and be able to see that list of objects and scroll through it. Navigation drawer has a property called menu which can use a menu xml resoruce file to fill itself with elements. What I'm trying to do is basically that but with a ListFragment instead of a menu.xml but I don't know how to do it.

How to set a FragmentList in a NavigationDrawer? Follow my code you easily create set of Fragment in NavigationDrawer

> manu.XML:

  • i have create 3 tab for 3 Fragment.

     <item android:id="@+id/menu_home" android:icon="@drawable/ic_baseline_home_24" android:title="@string/home"/> <item android:id="@+id/menu_call" android:icon="@drawable/ic_baseline_call_24" android:title="@string/call"/> <item android:id="@+id/menu_search" android:icon="@drawable/ic_baseline_search_24" android:title="@string/search"/>
  • create fragment do you need. MainActivity.class

    public class MainActivity extends AppCompatActivity {

     private BottomNavigationView bnv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bnv=(BottomNavigationView) findViewById(R.id.bottom_navigation); //app start thata j home fragme t load kri lesu getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,new HomeFragment()).commit(); bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { //first apne ek temparary fragment create krsu e fragmnent ma jyare item click krse tyare tema // baki na fragment ne replace kri dese // md jaml -> https://youtu.be/Q3WtyqMBZ8c Fragment temp = new Fragment(); switch(item.getItemId()) { case R.id.menu_home: temp = new HomeFragment(); break; case R.id.menu_call: temp = new CallFragment(); break; case R.id.menu_search: temp = new SearchFragment(); } //have temp fragment ne main layout na fragment ma replace kri desu getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,temp).commit(); return true; } }); }

activity_main.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation"
        android:background="@color/purple_200"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_navigation"
        android:background="#E0DBEB"
        android:layout_alignParentBottom="true"
        app:menu="@menu/navigation_menu"/>


</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM