Recyclerview滚动时更改项目

时间:2016-04-14 03:40:13

标签: android android-recyclerview

有人可以向我解释,问题是什么?

当我向下滚动然后回到顶部时,我的项目已更改为列表中的其他项目,如果我再次滚动,则会再次更改项目。

这是我的适配器。

public class AcademicAdapter extends RecyclerView.Adapter<AcademicAdapter.AcademicVH> {
    private LayoutInflater inflater;
    private List<AcademicStatus> data;

public AcademicAdapter(List<AcademicStatus> data) {
    this.data = data;
}

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

    return new AcademicVH(view);
}

@Override
public void onBindViewHolder(AcademicVH holder, int position) {
    View view = inflater.inflate(R.layout.row_container_academic_level, holder.containerData, false);

    TextView txtSubject = (TextView) view.findViewById(R.id.txtSubjectValue);
    TextView txtGroup = (TextView) view.findViewById(R.id.txtGroupValue);
    TextView txtStatus = (TextView) view.findViewById(R.id.txtStatusValue);
    TextView txtFinalNote = (TextView) view.findViewById(R.id.txtFinalNoteValue);
    TextView txtYear = (TextView) view.findViewById(R.id.txtYearValue);

    txtSubject.setText(data.get(position).getSubject());
    txtGroup.setText(data.get(position).getGroup());
    txtStatus.setText(data.get(position).getStatus());
    txtFinalNote.setText(data.get(position).getFinalNote());
    txtYear.setText(data.get(position).getAcademicYear());

    holder.containerData.addView(view);
}

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

public static class AcademicVH extends RecyclerView.ViewHolder {
    private LinearLayout containerData;

    public AcademicVH(View itemView) {
        super(itemView);

        containerData = (LinearLayout) itemView.findViewById(R.id.containerData);
    }
}
}

6 个答案:

答案 0 :(得分:8)

尝试在RecyclerView.ViewHolder上初始化您的视图,并在onCreateViewHolder()上对您的布局进行充气,并且不要再次对onBindViewHolder()中的布局进行充气。

public class AcademicAdapter extends RecyclerView.Adapter<AcademicAdapter.AcademicVH> {
    private LayoutInflater inflater;
    private List<AcademicStatus> data;

public AcademicAdapter(List<AcademicStatus> data) {
    this.data = data;
}

@Override
public AcademicVH onCreateViewHolder(ViewGroup parent, int viewType) {

    View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_academic_level, parent, false);
    AcademicVH mViewHold = new ViewHolder(mView);
    return mViewHold;
}

@Override
public void onBindViewHolder(AcademicVH holder, int position) {
     final AcademicStatus mItems=this.data.get(position);


    holder.txtSubject.setText(mItems.getSubject());
    holder.txtGroup.setText(mItems.getGroup());
    holder.txtStatus.setText(mItems.getStatus());
    holder.txtFinalNote.setText(mItems.getFinalNote());
    holder.txtYear.setText(mItems.getAcademicYear());

}

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

public static class AcademicVH extends RecyclerView.ViewHolder {
    public LinearLayout containerData;
    public TextView txtSubject,txtGroup,txtStatus,txtFinalNote,txtYear;

    public AcademicVH(View itemView) {
        super(itemView);

        containerData = (LinearLayout) itemView.findViewById(R.id.containerData);
        txtSubject = (TextView) itemView.findViewById(R.id.txtSubjectValue);
        txtGroup = (TextView) itemView.findViewById(R.id.txtGroupValue);
        txtStatus = (TextView) itemView.findViewById(R.id.txtStatusValue);
        txtFinalNote = (TextView) itemView.findViewById(R.id.txtFinalNoteValue);
        txtYear = (TextView) itemView.findViewById(R.id.txtYearValue);
    }
}
}

答案 1 :(得分:4)

您在错误的位置定义项目,在view holder内定义您的视图,如下所示

public static class AcademicVH extends RecyclerView.ViewHolder {
private LinearLayout containerData;

public AcademicVH(View itemView) {
    super(itemView);
    TextView txtSubject = (TextView) view.findViewById(R.id.txtSubjectValue);
    TextView txtGroup = (TextView) view.findViewById(R.id.txtGroupValue);
    TextView txtStatus = (TextView) view.findViewById(R.id.txtStatusValue);
    TextView txtFinalNote = (TextView) view.findViewById(R.id.txtFinalNoteValue);
    TextView txtYear = (TextView) view.findViewById(R.id.txtYearValue);
    containerData = (LinearLayout) itemView.findViewById(R.id.containerData);
    }
}

另一个技巧,当你在onBindViewHolder中使用condition(if)时,你应该用(else)来完成它。

答案 2 :(得分:1)

只需将recylerView放入xml的NestedScroll视图中,然后添加属性nestedScrollingEnabled = false。

然后在适配器onBindViewHolder上添加以下行

最后的AcademicVH viewHolder =(AcademicVH)holder;

将此viewHolder对象与您的视图一起用于setText或执行任何类型的Click事件。

例如viewHolder.txtSubject.setText(“ Example”);

答案 3 :(得分:1)

Recyclerview shuffling issue understanding of the concept behind

  @Override
  public void onBindViewHolder(final MyViewHolder holder, int position) {
      holder.setIsRecyclable(false);
  }

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

 @Override
 public int getItemViewType(int position) 
 {
        return position;
 }

添加setHasStableIds(true);在您的适配器构造函数中,并覆盖这两个      适配器中的方法。

答案 4 :(得分:0)

问题在于图书馆 compile 'com.android.support:recyclerview-v7:25.0.0'。 尝试使用 compile 'com.android.support:recyclerview-v7:23.1.1'

答案 5 :(得分:0)

在您的onBindViewHolder中添加holder.setIsRecyclable(false);

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.setIsRecyclable(false);
}