在ExpandableListview Android中动态灰显项目

时间:2014-08-28 19:30:07

标签: android expandablelistview expandablelistadapter

我正在创建ExpandableListview,它具有从BaseExpandableListAdapter派生的customAdapter。我希望在后续请求中将可扩展列表视图中的几个子项灰显

返回isChildSelectable = false,没有帮助,因为我需要在后续请求中使选项变灰。

notifyDataSetChanged也不是视图。

实施它的最佳方法是什么?

非常感谢!!

public class CustomAdapter extends BaseExpandableListAdapter {

static class ViewHolder{
    public CheckBox checkBox;
}

private Context mContext;

 private List<String> mListDataHeader; // header titles
    private Map<String, List<String>> mListDataChild;


    private Map<Integer, Map<Integer, Boolean>> mCheckState = new HashMap<Integer, Map<Integer,Boolean>>();

public CustomAdapter(Context context, List<String> listDataHeader, Map<String, List<String>> listChildData) {



    this.mContext = context;    

    this.mListDataHeader = listDataHeader;
    this.mListDataChild = listChildData;
}



@Override
public int getGroupCount() {
      return this.mListDataHeader.size();
}



@Override
public int getChildrenCount(int groupPosition) {        
      return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size();                       
}



@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
     return this.mListDataHeader.get(groupPosition);
}



@Override
public Object getChild(int groupPosition, int childPosition) {
     return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).get(childPosition);

}



@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}



@Override
public long getChildId(int groupPosition, int childPosition) {
     return childPosition;
}



@Override
public boolean hasStableIds() {
    return false;
}


public boolean areAllItemsEnabled() {
    return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {


    return convertView;
}



@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

 return convert view;
}



@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

创建一个新的List或SparseArray,您可以在其中保存每个组的子状态(灰色/非灰色):

public SparseArray<List> grayChildren = new SparseArray<List>();

在构造函数中或在扩展某个特定组时初始化该数组。

将您的isChildSelectable修改为:

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    if (grayChildren.get(groupPosition).get(childPosition)) {
        return true;
    } else {
        return false;
    }
}

现在,当您需要将某些项目调用变灰时:

mAdapter.grayChildren.get(groupPos).set(childPos, true);
mAdapter.notifyDataSetChanged();