栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

RecyclerView中的选定项目在滚动时发生更改

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RecyclerView中的选定项目在滚动时发生更改

Recyclerview总是在滚动时重用视图,因此您必须将选定的位置存储到临时arraylist中,然后将条件检查保持在onBindViewHolder中,以确保该特定位置是否已经存在于arraylist中?我更新了你的衣服。在评论中找到以下变化

    private List<Occurrence> mDataSet;private Activity activity;//Added here temporary ArrayListprivate ArrayList<String> mSelectedPosition = new ArrayList<String>;public <OccurrencesActivity extends OnOccurrenceSelectedListener> OccurrencesAdapter(OccurrencesActivity occurrencesActivity, List<Occurrence> occurrences) {    this.activity = (android.app.Activity) occurrencesActivity;    mDataSet = occurrences;}@Overridepublic void onBindViewHolder(final ViewHolder holder, final int position) {    //Set ViewTag    holder.itemView.setTag(position);    //Check everyposition during view binding process    if(mSelectedPosition.contains(String.valueOf(position))){     holder.itemView.setBackgroundColor(App.getContext().getResources().getColor(R.color.white));     holder.titleTextView.setTextColor(App.getContext().getResources().getColor(R.color.turquoise));     holder.statusTextView.setTextColor(App.getContext().getResources().getColor(R.color.grey));     holder.dateTextView.setTextColor(App.getContext().getResources().getColor(R.color.grey));     holder.timeTextView.setTextColor(App.getContext().getResources().getColor(R.color.grey));     }else{    holder.itemView.setBackgroundColor(App.getContext().getResources().getColor(R.color.white));         holder.titleTextView.setTextColor(App.getContext().getResources().getColor(R.color.turquoise));         holder.statusTextView.setTextColor(App.getContext().getResources().getColor(R.color.grey));         holder.dateTextView.setTextColor(App.getContext().getResources().getColor(R.color.grey));         holder.timeTextView.setTextColor(App.getContext().getResources().getColor(R.color.grey));      }    Occurrence instance = mDataSet.get(position);    ...    setonClickListener(holder, instance);    }private void setonClickListener(final ViewHolder holder, final Occurrence occurrence) {    holder.itemView.setonClickListener(new View.onClickListener() {        @Override        public void onClick(View view) {       // Get Position         int position = (int) view.getTag(); //Remove SelectedPosition if Already there   if(mSelectedPosition.contains(position))       mSelectedPosition.remove(String.valueOf(position));    else       mSelectedPosition.add(String.valueOf(position));     notifyDataSetChanged();    //Not sure about this lines      occurrence.setSelected(!occurrence.isSelected());     ((OnOccurrenceSelectedListener)activity).onOccurrenceSelected(mDataSet); }        });    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410542.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号