自定义 ListView 的适配器
Adapter for custom ListView
我创建了一个联系人列表并使用自定义 ListView(有照片、姓名和不可见的 CheckBox)。像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<ImageView
android:id="@+id/lv_img"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/default_user"
android:layout_weight="0"/>
<TextView
android:id="@+id/lv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="16dp"
android:layout_gravity="center_vertical"
android:textSize="24sp"/>
<CheckBox
android:id="@+id/lv_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="invisible"
android:layout_gravity="center"/>
</LinearLayout>
为了管理这个,我使用适配器:
public class ContactAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private ArrayList<Contact> contacts;
private View view;
public ContactAdapter(Context context, ArrayList<Contact> contacts) {
this.context = context;
this.contacts = contacts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private Contact getContact(int position) {
return (Contact) getItem(position);
}
public void showCheckBox() {
view = inflater.inflate(R.layout.contact_item, null, false);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.contact_item, parent, false);
}
Contact c = getContact(position);
((TextView) view.findViewById(R.id.lv_name)).setText(c.getName() + " " + c.getSurname());
((ImageView) view.findViewById(R.id.lv_img)).setImageDrawable(c.getPhoto());
return view;
}
public ArrayList<Contact> getChecked() {
ArrayList<Contact> checkedContacts = new ArrayList<Contact>();
for (Contact c : contacts) {
if (c.isChecked()) checkedContacts.add(c);
}
return checkedContacts;
}
}
我需要在调用 showCheckBox() 时所有的 CheckBox 都变得可见,但只有 ListView 上的最后一个 CheckBox 变得可见。我该如何解决这个问题?
试试这个建议:
public class ContactAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private ArrayList<Contact> contacts;
private boolean isCheckBoxVisible;
private View view;
public ContactAdapter(Context context, ArrayList<Contact> contacts) {
this.context = context;
this.contacts = contacts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.isCheckBoxVisible = false;
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private Contact getContact(int position) {
return (Contact) getItem(position);
}
public void showCheckBox() {// if you want, you can add a boolean parameter to this method to change visibility
isCheckBoxVisible = true;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.contact_item, parent, false);
}
CheckBox cb = (CheckBox)view.findViewById(R.id.lv_box);
if(isCheckBoxVisible){
cb.setVisibility(View.VISIBLE);
} else {
cb.setVisibility(View.INVISIBLE);
}
Contact c = getContact(position);
((TextView) view.findViewById(R.id.lv_name)).setText(c.getName() + " " + c.getSurname());
((ImageView) view.findViewById(R.id.lv_img)).setImageDrawable(c.getPhoto());
return view;
}
public ArrayList<Contact> getChecked() {
ArrayList<Contact> checkedContacts = new ArrayList<Contact>();
for (Contact c : contacts) {
if (c.isChecked()) checkedContacts.add(c);
}
return checkedContacts;
}
}
我创建了一个联系人列表并使用自定义 ListView(有照片、姓名和不可见的 CheckBox)。像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<ImageView
android:id="@+id/lv_img"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/default_user"
android:layout_weight="0"/>
<TextView
android:id="@+id/lv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="16dp"
android:layout_gravity="center_vertical"
android:textSize="24sp"/>
<CheckBox
android:id="@+id/lv_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="invisible"
android:layout_gravity="center"/>
</LinearLayout>
为了管理这个,我使用适配器:
public class ContactAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private ArrayList<Contact> contacts;
private View view;
public ContactAdapter(Context context, ArrayList<Contact> contacts) {
this.context = context;
this.contacts = contacts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private Contact getContact(int position) {
return (Contact) getItem(position);
}
public void showCheckBox() {
view = inflater.inflate(R.layout.contact_item, null, false);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.contact_item, parent, false);
}
Contact c = getContact(position);
((TextView) view.findViewById(R.id.lv_name)).setText(c.getName() + " " + c.getSurname());
((ImageView) view.findViewById(R.id.lv_img)).setImageDrawable(c.getPhoto());
return view;
}
public ArrayList<Contact> getChecked() {
ArrayList<Contact> checkedContacts = new ArrayList<Contact>();
for (Contact c : contacts) {
if (c.isChecked()) checkedContacts.add(c);
}
return checkedContacts;
}
}
我需要在调用 showCheckBox() 时所有的 CheckBox 都变得可见,但只有 ListView 上的最后一个 CheckBox 变得可见。我该如何解决这个问题?
试试这个建议:
public class ContactAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private ArrayList<Contact> contacts;
private boolean isCheckBoxVisible;
private View view;
public ContactAdapter(Context context, ArrayList<Contact> contacts) {
this.context = context;
this.contacts = contacts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.isCheckBoxVisible = false;
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private Contact getContact(int position) {
return (Contact) getItem(position);
}
public void showCheckBox() {// if you want, you can add a boolean parameter to this method to change visibility
isCheckBoxVisible = true;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.contact_item, parent, false);
}
CheckBox cb = (CheckBox)view.findViewById(R.id.lv_box);
if(isCheckBoxVisible){
cb.setVisibility(View.VISIBLE);
} else {
cb.setVisibility(View.INVISIBLE);
}
Contact c = getContact(position);
((TextView) view.findViewById(R.id.lv_name)).setText(c.getName() + " " + c.getSurname());
((ImageView) view.findViewById(R.id.lv_img)).setImageDrawable(c.getPhoto());
return view;
}
public ArrayList<Contact> getChecked() {
ArrayList<Contact> checkedContacts = new ArrayList<Contact>();
for (Contact c : contacts) {
if (c.isChecked()) checkedContacts.add(c);
}
return checkedContacts;
}
}