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
- Android中完美解决CheckedTextView选择乱跳问题
- [android]完美解决ListView加载网络图片乱跳问题
- 完美解决ListView加载网络图片乱跳问题
- 对于MyEclipse中光标乱跳问题的解决
- Android中ListView中使用CheckedTextView
- ListView(乱跳问题完美(包括点击闪动)解决_每一个条目都做动画_不复用convertview20160402)
- android使用CheckedTextView搭配listview完成选择列表
- Android 三星手机拍照,从图库选择照片旋转问题完美解决
- (转)Android 三星手机拍照,从图库选择照片旋转问题完美解决
- Android 三星手机拍照,从图库选择照片旋转问题完美解决
- 解决myeclipse 光标乱跳问题
- 解决Android自定义多选ListView乱序问题
- 【完美解决系列】解决Android Studio中找不到R文件的问题
- 【完美解决系列】解决在Android中使用background时导致图片被拉伸的问题
- 解决android中使用webview选择文件上传问题
- CheckedTextView的显示问题
- Android 如何完美解决MenuItem偏移问题
- android 启动白屏问题完美解决
- 线程状态
- Android之LinearLayout学习
- 使用命令行模拟Eclipse中Android Project的编译流程(从源代码编译出最后的apk)
- HOJ 1191 (next_permutation()的应用)
- 2014.3.29阿里实习笔试题
- Android中完美解决CheckedTextView选择乱跳问题
- maven springMVC+Mybatis
- dispatch_semaphore_create信号
- 数组指针(int *)(&a+1)
- dedecms V5.7笔记:判断空值或含有某个值的写法(例如是否推荐文章,是否图片等)及field:picname function='thumb(@me最大比例缩图的显示判断
- 【排序算法】经典排序算法之插入排序
- php函数传值的几个细节,及对应实例代码
- Maven系列2--pom.xml 配置详解
- poj1035 Spell Checker