回收器适配器项目错误

时间:2021-03-30 22:25:13

标签: java android android-recyclerview

我正在尝试制作一个类似于 instagram 的评论页面,但出现了一个荒谬的错误;

每个评论行都有一个 recyclerview,用户单击“显示回复”按钮我使 Recyclerview 可见,但在 10-11 个项目后,该项目的 recyclerview 也可见。

我错误地知道我的英语很差,但我需要帮助。

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 个答案:

答案 0 :(得分:0)

在模型类中创建一个布尔值以跟踪可见性

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) {

      }
    });
  }

});
相关问题