如何阻止我的RecyclerView重用视图

时间:2016-04-24 17:35:16

标签: android android-arrayadapter android-recyclerview

我的问题是:当我滚动页面时,一些视图将出现在一个新的位置,并非所有来自适配器的内容都会显示,但只显示某些类型。 下面是我的适配器类:

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.TextView;
import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private List<BabyFigure> rinfoList ;


public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView id,date,time,type,quantity,demo;

    public MyViewHolder(View view) {
        super(view);
        id = (TextView) view.findViewById(R.id.recycle_view_id);
        date = (TextView) view.findViewById(R.id.recycle_view_date);
        time = (TextView) view.findViewById(R.id.recycle_view_time);
        type = (TextView) view.findViewById(R.id.recycle_view_type);
        quantity = (TextView) view.findViewById(R.id.recycle_view_quantity);
        demo = (TextView) view.findViewById(R.id.recycle_view_demo);
    }
}

public RecyclerViewAdapter(List<BabyFigure> infoList) {
    this.rinfoList = infoList;
    setHasStableIds(true);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_content,
            parent,false);
    return new MyViewHolder(itemView);
}

@Override
public int getItemCount() {
    Log.d(getClass().getSimpleName(),String.valueOf(rinfoList.size()));
    return rinfoList.size();
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

@Override
public void onBindViewHolder(MyViewHolder holder,int position) {
    BabyFigure babyFigure = rinfoList.get(position);
    holder.quantity.setText(String.valueOf(babyFigure.getQuantity()));
    holder.id.setText(String.valueOf(babyFigure.getId()));
    holder.demo.setText(babyFigure.getDemo());
    holder.date.setText(babyFigure.getDate());
    holder.time.setText(babyFigure.getTime());
    holder.type.setText(babyFigure.getType());
}

}

非常感谢

1 个答案:

答案 0 :(得分:3)

问题是你没有实现getItemId()。您正在调用超类实现,它只为所有位置返回相同的值。

至少,你可以这样做:

@Override
public long getItemId(int position) {
    return position;
}

但是,如果您的数据项实际上具有有意义的ID,则应该返回该项的ID。

@Override
public long getItemId(int position) {
    return rinfoList.get(position).getId();
}