Android可扩展列表视图片段

Android可扩展列表视图片段,第1张

概述我正在寻找实现具有可扩展列表视图的片段.搜索后,我发现了一个在https://gist.github.com/1316903上实现可扩展listview片段的类.但是我不知道如何使用它.请帮我.我尝试了列表片段,但我不知道先使用带有可扩展列表视图的片段,谢谢.解决方法:这是我在MonoDroid/C#中得到的.我不得

我正在寻找实现具有可扩展列表视图的片段.搜索后,我发现了一个在https://gist.github.com/1316903上实现可扩展ListvIEw片段的类.但是我不知道如何使用它.请帮我.我尝试了列表片段,但我不知道先使用带有可扩展列表视图的片段,谢谢.

解决方法:

这是我在MonoDroID / C#中得到的.我不得不删除一些代码(出于保密目的),但是它应该基本完整.

此实现可以支持不同的子视图,其中每个组包含一个特定的子类型,该子类型在整个组中都是相同的.但是,您可以在组中混合使用子类型,甚至可以使用不同的组类型(这实际上只是更改了组标题;我对两个组都使用相同的组类型,并使用组的位置来确定子类型-类型-因此group [0]包含ExpandableListChild1的子级和ExpandableListChild2的group [1](按预期用途).

这里的子类型仅因其背景颜色不同(为简单起见),但是这些视图可以是您需要的任何视图,包括自定义视图.只需为所需的任何视图创建一个对应的ExpandListChild子类.同样,基类ExpandListChildAbs可以是适合您的应用程序所需的任何类.如果只有一个子类型,则不需要基类;但是,如果有两个或多个子类型,则需要某种基类,这两个子类型都可以作为子类来支持Baseexpandablelistadapter方法getChild和相关方法中的多态性. .

[Activity(    Label = "ExpandableListVIEw in a Fragment",     theme = "@androID:style/theme.NoTitlebar",     MainLauncher = false,    ConfigurationChanges = ConfigChanges.KeyboardHIDden,    windowsoftinputMode = Softinput.AdjustPan | Softinput.StateHIDden)] public class Fragment1 : Fragment{    private VIEwGroup _thisVIEw;           private Bundle _bundle;    private LayoutInflater _inflater;    private VIEwGroup _container;    public overrIDe voID OnCreate(Bundle bundle)    {        base.OnCreate(bundle);                  _bundle = bundle;        _model = Model;    }    public overrIDe VIEw OnCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle bundle)    {        base.OnCreateVIEw(inflater, container, bundle);        _inflater = inflater;        _container = container;        _bundle = bundle;         Render();        return _thisVIEw;    }            public overrIDe voID OnAttach(Activity activity)    {        base.OnAttach(activity);        _dialogListener = (IDialogWindow)activity;    }                    //public Context LocalContext { get; set; }    public overrIDe voID OnActivityCreated(Bundle savedInstanceState)    {        base.OnActivityCreated(savedInstanceState);                }    public overrIDe voID OnVIEwCreated(VIEw vIEw, Bundle savedInstanceState)    {        base.OnVIEwCreated(vIEw, savedInstanceState);                }    public overrIDe voID Render()    {           _thisVIEw = (VIEwGroup)_inflater.Inflate(Resource.Layout.MainLayout, _container, false);        ExpandableListVIEw elvParcelinfo = _thisVIEw.FindVIEwByID<ExpandableListVIEw>(Resource.ID.elv_parcel_info);        List<ExpandListGroup> expandListItems = SetStandardGroups();        Expandlistadapter expandlistadapter = new Expandlistadapter(Activity.ApplicationContext, expandListItems);        elvParcelinfo.SetAdapter(expandlistadapter);    }    public List<ExpandListGroup> SetStandardGroups()    {        List<ExpandListChild1> childern1 = new List<ExpandListChild1>();        for (int i = 0; i < 20; i++)        {            ExpandListChild1 child1 = new ExpandListChild1();            child1.name = "child1 #" + i.ToString();            child1.Tag = null;            childern1.Add(child1);        }        ExpandListGroup group1 = new ExpandListGroup();        group1.name = "Comedy";        //group1.Items = childern1;        group1.Items = new List<ExpandListChildAbs>();        foreach (ExpandListChild1 child1 in childern1)        {            group1.Items.Add(child1);        }        /////////////////////////////////////////////////////////////        List<ExpandListChild2> childern2 = new List<ExpandListChild2>();        for (int i = 0; i < 20; i++)        {            ExpandListChild2 child2 = new ExpandListChild2();            child2.name = "child2 #" + i.ToString();            child2.Tag = null;            childern2.Add(child2);        }        ExpandListGroup group2 = new ExpandListGroup();        group2.name = "Action";        //group2.Items = childern2;        group2.Items = new List<ExpandListChildAbs>();        foreach (ExpandListChild2 child2 in childern2)        {            group2.Items.Add(child2);        }        /////////////////////////////////////////////////////////////        List<ExpandListGroup> groups = new List<ExpandListGroup>();        groups.Add(group1);        groups.Add(group2);        return groups;    }    public abstract class ExpandListChildAbs : java.lang.Object    {        public abstract String name { get; set; }        public abstract String Tag { get; set; }    }    public class ExpandListChild1 : ExpandListChildAbs    {        public overrIDe String name { get; set; }        public overrIDe String Tag { get; set; }    }    public class ExpandListChild2 : ExpandListChildAbs    {        public overrIDe String name { get; set; }        public overrIDe String Tag { get; set; }    }    public class ExpandListGroup : java.lang.Object    {        public String name { get; set; }        public List<ExpandListChildAbs> Items { get; set; }    }    public class Expandlistadapter : Baseexpandablelistadapter    {        private enum ChildTypes { ChildType1, ChildType2 }        private Context context;        private List<ExpandListGroup> groups;        public Expandlistadapter(Context context, List<ExpandListGroup> groups)         {            this.context = context;            this.groups = groups;        }        public voID AddItem(ExpandListChildAbs item, ExpandListGroup group)         {                                          if (!groups.Contains(group))             {                groups.Add(group);            }            int index = groups.IndexOf(group);            List<ExpandListChildAbs> ch = groups[index].Items;            ch.Add(item);            groups[index].Items = ch;        }        public overrIDe bool HasStableIDs        {            get { return true; }        }        public overrIDe bool IsChildSelectable(int arg0, int arg1)        {            return true;        }        //______________________________________________________________________________________________________        // Get Child Methods        //______________________________________________________________________________________________________        public overrIDe long GetChildID(int groupposition, int childposition)        {            return childposition;                        }        public overrIDe int GetChildrenCount(int groupposition)        {            List<ExpandListChildAbs> chList = groups[groupposition].Items;                           return chList.Count;        }        public overrIDe int ChildTypeCount        {            get { return Enum.Getnames(typeof(ChildTypes)).Length; }        }        public overrIDe int GetChildType(int groupposition, int childposition)        {               //return base.GetChildType(groupposition, childposition);            if (groupposition == 0)            {                return (int)ChildTypes.ChildType1;            }            if (groupposition == 1)            {                return (int)ChildTypes.ChildType2;            }            return 0;        }        public overrIDe java.lang.Object GetChild(int groupposition, int childposition)        {            List<ExpandListChildAbs> chList = groups[groupposition].Items;            return chList[childposition];        }        public overrIDe VIEw GetChildVIEw(int groupposition, int childposition, bool isLastChild, VIEw vIEw, VIEwGroup parent)        {            int ChildType = GetChildType(groupposition, childposition);            if (ChildType == (int)ChildTypes.ChildType1)            {                return GetChildVIEw_ChildType1(groupposition, childposition, isLastChild, vIEw, parent);            }            if (ChildType == (int)ChildTypes.ChildType2)            {                return GetChildVIEw_ChildType2(groupposition, childposition, isLastChild, vIEw, parent);            }            return null;        }        private VIEw GetChildVIEw_ChildType1(int groupposition, int childposition, bool isLastChild, VIEw vIEw, VIEwGroup parent)        {               ExpandListChild1 child1 = (ExpandListChild1)GetChild(groupposition, childposition);            if (vIEw == null)            {                LayoutInflater infalinflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);                vIEw = infalinflater.Inflate(Resource.Layout.ExpandList_ChildItem1, null);            }            TextVIEw tv = vIEw.FindVIEwByID<TextVIEw>(Resource.ID.tvChild1);            tv.Text = child1.name;            tv.Tag = child1.Tag;            return vIEw;        }        private VIEw GetChildVIEw_ChildType2(int groupposition, int childposition, bool isLastChild, VIEw vIEw, VIEwGroup parent)        {            ExpandListChild2 child2 = (ExpandListChild2)GetChild(groupposition, childposition);            if (vIEw == null)            {                LayoutInflater infalinflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);                vIEw = infalinflater.Inflate(Resource.Layout.ExpandList_ChildItem2, null);            }            TextVIEw tv = vIEw.FindVIEwByID<TextVIEw>(Resource.ID.tvChild2);            tv.Text = child2.name;            tv.Tag = child2.Tag;            return vIEw;        }        //________________________________________________________________________________________________________        //______________________________________________________________________________________________________        // Get Group Methods        //______________________________________________________________________________________________________        public overrIDe long GetGroupID(int groupposition)        {            return groupposition;        }        public overrIDe int GroupCount        {            get { return groups.Count; }        }        public overrIDe int GroupTypeCount         {             get { return base.GroupTypeCount; }         }        public overrIDe int GetGroupType(int groupposition)        {            return base.GetGroupType(groupposition);        }        public overrIDe java.lang.Object GetGroup(int groupposition)        {            return groups[groupposition];        }        public overrIDe VIEw GetGroupVIEw(int groupposition, bool isLastChild, VIEw vIEw, VIEwGroup parent)         {            ExpandListGroup group = (ExpandListGroup) GetGroup(groupposition);            if (vIEw == null)             {                LayoutInflater inf = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);                vIEw = inf.Inflate(Resource.Layout.ExpandList_GroupItem, null);            }            TextVIEw tv = vIEw.FindVIEwByID<TextVIEw>(Resource.ID.tvGroup);            tv.Text = group.name;            return vIEw;        }        //________________________________________________________________________________________________________    }      }

主布局

<?xml version="1.0" enCoding="utf-8"?><linearLayout  xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:ID="@+ID/ll_left_parcel"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:orIEntation="vertical"  androID:background="#FF8F8D8F">       <ExpandableListVIEw    androID:ID="@+ID/elv_parcel_info"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:background="#FFCFCDCF"    androID:groupIndicator="@null"/>  </linearLayout>

ExpandList_GroupItem

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="55dip"    androID:background="#FF00AA55"    androID:orIEntation="vertical" >  <TextVIEw      androID:ID="@+ID/tvGroup"      androID:layout_wIDth="fill_parent"      androID:layout_height="wrap_content"      androID:layout_marginRight="15dp"      androID:textcolor="#FF000000"      androID:textSize="17dip" /></linearLayout>

ExpandList_ChildItem1

<?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:background="#FF8800CC"    androID:orIEntation="vertical" >  <TextVIEw      androID:ID="@+ID/tvChild1"      androID:layout_wIDth="fill_parent"      androID:layout_height="wrap_content"      androID:textcolor="#FF000000"      androID:textSize="17dip" /></linearLayout>

ExpandList_ChildItem2

<?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:background="#FFAA00FF"    androID:orIEntation="vertical" >  <TextVIEw      androID:ID="@+ID/tvChild2"      androID:layout_wIDth="fill_parent"      androID:layout_height="wrap_content"      androID:textcolor="#FF000000"      androID:textSize="17dip" /></linearLayout>
总结

以上是内存溢出为你收集整理的Android可扩展列表视图片段全部内容,希望文章能够帮你解决Android可扩展列表视图片段所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1211042.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存