我可以在ExpandableListView中实现CheckedTextView。但当我检查某个子项时,其他父项的某个子项也会被选中。我不明白我为什么要面对这样的问题?有人遇到过类似的情况吗?提前谢谢你的帮助。
编辑:
正如建议的那样,我在customadapter中创建了布尔多维数组:`
Boolean[][] categoryx = new Boolean` [5][5];
自定义适配器中有GetChildView方法:
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
System.out.print(groupPosition);
System.out.println(childPosition);
final String children = (String) getChild(groupPosition, childPosition);
CheckedTextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (CheckedTextView) convertView.findViewById(R.id.detail_textView1);
text.setText(children);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((CheckedTextView )v).toggle();
}
});
return convertView;
}
但我不知道应该使用哪种方法来使用访问的childposition切换我的值。
编辑2:尝试保存状态的自定义适配器:
import android.app.Activity;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;
public class MyCategoryAdapter extends BaseExpandableListAdapter {
private SparseArray<Groupx> groups;
public LayoutInflater inflater;
public Activity activity;
Boolean[][] categoryx = new Boolean [5][5];
public MyCategoryAdapter(Activity act, SparseArray<Groupx> groups) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
System.out.print(groupPosition);
System.out.println(childPosition);
final String children = (String) getChild(groupPosition, childPosition);
CheckedTextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (CheckedTextView) convertView.findViewById(R.id.detail_textView1);
text.setText(children);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Integer lol = (Integer) v.getTag();
((CheckedTextView )v).toggle();
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
Groupx group = (Groupx) getGroup(groupPosition);
((CheckedTextView) convertView).setText(group.stringx.toString());
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
最佳答案
这是由于对listview行回收的误解而经常出现的问题。
问题是,您只在视图中保留选中的属性。现在,ListView
s重用从屏幕上消失的单元格以节省内存,因此,一旦选中一行,另一行也将显示为选中,一旦原始单元格被回收。
要避免这种情况,您必须将checked状态保存在ListView
的数据源中,并在ListView
适配器中设置适当的checked值。