实现效果: 长按LitsView的item时, CheckBox显示,CheckBox可选中,在这个效果当中可能会有CheckBox被选中时出现混乱的情况,在此记录一下这个解决办法.
-
先做准备工作: 一个listview 的item行布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_margin="10dp" android:id="@+id/tv_item_list_name" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_margin="10dp" android:layout_toRightOf="@id/tv_item_list_name" android:id="@+id/tv_item_list_age" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <CheckBox android:layout_alignParentRight="true" android:layout_margin="10dp" android:visibility="gone" android:id="@+id/ckb_item_list" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
-
listview的adapter
package com.limiao.checkbox; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import java.util.List; import java.util.zip.Inflater; /** * Created by limiao on 16/11/24. */ public class MyAdapter extends BaseAdapter { private List<Person> mPersons; private Context mContext; private boolean mVisibility = false;// checkbox是否显示,默认不显示 public MyAdapter(Context context) { mContext = context; notifyDataSetChanged(); } public void setVisibility(boolean visibility) { mVisibility = visibility; notifyDataSetChanged(); } public void setPersons(List<Person> persons) { mPersons = persons; notifyDataSetChanged(); } @Override public int getCount() { return mPersons == null ? 0 : mPersons.size(); } @Override public Object getItem(int position) { return mPersons.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, parent, false); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } Person person = mPersons.get(position); holder.mNameTv.setText(person.getName()); holder.mAgeTv.setText(person.getAge()); if (mVisibility) { holder.mCheckBox.setVisibility(View.VISIBLE); final ViewHolder finalHolder = holder; /** * 此处要用setOnClickListener,而不是setOnCheckedChangeListener() * 否则会出现混乱的情况 */ holder.mCheckBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CheckBox checkBox = (CheckBox) v; finalHolder.mCheckBox.setChecked(checkBox.isChecked()); } }); } return convertView; } private static class ViewHolder { private TextView mNameTv; private TextView mAgeTv; private CheckBox mCheckBox; public ViewHolder(View view) { mNameTv = (TextView) view.findViewById(R.id.tv_item_list_name); mAgeTv = (TextView) view.findViewById(R.id.tv_item_list_age); mCheckBox = (CheckBox) view.findViewById(R.id.ckb_item_list); }
}
在为CheckBox设置选中状态时,这里需要用setOnClickListener(),才不会出现混乱,如果用setOnCheckedChangeListener()就会出现以下的情况
- 一个很简单的实体类
package com.limiao.checkbox;
/**
* Created by limiao on 16/11/23.
*/
public class Person {
String name ;
String age;
public Person(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
-
最后是MainActivity
package com.limiao.checkbox; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView mListView; private MyAdapter mMyAdapter; private List<Person> mPersons; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.lv_aty_main); mPersons = new ArrayList<>(); for (int i = 0; i < 90; i++) { Person person = new Person("aaa","34"); mPersons.add(person); } mMyAdapter = new MyAdapter(this); mMyAdapter.setPersons(mPersons); mListView.setAdapter(mMyAdapter); mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { mMyAdapter.setVisibility(true); return false; } }); } }