滚动到嵌套的recyclerview中的最后一项

时间:2016-11-11 23:21:35

标签: android scroll android-recyclerview

我正在构建一个应用程序,用户可以在其中提问并回复答案(类似于带有评论的Facebook帖子)。

对于回答问题,可以在recyclerview内的每个项目中嵌套回收的回复视图。

我正在尝试这样做,以便当用户点击“回复”时,屏幕将滚动到问题的最后一个答案(最后一个答案由以下屏幕截图中的蓝色字体表示):

Before clicking Reply

After clicking Reply

我认为这些是我要分享的代码的相关部分:

QuestionRecyclerViewAdapter.java:

public void onBindViewHolder(ViewHolder holder, int position) {
    Question question = mQuestionList.get(position);

    // Question
    int thumbnailSizeDp = 30;
    CircularImage circularImage = new CircularImage();
    circularImage.loadCircularImage(thumbnailSizeDp, question.getCreatorThumbnail(), mContext, holder.questionCreatorThumbnail);
    holder.questionCreator.setText(question.getCreator());
    Format formatter = new SimpleDateFormat("MMMM dd, yyyy");

    // Multiply by 1000 since Java is expecting milliseconds
    String dateCreated = formatter.format(new Date(Long.parseLong(question.getCreatedAt()) * 1000));
    holder.questionDate.setText(dateCreated);
    holder.questionContent.setText(question.getContent());

    // Last question
    if (position == mQuestionList.size() - 1) {
        holder.questionContent.setTextColor(Color.BLUE);
    }

    // Answers
    holder.rvAnswers.setLayoutManager(new LinearLayoutManager(mContext));
    AnswerRecyclerViewAdapter answerRecyclerViewAdapter = new AnswerRecyclerViewAdapter(mContext, question);
    holder.rvAnswers.setAdapter(answerRecyclerViewAdapter);
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView questionCreatorThumbnail;
    TextView questionCreator;
    TextView questionDate;
    TextView questionContent;
    TextView questionReply;
    RecyclerView rvAnswers;

    public ViewHolder(View v) {
        super(v);

        questionCreatorThumbnail = (ImageView) v.findViewById(R.id.iv_question_creator_thumbnail);
        questionCreator = (TextView) v.findViewById(R.id.tv_question_creator);
        questionDate = (TextView) v.findViewById(R.id.tv_question_date);
        questionContent = (TextView) v.findViewById(R.id.tv_question_content);
        questionReply = (TextView) v.findViewById(R.id.tv_reply);
        rvAnswers = (RecyclerView) v.findViewById(R.id.rv_all_answers);

        questionReply.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Question question = mQuestionList.get(getAdapterPosition());
        String questionId = question.getId();

        // Scroll to last answer
        LinearLayoutManager manager = (LinearLayoutManager) rvAnswers.getLayoutManager();
        manager.setStackFromEnd(true);
    }
}

在onClick方法中,我有:

LinearLayoutManager manager = (LinearLayoutManager) rvAnswers.getLayoutManager();
manager.setStackFromEnd(true);

在另一个用例中工作正常,但在这里不起作用,我认为它与recyclerview嵌套这一事实有关。

我也尝试使用scrollToPosition,但这也不起作用。

非常感谢帮助。谢谢。

0 个答案:

没有答案