Android 可扩展列表视图,包括单选和多选

Android 可扩展列表视图,包括单选和多选,android,android-recyclerview,expandablelistview,selectionchanged,Android,Android Recyclerview,Expandablelistview,Selectionchanged,[适配器类] 公共类CustomExpandableListAdapter扩展了BaseExpandableListAdapter{ private Context context; private List<String> expandableListTitle; private HashMap<String, List<FilterList>> expandableListDetail; private boolean checked; private i

[适配器类]

公共类CustomExpandableListAdapter扩展了BaseExpandableListAdapter{

private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<FilterList>> expandableListDetail;
private boolean checked;
private int lastClickedPosition;

    private List<Boolean> setValueForSeletedFilter;
private String[] keysOfHashmap;

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                   HashMap<String, List<FilterList>> expandableListDetail, List<Boolean> setValueForSeletedFilter) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
    this.setValueForSeletedFilter = setValueForSeletedFilter;

}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .get(expandedListPosition).getSearchFilterValue();
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(final int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String expandedListText = (String) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        int i = listPosition;
        int j = expandedListPosition;

        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);



        TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.expandedListItem);
        expandedListTextView.setText(expandedListText);
        String s = expandableListTitle.get(listPosition);
        final ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_select);

        if (setValueForSeletedFilter.get(expandedListPosition) == true) {
            imageView.setVisibility(View.VISIBLE);
        } else {
            imageView.setVisibility(View.GONE);
        }


    }
    return convertView;
}

private void toggleSelection(int i, View v) {
    int j = i;
    ImageView imageView = (ImageView) v.findViewById(R.id.iv_select);
    imageView.setImageResource(R.drawable.ic_review);
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

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

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

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}

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

public void setData(){
    notifyDataSetChanged();
}

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}
私有上下文;
私有列表expandableListTitle;
私有HashMap expandableListDetail;
私有布尔检查;
上次点击位置的私人信息;
私有列表SetValueForEletedFilter;
私有字符串[]密钥映射;
公共CustomExpandableListAdapter(上下文、列表expandableListTitle、,
HashMap expandableListDetail,ListSetValueForSeleedFilter){
this.context=上下文;
this.expandableListTitle=expandableListTitle;
this.expandableListDetail=expandableListDetail;
this.setValueForSeletedFilter=setValueForSeletedFilter;
}
@凌驾
公共对象getChild(int listPosition,int expandedListPosition){
返回this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition).getSearchFilterValue();
}
@凌驾
公共长getChildId(int-listPosition、int-expandedListPosition){
返回expandedListPosition;
}
@凌驾
公共视图getChildView(最终整型listPosition,最终整型expandedListPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串expandedListText=(字符串)getChild(listPosition,expandedListPosition);
if(convertView==null){
int i=列表位置;
int j=展开列表位置;
LayoutInflater LayoutInflater=(LayoutInflater)this.context
.getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.list_项,空);
TextView expandedListTextView=(TextView)convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
字符串s=expandableListTitle.get(listPosition);
最终ImageView ImageView=(ImageView)convertView.findViewById(R.id.iv_选择);
if(setValueForSeletedFilter.get(expandedListPosition)=true){
设置可见性(View.VISIBLE);
}否则{
设置可见性(View.GONE);
}
}
返回视图;
}
私有无效切换选择(int i,视图v){
int j=i;
ImageView ImageView=(ImageView)v.findViewById(R.id.iv_选择);
imageView.setImageResource(R.drawable.ic_review);
}
@凌驾
公共int getChildrenCount(int listPosition){
返回this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@凌驾
公共对象getGroup(int listPosition){
返回此.expandableListTitle.get(listPosition);
}
@凌驾
public int getGroupCount(){
返回此.expandableListTitle.size();
}
@凌驾
公共长getGroupId(int-listPosition){
返回列表位置;
}
@凌驾
公共视图getGroupView(int listPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串listTitle=(字符串)getGroup(listPosition);
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)this.context。
getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.list\u组,空);
}
TextView listTitleTextView=(TextView)convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null,Typeface.BOLD);
setText(listTitle);
返回视图;
}
@凌驾
公共布尔表ID(){
返回true;
}
public void setData(){
notifyDataSetChanged();
}
@凌驾
公共布尔值isChildSelectable(int listPosition,int expandedListPosition){
返回true;
}
}

