如图所示:
我点击Item,右边的checkBox就会相应的变化,但是当我第一次做的时候,点击第一个Item,右边的checkBox变为绿色,但是当我listView往下拉的时候,发现下面也有是绿色的checkBox,很显然我是没有点击下面的。那么这个问题应该怎么解决呢,下面是我解决的方法:
首先是Item的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/gray" > <RelativeLayout android:id="@+id/update_rela" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="7" android:background="@drawable/set_edge_bg" > <ImageView android:id="@+id/record_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginRight="@dimen/small_margin" android:background="@drawable/record_icon2" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/record_icon" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/small_margin" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/time" android:textColor="@color/black_gray" /> <TextView android:id="@+id/tv_synctime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2014年9月19日 17:38:03" android:textColor="@color/black_gray" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginRight="10dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/local" android:textColor="@color/black_gray" /> <TextView android:id="@+id/tv_synccount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="双向同步完成,共165条数据" android:textColor="@color/black_gray" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/local" android:textColor="@color/black_gray" /> <TextView android:id="@+id/tv_synclocal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增0,删0,改0" android:textColor="@color/black_gray" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clouds" android:textColor="@color/black_gray" /> <TextView android:id="@+id/tv_syncserver" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增0,删0,改0" android:textColor="@color/black_gray" /> </LinearLayout> </LinearLayout> </RelativeLayout> <CheckBox android:id="@+id/update_delete" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:clickable="false" android:layout_gravity="center_vertical" android:button="@drawable/checkbox_selector" android:layout_marginLeft="@dimen/small_margin" /> </LinearLayout>
checkBox的背景是在res/drawable下的自定义的checkbox_selector.xml中的
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/check_on" /><!--选中时效果--> <item android:state_checked="false" android:drawable="@drawable/check_off" /><!--未选中时效果--> <!-- 修改成你自己的图片就可以了 --> </selector>
下面的是重点,继承的baseAdapter中的getView方法,我已经加了注释,自己能看明白的
package com.bcinfo.pwzs.ui.adapter; import java.util.List; import com.bcinfo.pwzs.R; import com.bcinfo.pwzs.bean.SyncLog; import com.bcinfo.pwzs.ui.adapter.UpdateRecordAdapter.Temple; 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.TextView; public class UpdateRecordEditAdapter extends BaseAdapter { LayoutInflater inflater; List<SyncLog> list; //这里定义一个数组,来识别checkBox是否被选中,<span style="font-family: Arial, Helvetica, sans-serif;">如果在不同包下,</span><span style="font-family: Arial, Helvetica, sans-serif;">记得这里是pubic。</span> public int first[]; public UpdateRecordEditAdapter(Context context, List<SyncLog> log) { inflater = LayoutInflater.from(context); this.list = log; //这里初始化数组 first=new int[log.size()]; for (int i = 0; i < log.size(); i++) { first[i]=0; } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Temple te; if (convertView == null) { te = new Temple(); convertView = inflater.inflate(R.layout.listview_update_edit, null); te.synccount = (TextView) convertView .findViewById(R.id.tv_synccount); te.synctime = (TextView) convertView.findViewById(R.id.tv_synctime); te.synclocal = (TextView) convertView .findViewById(R.id.tv_synclocal); te.syncserver = (TextView) convertView .findViewById(R.id.tv_syncserver); te.cb=(CheckBox) convertView.findViewById(R.id.update_delete); convertView.setTag(te); } else { te = (Temple) convertView.getTag(); } if (list != null && list.size() != 0) { te.synccount.setText(list.get(position).getCount()); te.synclocal.setText(list.get(position).getLocal()); te.syncserver.setText(list.get(position).getSever()); te.synctime.setText(list.get(position).getSyncTime()); } //在这里进行判断,如果是0,代表没被选中,如果是1,代表被选中 if(first[position]==0){ te.cb.setChecked(false); }else{ te.cb.setChecked(true); } return convertView; } class Temple { TextView synctime; TextView synclocal; TextView syncserver; TextView synccount; CheckBox cb; } }
接下来就是Activity里面进行操作了,因为Activity里面我写的东西比较多,不能全部复制过来,我把具体实现代码贴出来
listView的点击事件中那段注释下面是重点
//定义的adapter UpdateRecordEditAdapter adapter; //定义的listView ListView listview; //下面是listView的点击事件 listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { CheckBox cb = (CheckBox) view.findViewById(R.id.update_delete); if (cb.isChecked()) { cb.setChecked(false); } else { cb.setChecked(true); } //在这里进行改变adapter里面的first数组中的值,这里是重点哈 if(adapter.first[position]==0){ adapter.first[position]=1; }else{ adapter.first[position]=0; } } });
时间: 2024-10-05 17:53:29