栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

滚动ExpendableListView后计数器的值更改

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

滚动ExpendableListView后计数器的值更改

试试这个适配器:

public class ExpandableListAdapter extends baseExpandableListAdapter {class ViewHolder {    TextView childText;    TextView counterText;    Button addItemButton;    Button deleteItemButton;}class ChildItem{    String name;    int quantity;    ChildItem(String name, int quantity){        this.name = name;        this.quantity = quantity;    }}class Pos{    int group;    int child;    Pos(int group, int child){        this.group = group;        this.child = child;    }}private Context context;private List<String> listDataHeader;//private HashMap<String, List<String>> listHashMap;private HashMap<String, List<ChildItem>> listChildMap;public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {    this.context = context;    this.listDataHeader = listDataHeader;    listChildMap = new HashMap<>();    for(int i=0; i<getGroupCount(); i++){        List<ChildItem> listTemp = new ArrayList<>();        for(int j=0; j<listHashMap.get(listDataHeader.get(i)).size(); j++){ listTemp.add(new ChildItem(listHashMap.get(listDataHeader.get(i)).get(j), 0));        }        listChildMap.put(listDataHeader.get(i), listTemp);    }}@Overridepublic int getGroupCount() {    return listDataHeader.size();}@Overridepublic int getChildrenCount(int groupPosition) {    return listChildMap.get(listDataHeader.get(groupPosition)).size();}@Overridepublic String getGroup(int groupPosition) {    return listDataHeader.get(groupPosition);}@Overridepublic ChildItem getChild(int groupPosition, int childPosition) {    return listChildMap.get(listDataHeader.get(groupPosition)).get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {    return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {    return childPosition;}@Overridepublic boolean hasStableIds() {    return false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {    String headerTitle = getGroup(groupPosition);    if(convertView == null) {        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        convertView = inflater.inflate(R.layout.list_group, null);    }    TextView listHeader = (TextView) convertView.findViewById(R.id.list_header);    listHeader.setTypeface(null, Typeface.BOLD);    listHeader.setText(headerTitle);    return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {    ViewHolder viewHolder;    if(convertView == null) {        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        convertView = inflater.inflate(R.layout.list_item, null);        TextView textListChild = (TextView) convertView.findViewById(R.id.list_item_header);        TextView itemsCounter = (TextView) convertView.findViewById(R.id.items_counter);        Button addItemButton = (Button) convertView.findViewById(R.id.plus_btn);        viewHolder = new ViewHolder();        viewHolder.childText = textListChild;        viewHolder.counterText = itemsCounter;        viewHolder.addItemButton = addItemButton;    } else {        viewHolder = (ViewHolder) convertView.getTag();    }    ChildItem child = getChild(groupPosition, childPosition);    viewHolder.childText.setText(child.name);    viewHolder.counterText.setText("" + child.quantity);    viewHolder.addItemButton.setonClickListener(new View.onClickListener(){        @Override        public void onClick(View v) { Pos pos = (Pos)v.getTag(); ChildItem selectedItem = getChild(pos.group, pos.child); selectedItem.quantity = selectedItem.quantity + 1; notifyDataSetChanged(); PutOrderDrinks.addOrder(selectedItem.name);        }    });    viewHolder.addItemButton.setTag(new Pos(groupPosition, childPosition));    convertView.setTag(viewHolder);    return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {    return true;}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号