我正在使用RecyclerView组件成功绘制一个列表,但是当我有很多项目时,如果我非常快地向下滚动,有时一个项目会在RecyclerView上重叠。如果我以“正常”速度滚动,只有当我在RecyclerView的第一次加载中快速向下滚动时,才会发生这种糟糕的性能。这似乎是一个错误...任何想法?这是我的适配器的代码:
public class RecibosListRVAdapter extends RecyclerView.Adapter<RecibosListRVAdapter.ItemReciboViewHolder> implements Filterable{
private static final String TAG = "RecibosListRVAdapter";
public static List<ItemRecibo> listRecibos, listRecibosFiltered;
private int lastPosition = -1;
Context context;
HashMap<String, Integer> hashLogos;
public InterfaceRecibo_Clicked asysncresponse;
TinyDB tini;
public static List<ItemRecibo> getListRecibosFiltered() {
return listRecibosFiltered;
}
public static List<ItemRecibo> getListRecibos() {
return listRecibos;
}
public static void setListRecibos(List<ItemRecibo> listRecibos) {
RecibosListRVAdapter.listRecibos = listRecibos;
}
public static void setListRecibosFiltered(List<ItemRecibo> listRecibosFiltered) {
RecibosListRVAdapter.listRecibosFiltered = listRecibosFiltered;
}
@Override
public Filter getFilter() {
return new RecibosFilter(this, listRecibos);
}
private static class RecibosFilter extends Filter {
private final RecibosListRVAdapter adapter;
private final List<ItemRecibo> originalList;
private final List<ItemRecibo> filteredList;
private RecibosFilter(RecibosListRVAdapter adapter, List<ItemRecibo> originalList) {
super();
this.adapter = adapter;
this.originalList = new LinkedList<>(originalList);
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (constraint.length() == 0) {
filteredList.addAll(originalList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final ItemRecibo user : originalList) {
if (user.getMontante().contains(constraint)) {
filteredList.add(user);
}
if (user.getDescripcion().toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredList.add(user);
}
if (user.getProveedor().toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredList.add(user);
}
if (user.getEstado().toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredList.add(user);
}
if (user.getFechaEntrada().toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredList.add(user);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.listRecibosFiltered.clear();
adapter.listRecibosFiltered.addAll((ArrayList<ItemRecibo>) results.values);
adapter.notifyDataSetChanged();
}
}
public RecibosListRVAdapter(FrmListadoRecibos activity, List<ItemRecibo> listRecibos, TinyDB tini)
{
this.asysncresponse = activity;
this.listRecibos = listRecibos;
this.listRecibosFiltered = new ArrayList<>(listRecibos);
hashLogos = Utilidades.rellenaHash();//TODO quitar estático
this.tini = tini;
}
@Override
public RecibosListRVAdapter.ItemReciboViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_listadorecibos, parent, false);
ItemReciboViewHolder itemrecibo_holder = new ItemReciboViewHolder(v, new ItemReciboViewHolder.IMyViewHolderClicks()
{
public void onPotatoClick(View caller, int posClicked) {
Log.d("TAG", "Poh-tah-tos");
if(caller instanceof CardView) {
asysncresponse.response_cardview_clicked(posClicked, (CardView) caller);
}
};
});
return itemrecibo_holder;
}
@Override
public void onBindViewHolder(RecibosListRVAdapter.ItemReciboViewHolder holder, int position)
{
if(position < listRecibosFiltered.size()) {
try {
holder.imgempresa.setImageResource(hashLogos.get(listRecibosFiltered.get(position).getUrlicon()));//R.mipmap.icn_iberdrola_beta
} catch (NumberFormatException ne) {
Log.e(TAG, "mal formato en RecibosListRVAdapter asignando la url del icono de empresa");
holder.imgempresa.setImageResource(R.mipmap.ic_launcher);
}
holder.txtrecibotitle.setText(listRecibosFiltered.get(position).getDescripcion());
holder.txtestado.setText(listRecibosFiltered.get(position).getEstado());
holder.txtfechavence.setText(listRecibosFiltered.get(position).getFechaEntrada());
holder.txtfacturaimporte.setText(TextUtils.concat(listRecibosFiltered.get(position).getMontante(), Constants.EUROSUFIJO));
if (tini.getBoolean(listRecibosFiltered.get(position).getId(), false)) {
holder.cv.setCardBackgroundColor(context.getResources().getColor(R.color.pagado));
holder.txtestado.setText(context.getResources().getString(R.string.pagado));
holder.txtestado.setTextColor(context.getResources().getColor(R.color.pagadoestado));
holder.txtestado.setTypeface(null, Typeface.BOLD);
} else {
holder.txtestado.setTextColor(context.getResources().getColor(R.color.pendienteorange));
holder.txtestado.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/robotolight.ttf"));
if (position % 2 == 0)
holder.cv.setCardBackgroundColor(context.getResources().getColor(R.color.orangelightest));
else
holder.cv.setCardBackgroundColor(context.getResources().getColor(R.color.orangelighter));
}
setAnimation(holder.itemView, position);
}
}
// Apply some custom animations to loading the items in the list
private void setAnimation(View viewToAnimate, int position)
{
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition)
{
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
@Override
public int getItemCount() {
if(this.listRecibosFiltered!=null)
return this.listRecibosFiltered.size();
else
return 0;
}
public static class ItemReciboViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
CardView cv;
ImageView imgempresa;
TextView txtrecibotitle;
TextView txtestado;
TextView txtfechavence;
TextView txtfacturaimporte;
public IMyViewHolderClicks mListener;
ItemReciboViewHolder(View itemView, IMyViewHolderClicks listener) {
super(itemView);
mListener = listener;
cv = (CardView)itemView.findViewById(R.id.itemlistadocard);
imgempresa = (ImageView)itemView.findViewById(R.id.imgempresa);
txtrecibotitle = (TextView)itemView.findViewById(R.id.txtfacturaname);
txtestado = (TextView)itemView.findViewById(R.id.txtfacturaestado);
txtfechavence = (TextView)itemView.findViewById(R.id.txtfacturafechavence);
txtfacturaimporte = (TextView)itemView.findViewById(R.id.txtfacturaimporte);
cv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v instanceof CardView){
mListener.onPotatoClick((CardView) v, this.getLayoutPosition());
}
}
public static interface IMyViewHolderClicks {
public void onPotatoClick(View caller, int posclick);
}
}
}