滚动 Recycler View 时有意丢失数据
Posted
技术标签:
【中文标题】滚动 Recycler View 时有意丢失数据【英文标题】:Intent losing data when scrolling Recycler View 【发布时间】:2021-11-06 13:28:40 【问题描述】:我正在尝试将一些文本从 Recycler View 发送到另一个 Activity,并且当屏幕没有滚动到底部时它可以正常工作。 但是当我向下滚动时,Intent 中的一些数据会丢失。当我向上滚动并单击按钮时,不会发生这种情况。
收藏按钮代码来自here.
我的适配器代码:
public class AdapterLanches extends RecyclerView.Adapter<AdapterLanches.MyViewHolder> implements Filterable
public static MaterialFavoriteButton favoriteButton;
private List<Lanches> listaLanches;
private List<Lanches> listaLanchesCompleta;
public static Lanches itemAtual;
ArrayList<CharSequence> favoritos;
boolean fav = false;
int estadoPrograma;
public Button lc,fit;
int posicao = -1;
public class MyViewHolder extends RecyclerView.ViewHolder
public MaterialFavoriteButton favoriteButton;
TextView titulo;
RecyclerView recyclerView;
public MyViewHolder(@NonNull View itemView)
super(itemView);
lc = itemView.findViewById(R.id.lc);
fit = itemView.findViewById(R.id.fit);
favoriteButton = itemView.findViewById(R.id.favorite);
favoriteButton.setOnFavoriteChangeListener(new MaterialFavoriteButton.OnFavoriteChangeListener()
@Override
public void onFavoriteChanged(MaterialFavoriteButton buttonView, boolean favorite)
final int posicao = getAdapterPosition();
fav = favorite;
favoritos.add(listaLanchesCompleta.get(posicao).getTitulo());
VeganoActivity.intentLanches2.putExtra("titulo", favoritos);
System.out.println(favoritos);
);
titulo = itemView.findViewById(R.id.textnomeLanchesId);
favoritos = new ArrayList<>();
public AdapterLanches(List<Lanches> listaLanches)
this.listaLanches = listaLanches;
listaLanchesCompleta = new ArrayList<>(this.listaLanches);
@Override
public Filter getFilter()
return lanchesFilter;
public Filter lanchesFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
List<Lanches> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0)
filteredList.addAll(listaLanchesCompleta);
else
estadoPrograma = 1;
String filterPattern = constraint.toString().toLowerCase().trim();
for ( Lanches item : listaLanchesCompleta)
if (item.getTitulo().toLowerCase().contains(filterPattern))
filteredList.add(item);
FilterResults results = new FilterResults();
results.values = filteredList;
results.count = filteredList.size();
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
listaLanches.clear();
listaLanches.addAll((ArrayList<Lanches>)results.values);
notifyDataSetChanged();
;
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View itemListaLanches = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_lanches, parent, false);
final MyViewHolder holder = new MyViewHolder(itemListaLanches);
return holder;
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position)
itemAtual = listaLanches.get(position);
holder.titulo.setText(itemAtual.getTitulo());
if (itemAtual.getId() == 0)
fit.setVisibility(View.GONE);
lc.setVisibility(View.GONE);
if (itemAtual.getId() == 1)
lc.setVisibility(View.VISIBLE);
fit.setVisibility(View.GONE);
if (itemAtual.getId() == 2)
fit.setVisibility(View.VISIBLE);
lc.setVisibility(View.GONE);
@Override
public int getItemViewType(int position)
return position;
@Override
public int getItemCount()
return listaLanches.size();
欢迎任何帮助,谢谢。
【问题讨论】:
【参考方案1】:创建一个列表,为这些数据提供更广泛的范围。因为 RecyclerView 会在它们消失时擦除它们。
var stuffINeedToKeep=mutableListOf<String>()//this goes right under the class constructor so that it has wide scope
...
heart.setOnClickListener(
stuffINeedToKeep.add(dataFromViewHolder)
)
【讨论】:
ArrayList以上是关于滚动 Recycler View 时有意丢失数据的主要内容,如果未能解决你的问题,请参考以下文章
当用户将手从屏幕上移开时,如何使 Recycler View 滚动缓慢而流畅?
如何在 kotlin 的 Recycler View Adapter 中添加更多数据列表而不清除先前的数据?
如何使 Recycler View 表现得像 PlayStore Recycler Views