android RecyclerView覆盖相同数据[不重复]

时间:2017-08-13 11:58:48

标签: android android-recyclerview

我正在检查 RecyclerView 的工作方式,我遇到了问题。数据在未滚动时按顺序加载到 RecyclerView ,但滚动时相同的数据被覆盖。所以我创建了一个 Arraylist 并将位置添加到 onBindViewHolder 中的 Arraylist ,并包含一个条件,检查该位置是否添加在 Arraylist 中。如果是,则将数据写入 TextView 。在if条件中,我包含一个Log来检查它是否正在加载。日志没有显示但是这次数据在向上滚动时混合在一起down。 RecyclerView 数据是一个数字的arraylist(0-149)。我在stackoverflow中检查了类似的问题,但它没有帮助我。

enter image description here

SampleRecyclerAdapter.java:

public class SampleRecyclerAdapter extends RecyclerView.Adapter<SampleRecyclerAdapter.RecyclerHolder> {
    View view;
    ArrayList arrayList;

    @Override
    public SampleRecyclerAdapter.RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_lyt,parent,false);
        if (arrayList==null){
            arrayList=new ArrayList();
        }
        return new RecyclerHolder(view);
    }

    @Override
    public void onBindViewHolder(SampleRecyclerAdapter.RecyclerHolder holder, int position) {
        Log.e("Position",Integer.toString(position));
        if (arrayList.size()>0&&!arrayList.contains(position)){
         holder.textView.setText(Data.getInstance().getData(position));
        arrayList.add(position);
            Log.e("Loaded","Loaded");
        }
         else if (arrayList.size()==0){
            holder.textView.setText(Data.getInstance().getData(position));
          arrayList.add(position);
            Log.e("Loaded","Loaded");
        }
    }

    @Override
    public int getItemCount() {
        return Data.getInstance().arrayList.size();
    }

    public class RecyclerHolder extends RecyclerView.ViewHolder{
        TextView textView;
    public RecyclerHolder(View itemView) {
        super(itemView);
        textView=(TextView) itemView.findViewById(R.id.txt);
    }
}
}

1 个答案:

答案 0 :(得分:1)

RecyclerView顾名思义重用视图,因此不必重新创建用户可见的所有视图。

试试这个,我还没有测试过,但它应该可以工作:

public class SampleRecyclerAdapter extends RecyclerView.Adapter<SampleRecyclerAdapter.RecyclerHolder> {
    View view;
    FragmentActivity fragActivity;
    ArrayList<String> arrayList;    

    public SampleRecyclerAdapter (FragmentActivity fragActivity, ArrayList<String> arrayList) {
        this.fragActivity = fragActivity;
        this.arrayList = arrayList;
    }   

    @Override
    public SampleRecyclerAdapter.RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_lyt,parent,false);    

        return new RecyclerHolder(view);
    }   

    public void dataSetChanged() {
        notifyDataSetChanged();
    }   

    @Override
    public void onBindViewHolder(SampleRecyclerAdapter.RecyclerHolder holder, int position) {
        holder.textView.setText(arrayList.get(position));   

    }   

    @Override
    public int getItemCount() {
        return arrayList.size();
    }   

    public class RecyclerHolder extends RecyclerView.ViewHolder{
        TextView textView;
        public RecyclerHolder(View itemView) {
            super(itemView);
            textView=(TextView) itemView.findViewById(R.id.txt);
        }
    }
}