2016-09-09 5 views
1

ナビゲーションドロワーの[Expandable listview]の例またはC#コードを入力してください。 私はXamarin StudioでXamarin.Androidを使用しています。xamarin androidを使用してナビゲーションドロワーで拡張可能なリストビューを作成する方法

私はこのように次の図のように作成します。

navigation drawer with expandable listview

私は、Javaコードに慣れていないんです。おかげさまで

答えて

0

私はヘッダ項目にもアイコンがあるコードサンプルを作成しました。私が持っているMainActivity.csで

using Android.App; 
using Android.Widget; 
using Android.OS; 
using Android.Support.V7.App; 
using Toolbar = Android.Support.V7.Widget.Toolbar; 
using Android.Support.Design.Widget; 
using System; 
using Android.Support.V4.Widget; 
using System.Collections.Generic; 
using Android.Views; 

namespace NavigationDrawer_ExpandableListView 
{ 
    [Activity(Label = "NavigationDrawer_ExpandableListView", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/MyTheme")] 
    public class MainActivity : AppCompatActivity 
    { 
     private DrawerLayout drawerLayout; 
     ExpandableListAdapter menuAdapter; 
     ExpandableListView expandableList; 
     List<ExpandedMenuModel> listDataHeader; 
     Dictionary<ExpandedMenuModel, List<String>> listDataChild; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 


      SetContentView (Resource.Layout.Main); 
      var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar); 

      //Toolbar will now take on default actionbar characteristics 
      SetSupportActionBar(toolbar); 

      SupportActionBar.Title = "Hello from Appcompat Toolbar"; 

      ExpandableListView expandableList = FindViewById<ExpandableListView>(Resource.Id.navigationmenu); 
      NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view); 

      if (navigationView != null) 
      { 
       navigationView.NavigationItemSelected += OnNavigationItemSelected; 
      } 


      PrepareListData(); 
      menuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList); 

      // setting list adapter 
      expandableList.SetAdapter(menuAdapter); 

     } 

     private void PrepareListData() 
     { 
      listDataHeader = new List<ExpandedMenuModel>(); 
      listDataChild = new Dictionary<ExpandedMenuModel, List<String>>(); 

      ExpandedMenuModel item1 = new ExpandedMenuModel(); 
      item1.Name = "heading1"; 
      item1.Image = Resource.Drawable.abc_ic_menu_copy_mtrl_am_alpha; 
      // Adding data header 
      listDataHeader.Add(item1); 

      ExpandedMenuModel item2 = new ExpandedMenuModel(); 
      item2.Name = "heading2"; 
      item2.Image = Resource.Drawable.abc_ic_voice_search_api_material; 
      listDataHeader.Add(item2); 

      ExpandedMenuModel item3 = new ExpandedMenuModel(); 
      item3.Name = "heading3"; 
      item3.Image = Resource.Drawable.abc_ic_menu_share_mtrl_alpha; 
      listDataHeader.Add(item3); 

      ExpandedMenuModel item4 = new ExpandedMenuModel(); 
      item4.Name = "heading4"; 
      item4.Image = Resource.Drawable.abc_ic_menu_paste_mtrl_am_alpha; 
      listDataHeader.Add(item4); 

      // Adding child data 
      List<String> heading1 = new List<String>(); 
      heading1.Add("Submenu of item 1"); 

      List<String> heading2 = new List<String>(); 
      heading2.Add("Submenu of item 2"); 
      heading2.Add("Submenu of item 2"); 
      heading2.Add("Submenu of item 2"); 

      List<String> heading3 = new List<String>(); 
      heading3.Add("Submenu of item 3"); 
      heading3.Add("Submenu of item 3"); 

      List<String> heading4 = new List<String>(); 
      heading4.Add("Submenu of item 4"); 
      heading4.Add("Submenu of item 4"); 

      listDataChild.Add(listDataHeader[0], heading1);// Header, Child data 
      listDataChild.Add(listDataHeader[1], heading2); 
      listDataChild.Add(listDataHeader[2], heading3); 
      listDataChild.Add(listDataHeader[3], heading4); 
     } 

     public override bool OnOptionsItemSelected(IMenuItem item) 
     { 
      switch (item.ItemId) 
      { 
       case Android.Resource.Id.Home: 
        drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.Start); 
        return true; 
      } 
      return base.OnOptionsItemSelected(item); 
     } 

     private void OnNavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e) 
     { 

      var menuItem = e.MenuItem; 
      menuItem.SetChecked(!menuItem.IsChecked); 
      drawerLayout.CloseDrawers(); 
     } 

    } 
} 

