Android中完美解决CheckedTextView选择乱跳问题

来源:互联网 发布:淘宝店过户 编辑:程序博客网 时间:2024/04/28 01:16

XML文件:

checked_layout.xml

<?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:orientation="vertical" >    <ListView        android:id="@+id/lv_checked"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </ListView></LinearLayout>

checkitem.xml

<?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:orientation="horizontal" >    <ImageView        android:id="@+id/iv_test"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:src="@drawable/ic_launcher" />    <CheckedTextView        android:id="@+id/cbt_test "        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checkMark="?android:attr/listChoiceIndicatorMultiple"        android:layout_gravity="center"        android:text="CheckedTextView" /></LinearLayout>


好了,上Java文件

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.app.Activity;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.CheckedTextView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;public class TestCheckedTextViewActivity extends Activity{private ListView mListView ;private List<String> list = new ArrayList<String>() ;private ListAdapter adapter ;private HashMap<Integer, Boolean> isCheckedMap = new HashMap<Integer, Boolean>() ;protected void onCreate(android.os.Bundle savedInstanceState) {super.onCreate(savedInstanceState) ;setContentView(R.layout.checked_layout) ;setUpViews() ;}private void setUpViews() {mListView = (ListView) this.findViewById(R.id.lv_checked) ;getData() ;adapter = new ListAdapter() ;Log.i("TAG", "adapter" + adapter) ;mListView.setAdapter(adapter) ;//每次点击时判断当前item的选项值,如果处于被选中状态则改变其值,然后刷新ListViewmListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {if (isCheckedMap.get(arg2)==false) {isCheckedMap.put(arg2, true) ;} else {isCheckedMap.put(arg2, false) ;}adapter.notifyDataSetChanged() ;}}) ;}private void getData(){for (int i = 0; i < 20; i++) {list.add("测试: "+i+"") ;//将checkedTextView的多选默认值设为falseisCheckedMap.put(i, false) ;}}private class ListAdapter extends BaseAdapter{public ListAdapter(){}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int arg0) {return null;}@Overridepublic long getItemId(int arg0) {return 0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {LayoutInflater inflater = LayoutInflater.from(TestCheckedTextViewActivity.this) ;ViewHolder holder = new ViewHolder() ;if (arg1 == null) {arg1 = inflater.inflate(R.layout.checkitem, null) ;holder.checkedTextView = (CheckedTextView) arg1.findViewById(R.id.cbt_test) ;holder.imageView = (ImageView) arg1.findViewById(R.id.iv_test) ;arg1.setTag(holder) ;} else {holder = (ViewHolder) arg1.getTag() ;}holder.checkedTextView.setText(list.get(arg0)) ;Log.i("TAG", "isChecked: " + "postion " +arg0 +", "+isCheckedMap.get(arg0)) ;holder.checkedTextView.setChecked(isCheckedMap.get(arg0)) ;holder.imageView.setImageResource(R.drawable.ic_launcher) ;return arg1 ;}}class ViewHolder {CheckedTextView checkedTextView ;ImageView imageView ;}}


通过Log可以看出,每次选中的对应位置以及状态。而界面上也不会任意跳动。其实,防止乱跳动的重点是不要直接用CheckedTextView来修改列表中Item的状态。

0 0