在我看来,使用自定义适配器会更容易:
class CustomAdapter extends ArrayAdapter<CustomRowItem> {
Context context;
public CustomAdapter(Context context, int resourceId, List<CustomRowItem> items) {
super(context, resourceId, items);
this.context = context;
}
private class ViewHolder {
TextView txt;
View layout;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
CustomRowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.сustom_list, null);
holder = new ViewHolder();
holder.txt = (TextView) convertView.findViewById(R.id.сustom_list_txt);
holder.layout = convertView.findViewById(R.id.сustom_list_layout);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txt.setText(rowItem.getText());
if(rowItem.isChecked())
holder.layout.setBackgroundColor(-16720999);
else
holder.layout.setBackgroundColor(0);
return convertView;
}
}
class CustomRowItem {
private boolean value;
private String text;
public CustomRowItem(String text, boolean value) {
this.text = text;
this.value = value;
}
public boolean isChecked() {
return value;
}
public void setChecked(boolean checked) {
value = checked;
}
public String getText() {
return text;
}
void setText(String text) {
this.text = text;
}
}
сustom_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:id="@+id/сustom_list_layout"
android:orientation="vertical" >
<TextView
android:id="@+id/сustom_list_txt"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
使用方法:
public class ExampleAct extends Activity {
final List<CustomRowItem> list = new ArrayList<CustomRowItem>();
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
ListView listView=(ListView)findViewById(R.id.listView);
adapter = new CustomAdapter(this, R.layout.сustom_list, list);
listView.setAdapter(adapter);
list.add(new CustomRowItem("unchecked item",false));
list.add(new CustomRowItem("checked item",true));
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View itemClicked, int index, long id) {
if(list.get(index).isChecked()) {
list.get(index).setChecked(false);
adapter.notifyDataSetChanged();
} else {
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View itemClicked, int index, long id) {
list.get(index).setChecked(true);
adapter.notifyDataSetChanged();
return true;
}
});
}
setChecked
和toggle
中,您忘记调用refreshDrawableState
。 - user1614309setChecked
和toggle
结束时调用refreshDrawableState
后能正常工作的解决方案。 - Walid Ammar