//—————————————–先上效果图—————————————-
//1.———————第一步 写布局
//————————-这是Activity的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/mQuan"
android:text="全选"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/mFan"
android:text="反选"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/mQu"
android:text="取消"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/mShan"
android:text="删除"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="@+id/mListView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
//——————————再写一个item适配器条目的布局
//————–写布局一定注意,CheckBox会阻断传向子控件的点击事件
//在xml最上面加一行
android:descendantFocusability="blocksDescendants"
//——————-这样就不会阻断了
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content"
>
<TextView
android:layout_centerVertical="true"
android:id="@+id/mText"
android:layout_width="wrap_content"
android:layout_height="60dp" />
<CheckBox
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:id="@+id/mCheck"
android:layout_width="wrap_content"
android:layout_height="60dp" />
</RelativeLayout>
//2.———————第二步建立集合与适配器并适配—————————
//一个数据集合,一个Boolean类型的集合,默认都添加false
private List<String> mList;
private List<Boolean> mCheckList;
mList=new ArrayList<>();
mCheckList=new ArrayList<>();
for (int i = 0; i < 10; i++) {
mList.add("WWW.baidu.COM");
mCheckList.add(false);
}
//3.————————第三步 ListView适配器————————————-
public class Apader extends BaseAdapter {
//构造方法
private List<String> mList;
private Context context;
public Apader(List<String> mList, Context context) {
this.mList = mList;
this.context = context;
}
//写个无参的方法,mList集合和mCheckList集合绑定到一起
private List<Boolean> mCheckList;
public void setData(List<String> mList, List<Boolean> mCheckList) {
this.mList = mList;
this.mCheckList = mCheckList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
//把convertView传到ViewHolder里,最后返回一个viewholder,id都在viewholder里
ViewHolder viewHolder=ViewHolder.getViewHolder(convertView);
//一条数据和一个复选框
viewHolder.mCheck.setChecked(mCheckList.get(position));
viewHolder.mText.setText(mList.get(position));
//点击事件,点击哪个复选框,让哪个当前下标选中,刷新适配器
viewHolder.mCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckList.set(position,isChecked);
notifyDataSetChanged();
}
});
return convertView;
}
//减少创建,多复用
public static class ViewHolder {
public View rootView;
public TextView mText;
public CheckBox mCheck;
public ViewHolder(View rootView) {
this.rootView = rootView;
this.mText = (TextView) rootView.findViewById(R.id.mText);
this.mCheck = (CheckBox) rootView.findViewById(R.id.mCheck);
}
//参数为convertView的公共静态方法,上面调用
public static ViewHolder getViewHolder(View convertView) {
//一进来先从convertView取,如果没有,则走以下
ViewHolder viewHolder= (ViewHolder) convertView.getTag();
//如果viewHolder为空则开始创建ViewHolder
if (viewHolder==null){
viewHolder=new ViewHolder(convertView);
//然后再给viewHolder赋上
convertView.setTag(viewHolder);
}
//最后返回一个viewHolder,布局id就都在viewholder里
return viewHolder;
}
}
}
4.————————————-第四步 返回Activity判断——————————————
//适配器适配,并且通过apader调用适配器里写的setData()方法,里面放mList,mCheckList这两个集合,绑定到一起了
apader=new Apader(mList,this);
apader.setData(mList,mCheckList);
mListView.setAdapter(apader);
//ListView点击事件,点击那个让哪个条目的复选框和当前状态不一样,true为false,false为true
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCheckList.set(position,!mCheckList.get(position));
apader.setData(mList,mCheckList);
apader.notifyDataSetChanged();
}
});
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mQuan:
quanXuan();
break;
case R.id.mFan:
fanXuan();
break;
case R.id.mQu:
quXiao();
break;
case R.id.mShan:
shanChu();
break;
}
}
//删除
private void shanChu() {
for (int i = 0; i < mCheckList.size(); i++) {
if (mCheckList.get(i)){
mCheckList.remove(i);
mList.remove(i);
i--;
}
}
apader.setData(mList,mCheckList);
apader.notifyDataSetChanged();
}
//取消
private void quXiao() {
for (int i = 0; i <mCheckList.size() ; i++) {
mCheckList.set(i,false);
}
apader.setData(mList,mCheckList);
apader.notifyDataSetChanged();
}
//反选
private void fanXuan() {
for (int i = 0; i <mCheckList.size() ; i++) {
mCheckList.set(i,!mCheckList.get(i));
}
apader.setData(mList,mCheckList);
apader.notifyDataSetChanged();
}
//全选
private void quanXuan() {
for (int i = 0; i <mCheckList.size() ; i++) {
mCheckList.set(i,true);
}
apader.setData(mList,mCheckList);
apader.notifyDataSetChanged();
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118367.html