Here if we use NavigationDrawer (android.support.v4.widget.DrawerLayout;) items will be the fragments and hence in case if we have a listview in the main activity which should not be shown in case navigation drawer item is selected then we should implement listview of main activity as the FragmentList.
If so we can replace fragments at runtime very easily
Layout of the MainActivity will be as follows
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/container_body"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.example.hp.listview.Fragment.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Oncreate of the main activity we attach our FragmentList as follows
If so we can replace fragments at runtime very easily
Layout of the MainActivity will be as follows
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/container_body"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.example.hp.listview.Fragment.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Oncreate of the main activity we attach our FragmentList as follows
Fragment monthsListFragment = new MyListFragment(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.container_body,monthsListFragment,null); fragmentTransaction.commit();
Here MyListFragment is the FragmentList which is to be created
When user selects navigationDrawer item then we replace container body which is a frame layout with the corresponding selected item fragment
@Overridepublic void onDrawerItemSelected(View view, int position) { displayView(position); } private void displayView(int position) { Fragment fragment = null; String title = getString(R.string.app_name); switch (position) { case 0: fragment = new HomeFragment(); title = getString(R.string.title_home); break; case 1: fragment = new FriendsFragment(); title = getString(R.string.title_friends); break; case 2: fragment = new MessagesFragment(); title = getString(R.string.title_messages); break; default: break; } if (fragment != null) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.container_body, fragment); fragmentTransaction.commit(); // set the toolbar title if(getSupportActionBar()!=null) getSupportActionBar().setTitle(title); } }
No comments:
Post a Comment