将按钮添加到可扩展列表视图作为最后一个子项

时间:2013-08-12 07:00:48

标签: android button expandablelistview

我在以下问题上需要帮助:

有没有办法在'expendableListView'中添加一个按钮作为最后一个孩子?

enter image description here

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

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

3 个答案:

答案 0 :(得分:2)

您的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”,您将拥有[“材料组:否”找到的信息“等等])。现在,在您的活动中保留对此数据模型的引用:

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;
}

答案 1 :(得分:1)

  

有没有办法在一个按钮中添加一个按钮作为最后一个孩子   '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);
       }
   }
   //....
}

答案 2 :(得分:1)

首先,您需要实现自己的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;
        }

希望这有帮助。