如图所示:
我点击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> |
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 | |
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 | |
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 |
02 | UpdateRecordEditAdapter adapter; |
03 |
04 | ListView listview; |
05 |
06 |
07 |
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 | |
23 | if (adapter.first[position]== 0 ){ |
24 | adapter.first[position]= 1 ; |
25 | } else { |
26 | adapter.first[position]= 0 ; |
27 | } |
28 |
29 | } |
30 | }); |