Reputation: 381
I'm new student on xamarin android. so I don't know how to create a event click on that.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_48dp"
android:title="Home" />
<item
android:id="@+id/nav_genre"
android:icon="@drawable/ic_toc_black_48dp"
android:title="Genres" />
<item
android:id="@+id/nav_audio"
android:icon="@drawable/ic_settings_input_antenna_black_48dp"
android:title="Audio" />
<item
android:id="@+id/nav_download"
android:icon="@drawable/ic_get_app_black_48dp"
android:title="Download" />
</group>
<item android:title="Account">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_lock_open_black_48dp"
android:title="About"/>
<item
android:id="@+id/nav_signout"
android:icon="@drawable/ic_perm_identity_black_48dp"
android:title="Sign out"/>
</group>
</menu>
</item>
</menu>
<!-- your content layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:titleTextColor="@android:color/background_light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<frameLayout
android:id:="@+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/nav_view"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/navmenu" />
</android.support.v4.widget.DrawerLayout>
I want to when click that item 1 -> fragmenthome(there only listview)
I want to when click that item 2 -> fragmentgenres(there only listview)
They all be showing up in frameContainer.
Upvotes: 3
Views: 1081
Reputation: 2434
This is how to handle click events and load fragments accordingly.
HomeFragment homFragment;
GenresFragment genresFragment;
int currentFragmentId=Resource.Id.nav_home;
Above declarations are to be made global in the Activity.
var navigationView = FindViewById<NavigationView> (Resource.Id.nav_view);
navigationView.NavigationItemSelected+= NavigationView_NavigationItemSelected;
CreateFragments ();
LoadInditialFragment ();
Add the above snippets in OnCreate.
void CreateFragments()
{
homeFragment = new HomeFragment ();
genresFragment = new GenresFragment ();
}
void LoadInditialFragment()
{
var transaction = FragmentManager.BeginTransaction ();
transaction.Add (Resource.Id.frameContainer, genresFragment).Hide(genresFragment);
transaction.Add (Resource.Id.frameContainer, homeFragment);
transaction.Commit ();
}
void NavigationView_NavigationItemSelected (object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
if (e.MenuItem.ItemId != currentFragmentId)
SwitchFragment (e.MenuItem.ItemId);
drawerLayout.CloseDrawers ();
}
void SwitchFragment(int FragmentId)
{
var transaction = FragmentManager.BeginTransaction ();
switch (currentFragmentId)
{
case Resource.Id.nav_home:
transaction.Hide (homeFragment).Commit ();
break;
case Resource.Id.nav_genre:
transaction.Hide (genresFragment).Commit ();
break;
}
transaction = FragmentManager.BeginTransaction ();
switch (FragmentId)
{
case Resource.Id.nav_home:
transaction.Show (homeFragment);
transaction.Commit ();
break;
case Resource.Id.nav_genre:
transaction.Show (genresFragment);
transaction.Commit ();
break;
}
currentFragmentId = FragmentId;
}
In the Create Fragment Method All Fragments are instantiated initially and attached to the Fragment. Then all fragments except the fragment to be shown are hidden. Then as the user clicks on an item in NavigationView the current fragment is hidden and the fragment corresponding to the menu item is shown. In this approach each fragment will not be created each time user switches menu. Thus pages will load faster.
Upvotes: 2