我有一个正在使用的RecyclerView
。我以前使用过RecyclerView
,但从未遇到过这个问题。
当我向上或向下滚动时,一些项目会消失,而一些消失的项目会再次出现在底部。
代码:
ViewHolder
:
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txt;
public ViewHolder(View view) {
super(view);
txt = (TextView) view.findViewById(R.id.txt);
}
}
适配器
:
public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
private final Activity activity;
private final ArrayList<HashMap<String, String>> mItems;
public MyAdapter (Activity activity, ArrayList<HashMap<String, String>> mItems) {
this.activity = activity;
this.mItems= mItems;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(activity).inflate(R.layout.items, viewGroup, false));
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
HashMap<String, String> item = mItems.get(position);
String info = item.get("info ");
if (info!= null) {
viewHolder.txt.setText(info);
} else {
viewHolder.txt.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return (null != mItems? mItems.size() : 0);
}
}