[设置适配器类的值并通知适配器更改值]

final HashMap>setValueForSeletedFilter=new HashMap(); 字符串头=null; final List booleanList=new ArrayList(); final HashMap>stringListHashMap=new HashMap(); 最终列表expandableListTitle; 对于(int i=0;i
if(filterInfo.get(i).getFilterHeaderName().equalsIgnoreCase(“距离”)){
header=filterInfo.get(i).getFilterHeaderName();
对于(int j=0;j        if (filtersInfos.get(i).getFilterHeaderName().equalsIgnoreCase("Distance")) {
            header = filtersInfos.get(i).getFilterHeaderName();
            for (int j = 0; j < filtersInfos.get(i).getFilterLists().size(); j++)
                booleanList.add(false);
        }
    }
    setValueForSeletedFilter.put(header, booleanList);


    expandableListTitle = new ArrayList<String>(stringListHashMap.keySet());
    final CustomExpandableListAdapter adapter = new CustomExpandableListAdapter(this, expandableListTitle, stringListHashMap, booleanList);
    expandableListView.setAdapter(adapter);
    filterDialog.show();

    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {

            Toast.makeText(getApplicationContext(),
                    expandableListTitle.get(groupPosition) + " List Expanded.",
                    Toast.LENGTH_SHORT).show();
        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    expandableListTitle.get(groupPosition) + " List Collapsed.",
                    Toast.LENGTH_SHORT).show();
        }
    });

    final boolean[] checked = {false};
    final int[] lastClickedPosition = {0};
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            if (expandableListTitle.get(groupPosition).equalsIgnoreCase("Distance")) {
                booleanList.add(childPosition, true);
                setValueForSeletedFilter.put(expandableListTitle.get(groupPosition), booleanList);
                expandableListView.setAdapter(adapter);
                adapter.notifyDataSetChanged();

            }

            return false;
        }
    });

    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            filterDialog.dismiss();
        }
    });
    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view,    ViewGroup viewGroup) {
    View v = LayoutInflater.from(context).inflate(R.layout.expandablelv_child, null);

    final CheckBox checkBox = v.findViewById(R.id.expandable_lv_child_cb);
    RelativeLayout quantityContainer = v.findViewById(R.id.expandable_lv_child_quantity_cont);
    final TextView textView = v.findViewById(R.id.expandable_lv_child_quantity_tv);

    checkBox.setText(children.get(i).get(i1).getItemName());

    double d=Double.parseDouble(children.get(i).get(i1).getMinQty());

    int k=(int)d;

    textView.setText(String.valueOf(k));


    quantityContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            QuantityDialog dialog = new QuantityDialog(context, children.get(i).get(i1).getMinQty(),
                    children.get(i).get(i1).getMaxQty(), new DialogSelectable() {
                @Override
                public void onQuantitySelected(String quantity) {
                    if (group[i].parent == 1 && group[i].child == i1)
                        textView.setText(quantity);
                        ExpandableLvAdapter.quantity[i] = quantity;
                }
            });
            dialog.show();
        }
    });

    try {
        // if (model.children.get(i).size() < children.get(i).size()) {
        try {
            model.children.get(i).add(checkBox);
        } catch (Exception e) {
            ArrayList<CheckBox> temp = new ArrayList<>();
            temp.add(checkBox);
            model.children.add(temp);
        }
        //}
    } catch (Exception e) {
        try {
            model.children.get(i).add(checkBox);
        } catch (Exception ex) {
            ArrayList<CheckBox> temp = new ArrayList<>();
            temp.add(checkBox);
            model.children.add(temp);
        }
    }

    if (group[i].parent == 1) {
        model.children.get(i).get(group[i].child).setEnabled(true);
        model.children.get(i).get(group[i].child).setChecked(true);
        model.disable(i, children.get(i).get(group[i].child).getItemName());
    } else {
        model.enable(i);
    }


    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if (b) {
                group[i].parent = 1;
                group[i].child = i1;
                model.disable(i, children.get(i).get(i1).getItemName());
                selected.add(children.get(i).get(i1));
                quantity[i] = children.get(i).get(i1).getMaxQty();
            } else {
                group[i].parent = 0;
                model.enable(i);
            }
        }
    });

    return v;
}