所以现在已经有一个月了,我还没有解决这个简单的问题。如何在Recyclerview中使用Expandable ListView?
目标: 我想有一个可扩展的列表,其中parent是textView,而child是包含2(现在)无线电的radiogroup。
我有一切工作,但后来我得知,Expandable ListView重用该对象建议使用recyclerview或viewholder。我尝试了查看者,但发生同样的事情: 如果我展开组1并检查无线电位置1,请展开组1并展开组2,检查无线电位置1。这是为什么发生?
下面是myClassList代码扩展BaseExapandableListAdapter:
public int getGroupCount() {
return titles.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return titles.get(groupPosition);
}
@Override
public List<String> getChild(int groupPosition, int childPosition) {
return colorsMap.get(titles.get(groupPosition));
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String title = (String) this.getGroup(groupPosition);
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert (layoutInflater != null);
convertView = layoutInflater.inflate(R.layout.parent_item_s, parent, false);
}
TextView textView = (TextView) convertView.findViewById(R.id.titleContent);
textView.setTypeface(null, Typeface.BOLD);
textView.setText(title);
return convertView;
}
class ViewHolder{
RadioButton radioButton;
RadioButton radioButton1;
ViewHolder(View view){
radioButton = (RadioButton) view.findViewById(R.id.rd1);
radioButton1 = (RadioButton) view.findViewById(R.id.rd2);
}
}
@Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
List<String> color = colorsMap.get(titles.get(groupPosition));
ViewHolder holder = null;
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert (layoutInflater != null);
convertView = layoutInflater.inflate(R.layout.child_item_s, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.radioButton.setText(color.get(0));
holder.radioButton1.setText(color.get(1));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
如何确保每个孩子都有自己的对象?
该位置可能是RecyclerViews,ListViews等的最大礼物。随着视图的回收,所提供的位置总是相同的。所以例如。 SparseArray可以用来在任何位置放置选择的收音机。然后你检索并使用,如果它没有null,并删除持有的位置,如果它不再被使用等等。有很多方法来保存在什么位置发生的事情。只要列表更改,请务必更新所有持有人。 – CmosBattery
我会查看SparseArryay,谢谢。另外,在RecyclerView成为事物之前,人们是如何处理这个问题的?肯定有人试图在2012年或更早的时候实施我想要的同样的事情? – rated2016