本教程将介绍Android如何在可展开的列表视图中获取孩子的父母姓名的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
我已经创建了一个类似下面的3-level ExpandableListView
下面是我的代码
ThreeLevelListAdapter
public class ThreeLevelListAdapter extends BaseExpandableListAdapter {
String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;
/**
* Constructor
* @param context
* @param parentHeader
* @param secondLevel
* @param data
*/
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
this.context = context;
this.parentHeaders = parentHeader;
this.secondLevel = secondLevel;
this.data = data;
}
@Override
public int getGroupCount() {
return parentHeaders.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// no idea why this code is working
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int group, int child) {
return child;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
text.setText(this.parentHeaders[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);
String[] headers = secondLevel.get(groupPosition);
List<String[]> childData = new ArrayList<>();
HashMap<String, String[]> secondLevelData = data.get(groupPosition);
for (String key : secondLevelData.keySet()) {
childData.add(secondLevelData.get(key));
}
secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));
secondLevelELV.setGroupIndicator(null);
secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
secondLevelELV.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
return secondLevelELV;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Second LevelAdapter
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;
List<String[]> data;
String[] headers;
ImageView ivGroupIndicator;
public SecondLevelAdapter(Context context, String[] headers, List<String[]> data) {
this.context = context;
this.data = data;
this.headers = headers;
}
@Override
public Object getGroup(int groupPosition) {
return headers[groupPosition];
}
@Override
public int getGroupCount() {
return headers.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_second, null);
TextView text = (TextView) convertView.findViewById(R.id.rowSecondText);
String groupText = getGroup(groupPosition).toString();
text.setText(groupText);
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
String[] childData;
childData = data.get(groupPosition);
return childData[childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_third, null);
TextView textView = convertView.findViewById(R.id.rowThirdText);
textView.setOnClickListener(v -> {
// here I want to get the all parent names of the child clicked
Common.showToast(context,"This Feature is under development", Toast.LENGTH_LONG);
});
String[] childArray = data.get(groupPosition);
String text = childArray[childPosition];
textView.setText(text);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
String[] children = data.get(groupPosition);
return children.length;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我想做什么?
我想在单击子对象时获取所有父对象名称。例如,如上图所示。如果我单击M/S PAK MEDICOSE PHARMACY
或M/S SERVAID PHARMACY
,则希望获取它们的父级名称LICE-O-NIL CREAM
和1112 STOP TOWNSHIP
,依此类推。
TextView textView = convertView.findViewById(R.id.rowThirdText);
textView.setOnClickListener(v -> {
//Here I want to get the names of parent
Common.showToast(context,"This Feature is under development", Toast.LENGTH_LONG);
});
怎么实现此目标?
如有任何帮助,我们将不胜感激。
推荐答案
第1步:您已经有了在回收器视图中显示的元素列表。现在创建这些列表的列表,好吗(我稍后会告诉您为什么?)
步骤2:您可以做的是创建另一个全局列表(如果经常使用,也是静态的)。提示:如果适配器在其他类中,则通过构造函数传递列表。
说明:
该列表将保存被点击项目的索引(我假设我们不知道回收器视图中有多少子列表)。当用户单击第一个回收者视图列表中的项目时,我们将通过(~Adapter.getAdapterPosition)保存索引
()然后用户单击子列表,然后我们也保存该索引,依此类推…
现在,假设用户像这样点击:
来自列表1-&>索引:4
来自列表2-&>索引:2
来自列表3-&>索引:3
所以我们现在要做的是,我们有一个包含一个索引的列表和另一个包含n个列表(其中n=子列表数)的列表。ListsArrayList包含按顺序排列的列表,indexClickedList包含我们单击的元素的索引,indexClickedList中的索引是我们从中单击项目的列表的编号,因此您可以从listsList访问该列表。通过这种方式,您可以获得我们所单击的元素的父项。在这里,您必须使用While循环,因为我们不知道有多少子列表。请问继续操作,直到我们到达list.get为止。
当我们有分支时,我们总是使用树结构,在第一条评论中,我以为你只有一个子列表,但现在你有多个子列表。所以你一定要使用树形结构。进行循环时要小心,可能会出现空指针异常。
好了关于Android怎么在可展开的列表视图中获取孩子的父母姓名的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。