【问题标题】:Adding Buttons to expendableListView as last Child将按钮添加到 expandableListView 作为最后一个子项
【发布时间】:2013-08-13 10:07:42
【问题描述】:

我需要以下问题的帮助:

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

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

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

【问题讨论】:

  • 你已经有一个每个组的文本视图列表,你需要在每个列表的底部有一个按钮吗?
  • 是的,完全正确。而Textview elements的数量是可变的。

标签: android button expandablelistview


【解决方案1】:

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

【讨论】:

  • 首先,非常感谢您的努力,我仍然遇到一些错误,但我了解了基础知识。但是为什么myTv = (TextView) convertView.findViewById(textView1);没有找到我的观点呢?
  • 您需要为这两种布局发布 XML 布局。它应该在那里。
【解决方案2】:

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

【讨论】:

  • 好的,到目前为止它工作正常,我得到一个按钮作为第一组的最后一个孩子。但是我想要一个按钮作为每个组的最后一个孩子(所以我只使用了 if (isLastChild) {//button... else { //text 。但是现在该按钮是第二组的倒数第二个,第三组的倒数第三个等等。你有知道为什么吗?
  • 不,抱歉,不知道为什么。
【解决方案3】:

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多