解决“listView点击一个Item,另外几个Item也跟着改变”的问题

本文解决了一个在ListView点击一个Item后,多个Item随之改变状态的问题。通过自定义适配器和监听器,确保只有点击的Item被正确处理,避免误触发其他Item的改变。使用了CheckBox作为交互元素,并在适配器中维护了一个数组来记录每个Item的选中状态。
解决“listView点击一个Item,另外几个Item也跟着改变”的问题

如图所示:


我点击Item,右边的checkBox就会相应的变化,但是当我第一次做的时候,点击第一个Item,右边的checkBox变为绿色,但是当我listView往下拉的时候,发现下面也有是绿色的checkBox,很显然我是没有点击下面的。那么这个问题应该怎么解决呢,下面是我解决的方法:

首先是Item的布局:


001 <?xml version="1.0" encoding="utf-8"?>
002 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
003     android:layout_width="match_parent"
004     android:layout_height="match_parent"
005     android:background="@color/gray" >
006  
007     <RelativeLayout
008         android:id="@+id/update_rela"
009         android:layout_width="0dp"
010         android:layout_height="wrap_content"
011         android:layout_weight="7"
012         android:background="@drawable/set_edge_bg" >
013  
014         <ImageView
015             android:id="@+id/record_icon"
016             android:layout_width="wrap_content"
017             android:layout_height="wrap_content"
018             android:layout_centerVertical="true"
019             android:layout_marginRight="@dimen/small_margin"
020             android:background="@drawable/record_icon2" />
021  
022         <LinearLayout
023             android:layout_width="wrap_content"
024             android:layout_height="wrap_content"
025             android:layout_toRightOf="@id/record_icon"
026             android:orientation="vertical" >
027  
028             <LinearLayout
029                 android:layout_width="wrap_content"
030                 android:layout_height="wrap_content"
031                 android:layout_marginTop="@dimen/small_margin"
032                 android:orientation="horizontal" >
033  
034                 <TextView
035                     android:layout_width="wrap_content"
036                     android:layout_height="wrap_content"
037                     android:text="@string/time"
038                     android:textColor="@color/black_gray" />
039  
040                 <TextView
041                     android:id="@+id/tv_synctime"
042                     android:layout_width="wrap_content"
043                     android:layout_height="wrap_content"
044                     android:text="2014年9月19日     17:38:03"
045                     android:textColor="@color/black_gray" />
046             </LinearLayout>
047  
048             <LinearLayout
049                 android:layout_width="wrap_content"
050                 android:layout_height="wrap_content"
051                 android:layout_marginTop="5dp"
052                 android:layout_marginRight="10dp"
053                 android:orientation="horizontal" >
054  
055                 <TextView
056                     android:layout_width="wrap_content"
057                     android:layout_height="wrap_content"
058                     android:text="@string/local"
059                     android:textColor="@color/black_gray" />
060  
061                 <TextView
062                     android:id="@+id/tv_synccount"
063                     android:layout_width="wrap_content"
064                     android:layout_height="wrap_content"
065                     android:text="双向同步完成,共165条数据"
066                     android:textColor="@color/black_gray" />
067             </LinearLayout>
068  
069             <LinearLayout
070                 android:layout_width="wrap_content"
071                 android:layout_height="wrap_content"
072                 android:layout_marginTop="5dp"
073                 android:orientation="horizontal" >
074  
075                 <TextView
076                     android:layout_width="wrap_content"
077                     android:layout_height="wrap_content"
078                     android:text="@string/local"
079                     android:textColor="@color/black_gray" />
080  
081                 <TextView
082                     android:id="@+id/tv_synclocal"
083                     android:layout_width="wrap_content"
084                     android:layout_height="wrap_content"
085                     android:text="增0,删0,改0"
086                     android:textColor="@color/black_gray" />
087             </LinearLayout>
088  
089             <LinearLayout
090                 android:layout_width="wrap_content"
091                 android:layout_height="wrap_content"
092                 android:layout_marginTop="5dp"
093                 android:layout_marginBottom="5dp"
094                 android:orientation="horizontal" >
095  
096                 <TextView
097                     android:layout_width="wrap_content"
098                     android:layout_height="wrap_content"
099                     android:text="@string/clouds"
100                     android:textColor="@color/black_gray" />
101  
102                 <TextView
103                     android:id="@+id/tv_syncserver"
104                     android:layout_width="wrap_content"
105                     android:layout_height="wrap_content"
106                     android:text="增0,删0,改0"
107                     android:textColor="@color/black_gray" />
108             </LinearLayout>
109  
110             
111         </LinearLayout>
112     </RelativeLayout>
113  
114      
115  
116         <CheckBox
117             android:id="@+id/update_delete"
118             android:layout_width="0dp"
119             android:layout_height="wrap_content"
120             android:layout_weight="1"
121             android:focusable="false"
122             android:clickable="false"
123             android:layout_gravity="center_vertical"
124             android:button="@drawable/checkbox_selector"
125             android:layout_marginLeft="@dimen/small_margin"
126              />
127    
128  
129 </LinearLayout>

