Ошибка элемента адаптера ресайклера

#java #android #android-recyclerview

Вопрос:

Я пытаюсь создать страницу комментариев, как в instagram, но получаю нелепую ошибку;

В каждой строке комментария есть один просмотр вторсырья, и пользователь нажимает кнопку «показать ответы», я делаю видимым просмотр вторсырья, но после 10-11 элементов также отображается просмотр этого элемента.

Я знаю, что мой английский ужасен по ошибке, но мне нужна помощь.

 holder.replyCount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(main,"sdf",Toast.LENGTH_LONG).show();
            new Comment().getCommentResponse(getCommentModels.get(position).getId(), new IMainResponse() {
                @Override
                public <T> void Succsess(Response<T> _response) {
                    getCommentModel = (List<getCommentModel>) _response.body();
                    AdapterComment adapterComment = new AdapterComment(main, getCommentModel);
                    
                    holder.recyclerView.setVisibility(View.VISIBLE);
                    holder.recyclerView.setAdapter(adapterComment);
                    holder.recyclerView.setHasFixedSize(true);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(main);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    holder.recyclerView.setLayoutManager(linearLayoutManager);
                }

                @Override
                public void Error(ErrorModel _eresponse) {

                }
            });
        }

    });
 

Ответ №1:

Создайте логическое значение в классе модели, чтобы отслеживать видимость

 if (getCommentModels.get(position).isVisible()) {
  holder.recyclerView.setVisibility(View.VISIBLE);
} else {
  holder.recyclerView.setVisibility(View.GONE);
}
holder.replyCount.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    getCommentModels.get(position).setVisible(!getCommentModels.get(position).isVisible());
    notifyItemChanged(position);
    new Comment().getCommentResponse(getCommentModels.get(position).getId(), new IMainResponse() {
      @Override
      public <T> void Succsess(Response<T> _response) {
        getCommentModel = (List<getCommentModel>) _response.body();
        AdapterComment adapterComment = new AdapterComment(main, getCommentModel);

        holder.recyclerView.setAdapter(adapterComment);
        holder.recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(main);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.recyclerView.setLayoutManager(linearLayoutManager);
      }

      @Override
      public void Error(ErrorModel _eresponse) {

      }
    });
  }

});