Android可扩展列表视图片段
我正在搜索使用可扩展列表视图实现片段。搜索之后,我发现一个类在上实现了可扩展的listview片段。但我不知道如何使用它。请帮帮我。我尝试了列表片段,但我不知道如何使用带有可扩展列表视图的片段,这要提前感谢。Android在ApiDemos项目中收集了大量工作代码示例。您可以在eclipse中以新的->Android示例项目的形式获得它。ApiDemos有一个很好的可扩展列表示例。在使用更复杂、更高级的示例之前,这是一个非常好的起点。以下是我在MonoDroid/C中得到的内容。我不得不去掉一些代码(为了保密),但它应该基本上是完整的 此实现可以支持不同的子视图,其中每个组都包含一个特定的子类型,该子类型在该组中是相同的。但是,您可以在一个组中混合子类型,甚至可以有不同的组类型(这实际上只是更改了组标题;我对两个组使用相同的组类型,并使用组的位置来确定子类型-因此组[0]包含ExpandableListChild1和组[1]的子级)ExpandableListChild2的定义-按预期用途) 这里的子类型仅因其背景颜色不同(为了简单起见),但是这些视图可以是您需要的任何视图,包括自定义视图。只需为您需要的任何视图创建相应的ExpandListChild子类。此外,基本类ExpandListChildAbs可以是任何您需要的,以适合您的应用程序。如果只有一个子类型,则不需要基类,但是如果有两个或更多子类型,则需要某种基类,这两个子类型都可以从中生成子类,以支持BaseExpandableListAdapter方法getChild和相关方法中的多态性Android可扩展列表视图片段,android,fragment,android-listfragment,Android,Fragment,Android Listfragment,我正在搜索使用可扩展列表视图实现片段。搜索之后,我发现一个类在上实现了可扩展的listview片段。但我不知道如何使用它。请帮帮我。我尝试了列表片段,但我不知道如何使用带有可扩展列表视图的片段,这要提前感谢。Android在ApiDemos项目中收集了大量工作代码示例。您可以在eclipse中以新的->Android示例项目的形式获得它。ApiDemos有一个很好的可扩展列表示例。在使用更复杂、更高级的示例之前,这是一个非常好的起点。以下是我在MonoDroid/C中得到的内容。我不得不去掉一些
[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;
}
//________________________________________________________________________________________________________
}
}
[活动](
Label=“片段中的ExpandableListView”,
Theme=“@android:style/Theme.NoTitleBar”,
MainLauncher=false,
ConfigurationChanges=ConfigChanges.KeyboardHidden,
WindowsofInputMode=SoftInput.AdjustPan | SoftInput.StateHidden)]
公共类片段1:片段
{
私有视图组_thisView;
私有包_Bundle;
私人充气机;
私有视图组_容器;
创建时公共覆盖无效(捆绑包)
{
base.OnCreate(bundle);
_束=束;
_模型=模型;
}
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包)
{
base.OnCreateView(充气机、容器、包裹);
_充气机=充气机;
_容器=容器;
_束=束;
Render();
返回此视图;
}
公共覆盖无效转速表(活动)
{
碱转氨酶(活性);
_dialogListener=(IDialogWindow)活动;
}
//公共上下文LocalContext{get;set;}
已创建ActivityState(Bundle savedInstanceState)上的公共覆盖无效
{
创建的基本活动(savedInstanceState);
}
创建视图时的公共覆盖无效(视图、捆绑包保存状态)
{
base.OnViewCreated(视图,savedInstanceState);
}
公共覆盖无效呈现()
{
_thisView=(视图组)\ u充气器.Inflate(Resource.Layout.MainLayout,\ u容器,false);
ExpandableListView elvParcelInfo=\u thisView.FindViewById(Resource.Id.elv\u parcel\u info);
List expandListItems=设置标准组();
ExpandListAdapter ExpandListAdapter=新的ExpandListAdapter(Activity.ApplicationContext,expandListItems);
elvParcelInfo.SetAdapter(expandListAdapter);
}
公共列表集合标准组()
{
List childern1=新列表();
对于(int i=0;i<20;i++)
{
ExpandListChild1 child1=新的ExpandListChild1();
child1.Name=“child1#”+i.ToString();
child1.Tag=null;
childern1.添加(child1);
}
ExpandListGroup group1=新的ExpandListGroup();
group1.Name=“喜剧”;
//组1。项目=子项目1;
group1.Items=新列表();
foreach(在childern1中展开列表child1 child1)
{
第1组。项目。添加(儿童1);
}
/////////////////////////////////////////////////////////////
List childern2=新列表();
对于(int i=0;i<20;i++)
{
ExpandListChild2 child2=新的ExpandListChild2();
child2.Name=“child2#”+i.ToString();
child2.Tag=null;
childern2.添加(child2);
}
ExpandListGroup组2=新的ExpandListGroup();
group2.Name=“Action”;
//组2。项目=儿童2;
group2.Items=新列表();
foreach(在childern2中展开列表child2 child2)
{
组2。项目。添加(儿童2);
}
/////////////////////////////////////////////////////////////
列表组=新列表();
添加(第1组);
添加(第2组);
返回组;
}
公共抽象类expandListChildLabs:Java.Lang.Object
{
公共抽象字符串名称{get;set;}
公共抽象字符串标记{get;set;}
}
公共类ExpandListChild1:ExpandListChildAbs
{
公共重写字符串名称{get;set;}
公共重写字符串标记{get;set;}
}
公共类ExpandListChild2:ExpandListChildAbs
{
公共重写字符串名称{get;set;}
公共重写字符串标记{get;set;}
}
公共类ExpandListGroup:Java.Lang.Object
{
公共字符串名称{get;set;}
公共列表项{get;set;}
}
公共类ExpandListAdapter:BaseExpandableListAdapter
{
私有枚举子类型{ChildType1,ChildType2}
私人语境;
私人名单组;
<?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>
<?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>
<?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>
<?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>