checkBox的背景是在res/drawable下的自定义的checkbox_selector.xml中的

1 <?xml version="1.0" encoding="utf-8"?> 
2     <selector xmlns:android="http://schemas.android.com/apk/res/android"
3         <item android:state_checked="true"  
4             android:drawable="@drawable/check_on" /><!--选中时效果--> 
5         <item android:state_checked="false"  
6             android:drawable="@drawable/check_off" /><!--未选中时效果--> 
7       <!-- 修改成你自己的图片就可以了 --> 
8     </selector>
下面的是重点,继承的baseAdapter中的getView方法,我已经加了注释,自己能看明白的

01 package com.bcinfo.pwzs.ui.adapter;
02  
03 import java.util.List;
04  
05 import com.bcinfo.pwzs.R;
06 import com.bcinfo.pwzs.bean.SyncLog;
07 import com.bcinfo.pwzs.ui.adapter.UpdateRecordAdapter.Temple;
08  
09 import android.content.Context;
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.widget.BaseAdapter;
14 import android.widget.CheckBox;
15 import android.widget.TextView;
16  
17 public class UpdateRecordEditAdapter extends BaseAdapter {
18     LayoutInflater inflater;
19     List<SyncLog> list;
20      
21     //这里定义一个数组,来识别checkBox是否被选中,<span style="font-family: Arial, Helvetica, sans-serif;">如果在不同包下,</span><span style="font-family: Arial, Helvetica, sans-serif;">记得这里是pubic。</span>
22     public int first[];
23  
24     public UpdateRecordEditAdapter(Context context, List<SyncLog> log) {
25         inflater = LayoutInflater.from(context);
26         this.list = log;
27         //这里初始化数组
28         first=new int[log.size()];
29         for (int i = 0; i < log.size(); i++) {
30             first[i]=0;
31         }
32     }
33  
34     @Override
35     public int getCount() {
36         return list.size();
37     }
38  
39     @Override
40     public Object getItem(int position) {
41         return list.get(position);
42     }
43  
44     @Override
45     public long getItemId(int position) {
46         return position;
47     }
48  
49     @Override
50     public View getView(int position, View convertView, ViewGroup parent) {
51         Temple te;
52         if (convertView == null) {
53             te = new Temple();
54             convertView = inflater.inflate(R.layout.listview_update_edit, null);
55             te.synccount = (TextView) convertView
56                     .findViewById(R.id.tv_synccount);
57             te.synctime = (TextView) convertView.findViewById(R.id.tv_synctime);
58             te.synclocal = (TextView) convertView
59                     .findViewById(R.id.tv_synclocal);
60             te.syncserver = (TextView) convertView
61                     .findViewById(R.id.tv_syncserver);
62             te.cb=(CheckBox) convertView.findViewById(R.id.update_delete);
63             convertView.setTag(te);
64         else {
65             te = (Temple) convertView.getTag();
66         }
67         if (list != null && list.size() != 0) {
68             te.synccount.setText(list.get(position).getCount());
69             te.synclocal.setText(list.get(position).getLocal());
70             te.syncserver.setText(list.get(position).getSever());
71             te.synctime.setText(list.get(position).getSyncTime());
72         }
73         //在这里进行判断,如果是0,代表没被选中,如果是1,代表被选中
74         if(first[position]==0){
75             te.cb.setChecked(false);
76         }else{
77             te.cb.setChecked(true);
78         }
79          
80         return convertView;
81          
82     }
83  
84     class Temple {
85         TextView synctime;
86         TextView synclocal;
87         TextView syncserver;
88         TextView synccount;
89         CheckBox cb;
90     }
91  
92 }

接下来就是Activity里面进行操作了,因为Activity里面我写的东西比较多,不能全部复制过来,我把具体实现代码贴出来

listView的点击事件中那段注释下面是重点

01 //定义的adapter
02 UpdateRecordEditAdapter adapter;
03 //定义的listView
04 ListView listview;
05  
06  
07 //下面是listView的点击事件
08 listview.setOnItemClickListener(new OnItemClickListener() {
09  
10             @Override
11             public void onItemClick(AdapterView<?> parent, View view,
12                     int position, long id) {
13                  
14                 CheckBox cb = (CheckBox) view.findViewById(R.id.update_delete);
15                 if (cb.isChecked()) {
16                     cb.setChecked(false);
17                      
18                 else {
19                     cb.setChecked(true);
20                      
21                 }
22                 //在这里进行改变adapter里面的first数组中的值,这里是重点哈
23                 if(adapter.first[position]==0){
24                     adapter.first[position]=1;
25                 }else{
26                     adapter.first[position]=0;
27                 }
28  
29             }
30         });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值