Android SimpleCursorAdapter 在滚动时保持样式
Android SimpleCursorAdapter keep styling on scroll
我有一个简单的光标适配器,它工作正常并显示所有数据。
我的听众在点击时更改了颜色:
listViewM.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
boolean exists = false;
TextView item = (TextView) view.findViewById(R.id.r_lv_name);
String selectedAnswer = item.getText().toString();
MultiSelection multiSelection = new MultiSelection((int) id, selectedAnswer);
for (MultiSelection mm : mMultiSelectionsArray) {
if (id == mm.getId()) {
mMultiSelectionsArray.remove(mm);
exists = true;
break;
} else {
exists = false;
}
}
if (!exists) {
mMultiSelectionsArray.add(multiSelection);
item.setTextColor(Color.parseColor("#2EFE2E"));
} else {
mMultiSelectionsArray.remove(multiSelection);
item.setTextColor(Color.parseColor("#000000"));
}
}
});
现在滚动适配器正在回收视图并将新项目标记为已选中(通过添加颜色)。我想我需要以某种方式保持状态,然后将其应用于视图创建,但经过 3 天的查找后我放弃了。有人可以帮忙吗?
尝试使用自定义适配器,例如:
public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {
public CustomSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
//HERE you can set the correct color for each item
TextView item = (TextView) view.findViewById(R.id.r_lv_name);
boolean exists = //check is item is selected
if (!exists) {
item.setTextColor(Color.parseColor("#2EFE2E"));
} else {
item.setTextColor(Color.parseColor("#000000"));
}
}
}
我有一个简单的光标适配器,它工作正常并显示所有数据。 我的听众在点击时更改了颜色:
listViewM.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
boolean exists = false;
TextView item = (TextView) view.findViewById(R.id.r_lv_name);
String selectedAnswer = item.getText().toString();
MultiSelection multiSelection = new MultiSelection((int) id, selectedAnswer);
for (MultiSelection mm : mMultiSelectionsArray) {
if (id == mm.getId()) {
mMultiSelectionsArray.remove(mm);
exists = true;
break;
} else {
exists = false;
}
}
if (!exists) {
mMultiSelectionsArray.add(multiSelection);
item.setTextColor(Color.parseColor("#2EFE2E"));
} else {
mMultiSelectionsArray.remove(multiSelection);
item.setTextColor(Color.parseColor("#000000"));
}
}
});
现在滚动适配器正在回收视图并将新项目标记为已选中(通过添加颜色)。我想我需要以某种方式保持状态,然后将其应用于视图创建,但经过 3 天的查找后我放弃了。有人可以帮忙吗?
尝试使用自定义适配器,例如:
public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {
public CustomSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
//HERE you can set the correct color for each item
TextView item = (TextView) view.findViewById(R.id.r_lv_name);
boolean exists = //check is item is selected
if (!exists) {
item.setTextColor(Color.parseColor("#2EFE2E"));
} else {
item.setTextColor(Color.parseColor("#000000"));
}
}
}