Android RecycleView 里面有按钮点击 复用错位的问题解决

释放双眼,带上耳机,听听看~!

今天做了商城收货地址选择的部分,地址信息又RecycleVeiw展示。里面有一个自己写的TextView 选择那个 那个背景图就是一个对号。而且是单选。之前定义一个int 常量num,每次点击textview都会去叠加,用奇偶性进行判断。奇数就是选中 偶数没选中 。。。。。。。。。。。大概知道我原来的思路了,,

后来在滑动页面的时候,因为复用各种错乱。。。。。。后来找个好的解决办法。在javabean中添加一个 tag值。

bean  selecttag为tag

package com.ingkoo.FragListBean.MySetBean;
import org.json.JSONObject;
/**
 * Created by Administrator on 2016/6/16.
 */
public class ReceivePlaceListBean {
private String countryName;
    private String detail;
    private String idCard;
    private String post;
    private String cityName;
    private String provinceName;
    private String addressId;
    private int country;
    private String city;
    private String id;
    private int isDefault;
    private String userId;
    private String province;
    private String mobile;
    private String name;
    private String selecttag = "0";
    public ReceivePlaceListBean(JSONObject item) {
countryName = item.optString("countryName");
detail = item.optString("detail");
idCard = item.optString("idCard");
post = item.optString("post");
cityName = item.optString("cityName");
provinceName = item.optString("provinceName");
addressId = item.optString("addressId");
country = item.optInt(String.valueOf("country"));
city = item.optString("city");
id = item.optString("id");
isDefault = item.optInt(String.valueOf("isDefault"));
name = item.optString("name");
userId = item.optString("userId");
province = item.optString("province");
mobile = item.optString("mobile");
//        selecttag = item.optString("selecttag");
}
public ReceivePlaceListBean(String tag){
this.selecttag = tag;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getAddressId() {
return addressId;
}
public void setAddressId(String addressId) {
this.addressId = addressId;
}
public int getCountry() {
return country;
}
public void setCountry(int country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getIsDefault() {
return isDefault;
}
public void setIsDefault(int isDefault) {
this.isDefault = isDefault;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSelecttag() {
return selecttag;
}
public void setSelecttag(String selecttag) {
this.selecttag = selecttag;
}
}

activity  的item onclick事件

  mReceivePlaceAdapter.setmOnItemClickListener(new ReceivePlaceAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position, String msg) {
for(int i = 0;i<position;i++){
mReceivePlaceListArray.get(i).setSelecttag("0");
}
for(int i = position+1;i<mReceivePlaceListArray.size();i++){
mReceivePlaceListArray.get(i).setSelecttag("0");
}
mReceivePlaceListArray.get(position).setSelecttag("1");
mReceivePlaceAdapter.notifyDataSetChanged();
//                Toast.makeText(ChoosePlaceActivity.this,"点击了", Toast.LENGTH_SHORT).show();
}

adapter

//这里设置了点击的Tag 写Tag值在Activity里面,然后获取AddressId
if (mDatas.get(position).getSelecttag().equals("1")){
holder.mReceivePlaceSelectTv.setBackgroundResource(R.mipmap.ic_checked);
mAccount.setAddressId(mDatas.get(position).getAddressId());
Log.e("哈哈哈哈哈哈哈哈哈哈哈哈哈",mAccount.getAddressId());
}else {
holder.mReceivePlaceSelectTv.setBackgroundResource(R.mipmap.ic_check_no);
}
}

人已赞赏
Android文章

Android WebView加载Html 乱码问题

2020-3-28 10:33:27

Android文章

compileDebugJavaWithJavac.compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.

2020-3-28 11:48:01

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索