Recycler Adapter item bug

Viewed 58

I'm trying to make a comment page like instagram but I'm getting a ridiculous error;

Every comment row has one recyclerview and user click the "show replies" button I make Recyclerview visible but After 10-11 items, recyclerview of that item is also visible.

I know my English is terrible by mistake but i need help.

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 Answers

Create a boolean in your model class to keep track of visibility

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

      }
    });
  }

});
Related