解决“listView点击一个Item,另外几个Item也跟着改变”的问题
如图所看到的:
我点击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;
} }
});
解决“listView点击一个Item,另外几个Item也跟着改变”的问题的更多相关文章
- 解决listview点击item失效
开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了 ...
- android中listview的item滑动删除效果(已解决listview点击问题)
领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面 ...
- android 解决ListView点击与滑动事件冲突
如果你的ListView的Item有滑动功能,但又点击Item跳转到其它activity,这样若是在Adapter里面写点击事件是会导致滑动事件获取不到焦点而失效: 解决方法:不要在adapter里面 ...
- ListView点击事件失效(item里面有button按钮控件)解决方法
ListView点击事件失效解决方法: 一般出现这个情况,就是你的item里面有按钮的点击事件,你的item里面有button控件,button控件是抢占焦点的,只要在你的item布局里面这样子写就可 ...
- 解决ListView中Item的子控件与Item点击事件冲突
常常会碰到在ListView中点击当中一个Item.会一并触发其子控件的点击事件.比如Item中的Button.ImageButton等.导致了点击Item中Button以外区域也会触发Button点 ...
- Android中Listview点击item不变颜色以及设置listselector 无效
Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...
- SlideAndDragListView,一个可排序可滑动item的ListView
SlideAndDragListView简介 SlideAndDragListView,可排序.可滑动item显示"菜单"的ListView. SlideAndDragListVi ...
- Android ListView的item背景色设置以及item点击无响应等相关问题
Android ListView的item背景色设置以及item点击无响应等相关问题 在Android开发中,listview控件是非常常用的控件,在大多数情况下,大家都会改掉listview的ite ...
- ListView点击Item展开隐藏项(单项展开、多项展开、复杂布局时的展开处理)
手机屏幕毕竟有限,当我们要显示较多数据时便不得不舍去一些次要信息.将主要信息优先显示,也使显示效果更加简洁美观.遇到类似的需求,我们使用最多的就是 ListView ,而假设每次点击一个 Item 都 ...
随机推荐
- 宝塔Linux常用命令
https://www.bt.cn/bbs/thread-1186-1-1.html 2017年3月8日发布全新架构的宝塔Linux 面板3.1Beta版,到现在的5.2.0正式版,历经100多天打磨 ...
- Delphi IdTCPClient IdTCPServer 点对点传送文件
https://blog.csdn.net/luojianfeng/article/details/53959175 2016年12月31日 23:40:15 阅读数:2295 Delphi ...
- java8 - 5
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.Lis ...
- js与jquery的动态加载脚本文件
jquery动态加载 jQuery.getScript(url,[callback]) js动态加载 function loadJs(name) { document.write('<scrip ...
- 006 python的面向对象基础
1.类 描述具有相同属性与方法的对象的集合. 2.创建类 使用class来创建一个新类,class之后为类的名称并以冒号结尾 3.程序 #!/usr/bin/python # -*- coding: ...
- hdoj1233 还是畅通工程(Prime || Kruskal)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1233 思路 最小生成树问题,使用Prime算法或者Kruskal算法解决. 代码 Prime算法: # ...
- Spark入门1(以WordCount为例讲解flatmap和map之间的区别)
package com.test import org.apache.spark.{SparkConf, SparkContext} object WordCount { def main(args: ...
- elasticsearch-.yml(中文配置详解)
此elasticsearch-.yml配置文件,是在$ES_HOME/config/下 elasticsearch-.yml(中文配置详解) # ======================== El ...
- <泛> C++3D数学库设计详解 向量篇
// 注:本内容为作者原创,禁止在其他网站复述内容以及用于商业盈利,如需引用,请标明出处:http://www.cnblogs.com/lv_anchoret/ Preface 为了支持光线追踪的学习 ...
- Counter Strike HDU 2443 逆序对数
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2443 这个题目尝试了很多种方法都过不去,上网查了一下网友们的的思路,竟然和逆序对数有关系!! 题目大意: ...