如何将不同的自定义子项放入可扩展的列表视图项?

时间:2016-04-19 10:56:20

标签: android xamarin mono xamarin.android

在可扩展列表视图中,我有三个包含子视图的标题,我想将自定义子视图放在每个标题中,就像在一个子视图中一样,我想放置图像的网格视图,而在其他子视图中,我想放置自定义搜索栏和单选按钮和列表视图等,如何将主要活动中的不同数据传递给每个子视图以及如何处理子项的单击事件?

Till now my progress 

1.Expandable List view

 <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffe0e0e0"
            android:layout_weight="0.6">
            <ExpandableListView
                android:id="@+id/lvExp"
                android:layout_height="wrap_content"
                android:layout_width="match_parent" />
        </LinearLayout>

2.自定义标题名称

<?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="55dip"
    android:orientation="vertical">
    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text="test"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:textColor="#ff009992" />
</LinearLayout>

3.grid view

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center" />

4.ExpLSt适配器

using System;
using Android.Widget;
using System.Collections.Generic;
using Android.App;
using Android.Views;

namespace NEC
{
    public class ExpandableListAdapter :BaseExpandableListAdapter
    {
        private Activity _context;
        private List<string> _listDataHeader; // header titles
        // child data in format of header title, child title
        private Dictionary<string, List<string>> _listDataChild;
        ImageAdapter imageAdaptor;


        public ExpandableListAdapter(Activity context, List<string> listDataHeader,Dictionary<String, List<string>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
        }

        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 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.ListItemCustomLayout, null); 
//          }
//          TextView  txtListChild = (TextView) convertView.FindViewById(Resource.Id.lblListItem);
//          txtListChild.Text=childText;


            if(groupPosition == 0) {
                convertView = View.Inflate(_context,Resource.Layout.expandable_Child_Layout, null);
                GridView grid = (GridView)convertView.FindViewById(Resource.Id.gridview);
                grid.SetAdapter(new ImageAdapter(this._context));

            }
            if(groupPosition == 1) {
                convertView = View.Inflate(_context, Resource.Layout.expandable_child_1_layout, null);
                TextView txtView = (TextView) convertView.FindViewById(Resource.Id.lblListItem);
                txtView.Text="Test";
                txtView.TextSize=15f;
            }
            if(groupPosition == 2) {
                convertView = View.Inflate(_context, Resource.Layout.expandable_child_2_layout, null);
                TextView txtView = (TextView) convertView.FindViewById(Resource.Id.lblListItem);
                txtView.Text="Test";
                txtView.TextSize=15f;
            }

            convertView.Invalidate();

            return convertView;
        } 
        public override int GetChildrenCount (int groupPosition)
        { 
            return _listDataChild [_listDataHeader [groupPosition]].Count; 
        } 
        public override Java.Lang.Object GetGroup (int groupPosition)
        {
            return _listDataHeader[groupPosition];
        }  
        public override int GroupCount {
            get {
                return _listDataHeader.Count;
            }
        }  
        public override long GetGroupId (int groupPosition)
        {
            return groupPosition;
        } 
        public override View GetGroupView (int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            string headerTitle = (string) GetGroup(groupPosition);
            if (convertView == null) { 
                convertView =_context.LayoutInflater.Inflate(Resource.Layout.HeaderCustomLayout,null); //infalInflater.inflate(R.layout.list_group, null);
            }

            TextView lblListHeader = (TextView)convertView.FindViewById (Resource.Id.lblListHeader); 
            lblListHeader.SetTypeface(null, Android.Graphics.TypefaceStyle.Bold);
            lblListHeader.Text=headerTitle;

            return convertView;
        } 
        public override bool HasStableIds {
            get {
                return false;
            }
        } 
        public override bool IsChildSelectable (int groupPosition, int childPosition)
        {
            return true;
        } 

        class ViewHolderItem :Java.Lang.Object
        {
        }
    }
}

5.ImageAdapter

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;

namespace NEC
{
    [Activity (Label = "ImageAdaptor")]         
    public class ImageAdapter : BaseAdapter
    {
        Context context;

        public ImageAdapter (Context c)
        {
            context = c;
        }

        public override int Count {
            get { return thumbIds.Length; }
        }

        public override Java.Lang.Object GetItem (int position)
        {
            return null;
        }

        public override long GetItemId (int position)
        {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public override View GetView (int position, View convertView, ViewGroup parent)
        {
            ImageView imageView;

            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView (context);
                imageView.LayoutParameters = new GridView.LayoutParams (85, 85);
                imageView.SetScaleType (ImageView.ScaleType.CenterCrop);
                imageView.SetPadding (8, 8, 8, 8);
            } else {
                imageView = (ImageView)convertView;
            }

            imageView.SetImageResource (thumbIds[position]);
            return imageView;
        }

