事件单击android.support.design.widget.NavigationView上的项目

时间:2016-04-15 02:05:49

标签: android xamarin xamarin.android

我是xamarin android的新生。所以我不知道如何创建一个事件点击它。



<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>
&#13;
&#13;
&#13;

&#13;
&#13;
<!-- 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>
&#13;
&#13;
&#13;

我想点击该项目1 - &gt; fragmenthome(只有listview)

我想在点击该项目时2 - &gt; fragmentgenres(只有listview)

它们都出现在frameContainer中。

1 个答案:

答案 0 :(得分:2)

这是处理点击事件和相应加载片段的方法。

HomeFragment homFragment;
GenresFragment genresFragment;
int currentFragmentId=Resource.Id.nav_home;

以上声明将在活动中全局化。

var navigationView = FindViewById<NavigationView> (Resource.Id.nav_view);
navigationView.NavigationItemSelected+= NavigationView_NavigationItemSelected;
CreateFragments ();
LoadInditialFragment ();

在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;
    }

在创建片段方法中,所有片段最初都被实例化并附加到片段。然后隐藏除了要显示的片段之外的所有片段。然后,当用户点击NavigationView中的项目时,隐藏当前片段并显示与菜单项对应的片段。在这种方法中,每次用户切换菜单时都不会创建每个片段。因此页面加载速度更快。