BaseExpandableListAdapter在旧项目上重写新项目

时间:2018-10-29 11:33:49

标签: android listview checkbox expandablelistview android-radiobutton

我正在使用BaseExpandableListAdapter来显示带有孩子的标题。子级可以是一个Checkbox或一个单选按钮,它取决于每个父项的父级,因此您可以有一个单选按钮组或一个复选框组。

问题是,当我滚动时,这些项目被覆盖不好,您可以看到新项目后面的旧元素。我尝试将getChildId和getGroupId与自动递增唯一性ID一起使用,并使用getCombinedGroupId和getCombinedChildId,将hasStableIds设置为true和false。但是它仍然在消沉。

这是我的Adapter类的一部分:

private Context context;
private ArrayList<Mode> parents;
private HashMap<Mode,ArrayList<Mode>> children;
private ArrayList<RadioButton> radioButtonList;
private byte idLang;
private ModeDao modeDao;
private ExpandableListView elOrderDialog;
private TextView title;
private Timer errorTimer;

public NewModesAdapter(Context context, ArrayList<Mode> parents, HashMap<Mode, ArrayList<Mode>> children, byte idLang,ExpandableListView elOrderDialog, TextView title) {
    this.context = context;
    this.parents = parents;
    this.children = children;
    this.idLang = idLang;
    this.modeDao = new ModeDao(context);
    this.elOrderDialog = elOrderDialog;
    this.title = title;
    this.radioButtonList = new ArrayList<>();
    this.errorTimer = new Timer();
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {

}

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

@Override
public int getChildrenCount(int groupPosition) {
    return children.get(parents.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this.parents.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.children.get(this.parents.get(groupPosition))
            .get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return this.parents.get(groupPosition).getAutoId();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return this.children.get(this.parents.get(groupPosition))
            .get(childPosition).getAutoId();
}

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    Mode currentFilterParent = (Mode) getGroup(groupPosition);
    String headerTitle = currentFilterParent.getTranslationName(idLang);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert inflater != null;
        convertView = inflater.inflate(R.layout.expandable_title, null);
    }

    FrameLayout flTitle = convertView.findViewById(R.id.fl_title);
    flTitle.setBackgroundColor(Color.parseColor(AppData.getColor2(context)));
    final TextView lblListHeader = convertView.findViewById(R.id.ex_tv_title);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setTextColor(Color.parseColor(AppData.getText2(context)));
    lblListHeader.setText(headerTitle);

    flTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //nothing to do
        }
    });

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    CheckBox cbModeItem;
    RadioButton rbModeItem;
    final Mode currentChild = (Mode) getChild(groupPosition, childPosition);
    Mode currentParent = modeDao.getModeById(currentChild.getIdFather());

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert inflater != null;
        convertView = inflater.inflate(R.layout.expandable_mode_item, null);
    }

    if (currentParent.getMultiOption() == 0) {
        rbModeItem = convertView.findViewById(R.id.rb_mode_item);
        rbModeItem.setText(currentChild.getTranslationName(idLang));
        rbModeItem.setTextColor(Color.parseColor(AppData.getText1(context)));
        rbModeItem.setVisibility(View.VISIBLE);
        setRBOnClick(rbModeItem, currentChild);
        radioButtonList.add(rbModeItem);
    } else {
        cbModeItem = convertView.findViewById(R.id.cb_mode_item);
        cbModeItem.setText(currentChild.getTranslationName(idLang));
        cbModeItem.setTextColor(Color.parseColor(AppData.getText1(context)));
        cbModeItem.setVisibility(View.VISIBLE);
        cbModeItem.setChecked(currentChild.isChecked());
        setCBOnClick(cbModeItem,currentChild);
    }

    return convertView;
}

谢谢。

0 个答案:

没有答案