- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在寻找实现具有可扩展 ListView 的 fragment 。搜索后,我发现了一个在 https://gist.github.com/1316903 上实现可扩展 ListView fragment 的类.但我不知道如何使用它。请帮我。我尝试了列表 fragment ,但我不知道如何使用具有可扩展 ListView 的 fragment ,在此先感谢。
最佳答案
这是我在 MonoDroid/C# 中得到的。我不得不剥离一些代码(为了保密),但它应该大部分是完整的。
此实现可以支持不同的 subview ,其中每个组都包含一个特定的子类型,该子类型在整个组中都是相同的。但是,您可以在一个组中混合使用子类型,甚至可以有不同的组类型(这只会真正改变组标题;我对两个组使用相同的组类型,并使用组的位置来确定子-类型 - 因此 group[0] 包含 ExpandableListChild1 的子级和 ExpandableListChild2 的 group[1] - 预期用途)。
此处的子类型仅在背景颜色上有所不同(为简单起见),但是这些 View 可以是您需要的任何 View ,包括自定义 View 。只需为您需要的任何 View 制作相应的 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 可扩展 ListView fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274392/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4 个月前关闭。 Improve
我已经为这个概念苦苦挣扎了一段时间。我正在尝试提出一种松散耦合的 Azure 组件设计,该设计可以使用队列和辅助角色完全可扩展,从而使项目出列并处理。我可以随意扩展工作角色,并且发布到队列从来都不是问
尝试在 android 中创建可扩展 ListView 。我希望每个组都扩展到一个预设的 XML 布局文件。例如。 A组{ 展开时显示 XML 文件 A:按钮、搜索栏、微调器等 } 乙组{ 展开时显示
在 android 可扩展 ListView 中,我需要显示子项,仅在按下展开折叠节点时..即;我不想让 children 按下可扩展 ListView 的行。我该如何完成? 问候,罗尼 最佳答案 我
我正在开发一个Android代码编辑器,但是当我对它应用语法突出显示时,它会变得很滞后,是否有任何方法可以对其进行优化? 这是我的代码: protected void onCreate(Bundle
我正在开发安卓应用程序。我发现下面的输出图像显示了以下问题。请帮我解决我的问题。 Xml file 输出截图- 最佳答案 使用 setIndicatorBounds。 开发链接:http://deve
最近我在尝试使用 android 的可扩展 ListView ,所以我四处搜索并偶然发现了这个 http://about-android.blogspot.com/2010/04/steps-to-i
在 Control.Exception 的文档中在 base 4.4.0.0 中有一个如何创建异常层次结构的示例。该示例展示了如何通过根据父异常声明 Exception 类的实例来捕获特定异常的概括。
我正在开发一个将显示一些可扩展列表的应用程序。事实上,有时该对象不会有任何子对象。所以我想在我的可扩展列表中仅显示具有子元素的元素。我尝试在 getGroupView 函数中放入 if ,如果该对象没
这是一个有点开放式的问题,但是,制作良好的可扩展 Electron 应用程序的好方法是什么? VSCode、Atom 和许多其他工具都支持扩展,但它们的代码库太大,我无法理解发生了什么。我对 Jupy
我和一些 friend 为 Facebook 编写了一款游戏,但没有过多考虑游戏的实际架构 - 想象一下当我们每月吸引超过 300,000 名独立玩家并且我们的服务器崩溃时我们的惊喜。 现在我们正在努
您好,我正在使用 Android Expandable listview 并在其中用不同的 View 膨胀 child 。我遇到的问题是,当我展开一个 View 然后打开另一个父 View 时,布局中
我的场景是,我有一个可扩展 ListView ,在列表中动态添加我和 child 。我的子布局就是这样。 当我点击相应项目的编辑按钮时,我打开对话框从用户那里获取输入,然后点击更新相应的详细信息更新到
我想使用 RecyclerView 创建项目列表,并希望在单击时展开特定项目(如电话列表)。我想在不使用任何库的情况下实现这一目标。谁能帮忙? 最佳答案 获取子数据列表作为数据集中父数据的成员。并且,
我使用 ExpandableListview ... 我能够将从 Web 服务检索到的值设置为子布局的单个 Textview。现在需要在 Web 服务的子布局中设置两个不同的 Textviews 值我
我的可扩展 ListView 的顺序似乎不正确。 这是我的数据提供者: public class DataProvider { public static HashMap> getInfo() {
所以我试图在我的可扩展列表的子项列表中创建一行,但它不起作用,这是我的布局:
我有一个需要自定义箭头的可扩展 ListView 。我尝试将 groupIndicator 设置为这样的选择器: 但是,出于某种原因,这会扭曲箭头的尺寸,请参见下文: 知道它们为
我正在尝试实现具有 2 种不同布局的 ExpandableListView,到目前为止,我花了一段时间才真正让 1 个 View 组使用一个布局,而其他 2 个 View 组使用另一个。到目前为止,外
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我是一名优秀的程序员,十分优秀!