gpt4 book ai didi

android - 将按钮作为最后一个子项添加到可扩展 ListView

转载 作者:搜寻专家 更新时间:2023-11-01 08:03:09 25 4
gpt4 key购买 nike

我需要以下问题的帮助:

有没有办法在“expendableListView”中添加一个按钮作为最后一个子项?

enter image description here

该按钮应位于“短文本”字段下方。

如果您需要任何进一步的信息,请发表评论,我会提供。

最佳答案

你的 ExpandableListView应该由一些数据模型支持。在每个列表项行中,您将在 View (TextView 或 Button)中显示一个文本。所以首先让自己成为一个包含这些信息的类:

public class RowData {
public static final int TYPE_TEXT = 0;
public static final int TYPE_BUTTON = 1;
private String label;
private int type;

public RowData(String label, int type) {
this.label = label;
this.type = type;
}

public RowData(String label) {
this.label = label;
}

public String getLabel() {
return label;
}

public int getType() {
return type;
}

}

最简单的模型(因为我们不了解您的代码)是 LinkedHashMap<String, List<RowData>> .这些键指的是组数据(“00010”、“00020”、“00030”等),而附加到这些键的每个值指的是组子项(例如,对于“00010”,您将有 ["Material Group: No找到的信息”等])。现在,在您的 Activity 中保留对此数据模型的引用:

private Map<String, List<RowData>> myData = new LinkedHashMap<String, List<RowData>>();

您需要保留对 ExpandableListView 适配器的引用:

private BaseExpandableListAdapter adapter = new CustomSpecificAdapter(yourActivityContext, myData);

当您需要添加额外的字段时,您只需将其添加到数据模型对象中即可:

myData.get("00010").add(new RowData("Button value", RowData.TYPE_BUTTON));

TextView : myData.get("00010").add(new RowData("TextView value"));并通知适配器:

adapter.notifyDataSetChanged();

这将更新可扩展列表。但是,您需要为每一行指定您将拥有什么样的 View ,每个位置将具有什么类型,因为这与 View 回收非常相关。所以你的相关CustomSpecificAdapter方法可能类似于:public class CustomSpecificAdapter extends BaseExpandableListAdapter

private LinkedHashMap<String, List<RowData>> modelData;
private Context context;

public CustomSpecificAdapter(Context context, LinkedHashMap<String, List<RowData>> modelData) {
this.context = context;
this.modelData = modelData;
}

private RowData getMyDataObj(int groupPosition, int childPosition) {
/**
* safe to do this since we are using a LinkedHashMap
* */
List<List<RowData>> children = new ArrayList<List<RowData>>(modelData.values());
return children.get(groupPosition).get(childPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return getMyDataObj(groupPosition, childPosition);
}

@Override
public int getChildTypeCount() {
/**
* since you will have a button and a textview there will be 2 types of views
* */
return 2;
}

@Override
public int getChildType(int groupPosition, int childPosition) {
RowData data = getMyDataObj(groupPosition, childPosition);
if (data.getType() == RowData.TYPE_TEXT) {
return 0;
}
// for button
return 1;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
/**
* this is the intersting part
* */
RowData data = getMyDataObj(groupPosition, childPosition);
TextView myTv = null;
if(convertView == null) {
if(data.getType() == RowData.TYPE_BUTTON) {
convertView = // inflate from button layout, in fact it's best to have only a <Button ... /> in the XML layout
} else {
convertView = // inflate from TextView layout, in fact it's best to have only a <Button ... /> in the XML layout
}
}
/**
* Have the same id for both TextView and Button - Button extends from TextView and so setText() method is available through polimorphysm. Also no need for ViewHolder as getViewById() will refer to itself.
* */
myTv = (TextView) convertView.findViewById(the_same_id_in_both_xml_files);
myTv.setText(data.getLabel());
return convertView;
}

关于android - 将按钮作为最后一个子项添加到可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18181187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com