        // references to our images
        int[] thumbIds = {
            Resource.Drawable.sample_2, Resource.Drawable.sample_3,
            Resource.Drawable.sample_4, Resource.Drawable.sample_5,
            Resource.Drawable.sample_6, Resource.Drawable.sample_7,
            Resource.Drawable.sample_0, Resource.Drawable.sample_1,
            Resource.Drawable.sample_2, Resource.Drawable.sample_3,
            Resource.Drawable.sample_4, Resource.Drawable.sample_5,
            Resource.Drawable.sample_6, Resource.Drawable.sample_7,
            Resource.Drawable.sample_0, Resource.Drawable.sample_1,
            Resource.Drawable.sample_2, Resource.Drawable.sample_3,
            Resource.Drawable.sample_4, Resource.Drawable.sample_5,
            Resource.Drawable.sample_6, Resource.Drawable.sample_7
        };
    }
}

6.主要活动

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using toolbar = Android.Support.V7.Widget.Toolbar;

using System.Collections.Generic;

namespace NEC
{
    [Activity(Label = "LedgerHome",Theme="@style/AppTheme",ScreenOrientation= Android.Content.PM.ScreenOrientation.Landscape)]
    public class Ledger_HomeActivity : AppCompatActivity
    {
        toolbar mToolbar;
        ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<string> listDataHeader;
        Dictionary<string, List<string>> listDataChild;

        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);
            this.Window.AddFlags (WindowManagerFlags.Fullscreen);


            SetContentView(Resource.Layout.Ledger_Home);

            mToolbar = FindViewById<toolbar>(Resource.Id.app_bar);
            SetSupportActionBar(mToolbar);
            SupportActionBar.Title = "";
            SupportActionBar.SetLogo(Resource.Drawable.TopLeftr_necLogo);
            expListView = FindViewById<ExpandableListView>(Resource.Id.lvExp);

            // Prepare list data
            FnGetListData();

            //Bind list
            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 
            expListView.SetAdapter (listAdapter); 

            FnClickEvents();


        }
        void FnClickEvents()
        { 
            expListView.ChildClick+= delegate(object sender, ExpandableListView.ChildClickEventArgs e) {
                Toast.MakeText(this,"child clicked",ToastLength.Short).Show();
            };
            expListView.GroupExpand += delegate(object sender, ExpandableListView.GroupExpandEventArgs e) {
                Toast.MakeText(this,"group expanded",ToastLength.Short).Show();
            };
            expListView.GroupCollapse+= delegate(object sender, ExpandableListView.GroupCollapseEventArgs e) {
                Toast.MakeText(this,"group collapsed",ToastLength.Short).Show();
            }; 

        }
        void FnGetListData() {
            listDataHeader = new List<string>();
            listDataChild = new Dictionary<string, List<string>>();

            // Adding child data
            listDataHeader.Add("QUICK ITEMS");
            listDataHeader.Add("PRODUCT LOOKUP");
            listDataHeader.Add("NON MARCHANDISE");

            // Adding child data
            List<string> lstCS = new List<string>();
            lstCS.Add("Field Theory");
            lstCS.Add("Field Theory");
            lstCS.Add("Field Theory");
            lstCS.Add("Field Theory");
            lstCS.Add("Field Theory");
            lstCS.Add("Field Theory");
            lstCS.Add("Field Theory");

            List<string> lstEC = new List<string>();
            lstEC.Add("Field Theory");
            lstEC.Add("Logic Design");
            lstEC.Add("Analog electronics");
            lstEC.Add("Network analysis");
            lstEC.Add("Micro controller");
            lstEC.Add("Signals and system");

            List<string> lstMech = new List<string>();
            lstMech.Add("Instrumentation technology");
            lstMech.Add("Dynamics of machinnes");
            lstMech.Add("Energy engineering");
            lstMech.Add("Design of machine");
            lstMech.Add("Turbo machine");
            lstMech.Add("Energy conversion");

            listDataChild.Add(listDataHeader[0], lstCS); // Header, Child data
            listDataChild.Add(listDataHeader[1], lstEC);
            listDataChild.Add(listDataHeader[2], lstMech);
        }

//        public override bool OnCreateOptionsMenu(IMenu menu)
//        {
//            MenuInflater.Inflate(Resource.Menu.action_menu, menu);
//          if (menu != null) { 
//              menu.FindItem (Resource.Id.action_show).SetVisible (true); 
//              menu.FindItem (Resource.Id.action_attach).SetVisible (false);
//          }
//
//            return base.OnCreateOptionsMenu(menu);
//        }
//      public override bool OnOptionsItemSelected (IMenuItem item)
//      {
//          switch (item.ItemId)
//          {
//          case Android.Resource.Id.Home:
//              //this.Activity.Finish();
//              return true; 
//          case Resource.Id.action_show:
//              //FnAttachImage();
//              FnAlertDailog ();
//              return true;
//          default:
//              return base.OnOptionsItemSelected(item);
//          }
//      }
    }
}

0 个答案:

没有答案