解决listView嵌套CheckBox的选中错乱
来源:互联网 发布:js抢红包源代码 编辑:程序博客网 时间:2024/06/05 00:54
先来看看效果图:
因为是个演示的Demo所以就写假数据了 -_ -~!
这样你滑动的时候就不会因为复用问题,导致checkBox选中错乱
1.布局文件
<ListView android:id="@+id/check_list" android:layout_width="match_parent" android:layout_height="match_parent"/>2.item的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <!--android:focusable="false"解决check的抢占焦点--> <CheckBox android:id="@+id/item_check" android:layout_width="wrap_content" android:focusable="false" android:layout_height="wrap_content" />. <TextView android:id="@+id/item_text" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>3.设置Main方法
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { private ListView mListView; private CheckListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mListView = (ListView) findViewById(R.id.check_list); adapter = new CheckListAdapter(this); mListView.setAdapter(adapter); mListView.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //点击listItem设置checkBox的状态,未选中变为选中 adapter.getItem(position).setSelected(!adapter.getItem(position).isSelected()); adapter.notifyDataSetChanged(); }}4.设置listView的Adapter
package com.example.checklistview.adapter;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 com.example.checklistview.R;import com.example.checklistview.model.Model;import java.util.ArrayList;import java.util.List;/** * Created by ALong on 2017/4/8. */public class CheckListAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener { List<Model> data; LayoutInflater inflater; public CheckListAdapter(Context context) { this.inflater=LayoutInflater.from(context); this.data=new ArrayList<>(); for (int i = 0; i < 30; i++) { Model model=new Model(); model.setName("今天是个好日子"+i); data.add(model); } } @Override public int getCount() { return data!=null?data.size():0; } @Override public Model getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView=inflater.inflate(R.layout.list_item,parent,false); holder=new ViewHolder(convertView); convertView.setTag(holder); }else { holder= (ViewHolder) convertView.getTag(); } //一定要在绑定数据之前进行设置标记位 holder.checkBox.setTag(position); holder.textView.setText(getItem(position).getName()); holder.checkBox.setChecked(getItem(position).isSelected()); //标记一个Tag记录当前checkbox的位置 holder.checkBox.setOnCheckedChangeListener(this); return convertView; } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Integer position=(Integer)buttonView.getTag(); getItem(position).setSelected(isChecked); } public static class ViewHolder{ TextView textView; CheckBox checkBox; public ViewHolder(View itemView) { textView= (TextView) itemView.findViewById(R.id.item_text); checkBox= (CheckBox) itemView.findViewById(R.id.item_check); } }}5.用到的模型类,属性(1.name 2.是否选中)
public class Model { private String name; private boolean isSelected;//生成set get 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; }}好了,完美的解决了,希望可以帮助到你!
每天比别人多学0.01,你会知道的更多
0 0
- 解决listView嵌套CheckBox的选中错乱
- 解决listview嵌套checkbox滑动时状态的错乱
- ListView嵌套CheckBox滑动时CheckBox选中状态错乱
- ListView嵌套CheckBox滑动时CheckBox选中状态错乱
- 解决checkbox在ListView中的选中错乱问题
- ListView 数据错乱问题的解决(CheckBox)
- ListView/GridView嵌套CheckBox滑动时CheckBox选中状态显示错乱
- 关于ListView中checkBox选中状态错乱问题的解决办法
- ListView中CheckBox错乱解决
- 解决checkbox在滑动时选中状态错乱的问题
- ListView+CheckBox 选中状态错乱问题
- listView中有checkBox选中错乱问题解决
- listview中嵌套checkbox滑动时错乱
- Gridview的Item嵌套CheckBox的另一种实现,避免了选中错乱等问题
- listview复用checkbox显示错乱解决demo
- 解决:ListView+CheckBox 点击错乱问题
- Android ListView在有checkbox的情况下上下滑动选中状态错误以及position错乱
- Android之ListView的CheckBox状态错乱
- 百度网盘上传文件限制500个
- 少用Data类
- windbg调试之死锁
- Calendar类的典型应用
- 枚举类型enum
- 解决listView嵌套CheckBox的选中错乱
- Java 8新增日期时间新成员
- 当前项目登录掉线之后重新进入到最后访问页面的方法
- Java正则表达式基础
- iOS开发过程中 const 与 static 的使用
- 多线程 AfxBeginThread 与 CreateThread 的区别
- JAVA三大框架的作用
- 读取HDFS写入HBase
- Ubuntu 安装opencv3.0和Kdevelop