问一下高手,为什么,本人在给RecycleView的adapter的加载list更新了,notifyDataSetChanged()无效,未更新
。
。
package sunland.com.itaxi.ui.illegal; import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import javax.inject.Inject; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import sunland.com.itaxi.R; import sunland.com.itaxi.bean.IllegalBean; import sunland.com.itaxi.bean.IllegalTypeBean; import sunland.com.itaxi.bean.PlateListBean; import sunland.com.itaxi.ui.punish.penalties.PenaltiesActivity; /** * Created by zjl on 2016/9/21. */ public class IllegalAdapter extends RecyclerView.Adapter<IllegalAdapter.MyViewHolder> { private List<IllegalBean> list; private Context context; private int type; private int platform; private ArrayList<IllegalTypeBean> typelist = new ArrayList<>(); private ArrayList<PlateListBean> platformlist = new ArrayList<>(); @Inject public IllegalAdapter(Context context) { this.context = context; } public void bind(List<IllegalBean> list) { Log.d("zjlsize+",""+list.size()); this.list = list;//list赋值是改变了 //notifyDataSetChanged();里面写也是无效的 } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_illegal_recycleview_item, parent, false); MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.pos = position; Log.d("zjl**istrue", "*" + list.get(position).isIsTrue()); if (!list.get(position).isIsTrue()) { holder.hide_rl.setVisibility(View.VISIBLE); holder.hide_rl.getBackground().setAlpha(204); holder.ifclick = 0; } else { holder.hide_rl.setVisibility(View.GONE); holder.ifclick = 1; } if (list.get(position).getGender() == 0) { holder.offender_portrait_rv.setImageDrawable(context.getResources().getDrawable(R.drawable.woman)); } else { holder.offender_portrait_rv.setImageDrawable(context.getResources().getDrawable(R.drawable.man)); } } @Override public int getItemCount() { return list.size(); } public interface OnItemClickLitener { void onItemClick(IllegalBean illegalBean, int position); } private OnItemClickLitener mOnItemClickLitener; public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; } class MyViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.activity_illegal_recycleview_item_show_rl) RelativeLayout show_rl; @BindView(R.id.activity_illegal_recycleview_item_hide_rl) RelativeLayout hide_rl; @BindView(R.id.offender_portrait_rv) ImageView offender_portrait_rv; @BindView(R.id.offender_name_tv) TextView offender_name_tv; @BindView(R.id.offender_vehicleNum_tv) TextView offender_vehicleNum_tv; @BindView(R.id.offender_illegal_date_tv) TextView offender_illegal_date_tv; @BindView(R.id.offender_illegal_type_tv) TextView offender_illegal_type_tv; @BindView(R.id.offender_illegal_place_tv) TextView offender_illegal_place_tv; @BindView(R.id.offender_carinfo_platform_tv) TextView offender_carinfo_platform_tv; @BindView(R.id.offender_carinfo_area_tv) TextView offender_carinfo_area_tv; @BindView(R.id.offender_describe_tv) TextView offender_describe_tv; @BindView(R.id.offender_carsource_iv) ImageView offender_carsource_iv; IllegalBean illegalBean; int pos; int ifclick = 1;//能否可点击 @OnClick(R.id.activity_illegal_recycleview_item_show_rl) void listItemClick() { if (mOnItemClickLitener != null) { if (ifclick == 0) { //对“正在处理中”的处理 } else { mOnItemClickLitener.onItemClick(list.get(pos), pos); } } } public MyViewHolder(View view) { super(view); ButterKnife.bind(this, view); } } }
代码如上,本人使用了代码
adapter.bind(illegallist); adapter.notifyDataSetChanged();
获取bind()方法里的Log,发现list是改变了的,但是notifyDataSetChanged()无效。
求高手帮助!
解决方案
5
listView的数据源要保持唯一 ,简单的说,你bind()的时候,传入的list必须是同一个对象(指向同一个地址).
5
recycleview 的刷新 不建议你直接调用
notifyDataSetChanged
, 建议你看下最新的一个谷歌最近更新了Support Library 24.2.0,而DiffUtil就是在这个版本添加的一个工具类。
使用这个来刷新界面最好
5
更新时,在设置一次adpater
5
虽然没有亲测,但貌似一楼是对的,绑定不是同一个list 意思就是你 bind方法不要对 list进行重新赋值,首次绑定数据可以赋值一次,以后刷新应该直接刷新就可以了,不必再赋值。