我需要以下问题的帮助:
有没有办法在“expendableListView”中添加一个按钮作为最后一个孩子?
该按钮应位于“短文本”字段下方。
如果您需要任何进一步的信息,请发表评论,我会提供。
我需要以下问题的帮助:
有没有办法在“expendableListView”中添加一个按钮作为最后一个孩子?
该按钮应位于“短文本”字段下方。
如果您需要任何进一步的信息,请发表评论,我会提供。
你ExpandableListView
应该得到一些数据模型的支持。在每个列表项行中,您将在视图(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信息”等])。现在,在您的活动中保留对此数据模型的引用:
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();
这将更新可扩展列表。但是,您需要为每一行指定您将拥有的视图类型,每个位置的视图类型,因为这与视图回收非常相关。因此,您的相关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;
}
有没有办法在“expendableListView”中添加一个按钮作为最后一个孩子?
我相信是的,您应该为所需组中的最后一个孩子膨胀另一个视图(包含按钮的视图)。
像这样的东西:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
if (convertView == null) {
if(groupPosition == FIRST_GROUP_POSITION && isLasChild){
convertView = inflater.inflate(R.layout.child_layout_with_buttons, null);
}else{
convertView = inflater.inflate(R.layout.normal_child_layout, null);
}
}
//....
}
首先,您需要实现自己的ExpandableListAdapter
,并实现getChildView
来控制每个子行的视图。尝试这个:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
if(isLastChild){
view = getLayoutInflater().inflate(R.layout.item_with_button, null);
}else{
view = getLayoutInflater().inflate(R.layout.item, null);
}
}
return view;
}
希望这可以帮助。