RecyclerView onclick返回错误的项目

时间:2017-12-03 10:38:46

标签: android android-recyclerview recycler-adapter

我使用运行良好的RecyclerView。除了当我单击元素1时,它向下滚动时返回元素3。当我向上滚动并单击元素4时,它返回元素3.

这是我的适配器

public class StarRVAdapter extends RecyclerView.Adapter<StarRVViewHolder>{

private Context c;
private ArrayList<Questions> questionEntries;
Questions qe;
public StarRVAdapter(Context c, ArrayList<Questions> questionEntries,Questions qe) {
    this.c = c;
    this.qe = qe;
    this.questionEntries = questionEntries;
}
@Override
public StarRVViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(c).inflate(R.layout.actu_list_item,parent,false);
    return new StarRVViewHolder(v,c);
}
@Override
public void onBindViewHolder(StarRVViewHolder holder, int position) {
     qe = (Questions)this.getItem(position);
    holder.setDate(qe.getQuestion_date());
    holder.setContent(qe.getQuestion_content());
    holder.setImg(qe.getImgUrl());
    holder.setTitle(qe.getQuestion_title());
    holder.setAsker(qe.getQuestion_username());
    holder.setIsRecyclable(true);

//this is My OnclickListener

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            GoToView();

        }
    });
}
@Override
public int getItemCount() {
    return questionEntries.size();
}
public Object getItem(int position) {
    return questionEntries.get(position);
}
private void GoToView() {
    Intent intent = new Intent(c,QuestionView.class);
    Bundle b = new Bundle();
    intent.putExtra("QuestionRef",qe.getTag_id());
    intent.putExtra("ContentRef",qe.getQuestion_content());
    intent.putExtra("TitleRef",qe.getQuestion_title());
    intent.putExtra("ImgRef",qe.getImgUrl());
    intent.putExtra("UsernameRef",qe.getQuestion_username());
    intent.putExtra("DateRef",qe.getQuestion_date());
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(c,intent, b);
//This is returning another item
}
}

请帮帮我

2 个答案:

答案 0 :(得分:2)

您正在使用错误的Questions对象。在您的点击本身中分配它。

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override 
    public void onClick(View view) {
        GoToView(holder.getAdapterPosition()); // passing position

    } 
}); 

相应地更改GoToView的签名:

private void GoToView(int position) { 
    Questions qe = (Questions)this.getItem(position);
    // Cool stuff with qe
}

无需将其保留为实例变量

答案 1 :(得分:1)

final Questions qe = (Questions)this.getItem(position);
holder.setDate(qe.getQuestion_date());
holder.setContent(qe.getQuestion_content());
holder.setImg(qe.getImgUrl());
holder.setTitle(qe.getQuestion_title());
holder.setAsker(qe.getQuestion_username());
holder.setIsRecyclable(true);

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        GoToView(qe);

    }
});

并更改您的

private void GoToView(Questions qe) {
    Intent intent = new Intent(c,QuestionView.class);
    Bundle b = new Bundle();
    intent.putExtra("QuestionRef",qe.getTag_id());
    intent.putExtra("ContentRef",qe.getQuestion_content());
    intent.putExtra("TitleRef",qe.getQuestion_title());
    intent.putExtra("ImgRef",qe.getImgUrl());
    intent.putExtra("UsernameRef",qe.getQuestion_username());
    intent.putExtra("DateRef",qe.getQuestion_date());
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(c,intent, b);
//This is returning another item
}