ExpandableListAdapter.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Java.Lang; 

namespace NavigationDrawer_ExpandableListView 
{ 
    class ExpandableListAdapter : BaseExpandableListAdapter 
    { 
     private Activity _context; 
     private List<ExpandedMenuModel> _listDataHeader; // header titles 

     // child data in format of header title, child title 
     private Dictionary<ExpandedMenuModel, List<string>> _listDataChild; 
     ExpandableListView expandList; 

     public ExpandableListAdapter(Activity context, List<ExpandedMenuModel> listDataHeader, Dictionary<ExpandedMenuModel, List<string>> listChildData, ExpandableListView mView) 
     { 
      _context = context; 
      _listDataHeader = listDataHeader; 
      _listDataChild = listChildData; 
      expandList = mView; 
     } 
     public override int GroupCount 
     { 
      get 
      { 
       return _listDataHeader.Count; 
      } 
     } 

     public override bool HasStableIds 
     { 
      get 
      { 
       return false; 
      } 
     } 

     public override Java.Lang.Object GetChild(int groupPosition, int childPosition) 
     { 
      return _listDataChild[_listDataHeader[groupPosition]][childPosition]; 
     } 

     public override long GetChildId(int groupPosition, int childPosition) 
     { 
      return childPosition; 
     } 

     public override int GetChildrenCount(int groupPosition) 
     { 
      int childCount = _listDataChild[_listDataHeader[groupPosition]].Count; 
      return childCount; 
     } 

     public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent) 
     { 
      string childText = (string)GetChild(groupPosition, childPosition); 
      if (convertView == null) 
      { 
       convertView = _context.LayoutInflater.Inflate(Resource.Layout.list_submenu, null); 
      } 
      TextView txtListChild = (TextView)convertView.FindViewById(Resource.Id.submenu); 
      txtListChild.Text = childText; 
      return convertView; 
     } 

     public override Java.Lang.Object GetGroup(int groupPosition) 
     { 
      return new JavaObjectWrapper<ExpandedMenuModel>() { Obj = _listDataHeader[groupPosition] }; 
     } 

     public override long GetGroupId(int groupPosition) 
     { 
      return groupPosition; 
     } 

     public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent) 
     { 
      ExpandedMenuModel headerTitle = _listDataHeader[groupPosition]; 

      convertView = convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.list_header, null); 
      TextView lblListHeader = (TextView)convertView.FindViewById(Resource.Id.submenu); 
      ImageView headerIcon = (ImageView)convertView.FindViewById(Resource.Id.iconimage); 
      lblListHeader.Text = headerTitle.Name; 
      headerIcon.SetImageResource(headerTitle.Image); 

      return convertView; 
     } 

     public override bool IsChildSelectable(int groupPosition, int childPosition) 
     { 
      return true; 
     } 
     public class JavaObjectWrapper<T> : Java.Lang.Object 
     { 
      public T Obj { get; set; } 
     } 

    } 
} 

ExpandedMenuModel.cs(あなたは単なる文字列を使用することができない場合は、ヘッダ上のアイコンを使用する場合モデルの代わりにリスト)

namespace NavigationDrawer_ExpandableListView 
{ 
    public class ExpandedMenuModel 
    { 
     public ExpandedMenuModel() 
     { 

     } 
     public string Name { get; set; } 
     public int Image { get; set; } 
    } 
} 

Main.axml(基本的には、モデルの代わりにExpandableListView IDE NavigationView)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" /> 

    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true"> 

    <ExpandableListView 
     android:id="@+id/navigationmenu" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white" 
     android:layout_marginTop="192dp"> 
    </ExpandableListView> 
    </android.support.design.widget.NavigationView> 
</android.support.v4.widget.DrawerLayout> 

list_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="20dp" 
     android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/iconimage" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:paddingTop="10dp"/> 

    <TextView 
     android:id="@+id/submenu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:textColor="#000000" 
     android:textSize="20sp"/> 

    </LinearLayout> 

</LinearLayout> 

list_submenu.xmlここ

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <TextView 
     android:id="@+id/submenu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:padding="10dp" 
     android:textColor="#000000" 
     android:textSize="18sp"/> 
</LinearLayout> 

は、それがどのように見えるかです:

enter image description here

GitHubからプロジェクトをダウンロードすることもできます:https://github.com/Florin-Birgu/NavigationDrawer-ExpandableListView-Xamarin.Android

関連する問題