0

I wanted to make a Comment & Reply function for social media app

something like instagram enter image description here

is there a way to have a nested recyclerview by using FirestoreRecyclerAdapter?

here is my plan

enter image description here

and here is the structure of my FirestoreRecyclerAdapter

    homeRecycler = findViewById(R.id.homeRecycler);
    linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
    homeRecycler.setLayoutManager(linearLayoutManager);

    homeRecycler.setItemAnimator(null);


    StorageReference profileRef = storageReference.child("ProfileImage/"+fAuth.getCurrentUser().getUid()+"/profile.jpg");
    profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            Picasso.get().load(uri).into(prof_img);
        }
    });

    Query query = fStore.collection("Post")
            .orderBy("homePostDate", Query.Direction.DESCENDING)
            .limit(50);


    FirestoreRecyclerOptions<HomeModel> options = new FirestoreRecyclerOptions.Builder<HomeModel>()
            .setQuery(query, HomeModel.class)
            .build();

    swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void onRefresh() {
            adapter.notifyDataSetChanged();
            swipeRefresh.setRefreshing(false);
        }
    });

    adapter = new FirestoreRecyclerAdapter<HomeModel, ViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull HomeModel model) {
            holder.bind(model);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int i) {
            View view = LayoutInflater.from(group.getContext())
                    .inflate(R.layout.home_each, group,false);
            return new ViewHolder(view);
        }
    };

    homeRecycler.setAdapter(adapter);

}

class ViewHolder extends RecyclerView.ViewHolder{
    TextView author, title, body, authorUid, docId, timestamp, upvotecount, downvotecount;
    Button addcoment;
    ToggleButton upvote, downvote;
    ImageView prof_img;
    HomeModel model;
    RecyclerView replyRecycler;

public ViewHolder(@NonNull View itemView) {...}

public void bind(HomeModel homeModel) {...}



edit:

I tried just duplicating everything within the firestorerecycleradapter viewholder but couldnt make it work and dont even know if this is the proper way..

commentRecycler = findViewById(R.id.commentRecycler);
        linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        commentRecycler.setLayoutManager(linearLayoutManager);

        commentRecycler.setItemAnimator(null);

        Query query = fStore.collection("Post")
                .document(pdocId.getText().toString()).collection("comment")
                .orderBy("commentPostDate", Query.Direction.DESCENDING)
                .limit(50);

        FirestoreRecyclerOptions<CommentModel> options = new FirestoreRecyclerOptions.Builder<CommentModel>()
                .setQuery(query, CommentModel.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<CommentModel, ViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull CommentModel model) {
                holder.bind(model);

                linearLayoutManager2 = new LinearLayoutManager(HomeView.this, RecyclerView.VERTICAL, false);
                holder.replyRecycler.setLayoutManager(linearLayoutManager2);

                holder.replyRecycler.setItemAnimator(null);

                Query query2 = fStore.collection("Post")
                        .document(pdocId.getText().toString()).collection("comment")
                        .document(model.docId).collection("reply")
                        .orderBy("commentPostDate", Query.Direction.DESCENDING)
                        .limit(50);

                FirestoreRecyclerOptions<ReplyModel> options2 = new FirestoreRecyclerOptions.Builder<ReplyModel>()
                        .setQuery(query2, ReplyModel.class)
                        .build();

                adapter2 = new FirestoreRecyclerAdapter<ReplyModel, ViewHolder2>(options2) {
                    @Override
                    protected void onBindViewHolder(@NonNull ViewHolder2 holder, int position, @NonNull ReplyModel model) {
                        holder.bind(model);
                        Toast.makeText(HomeView.this, "this is test", Toast.LENGTH_SHORT).show();
                    }

                    @NonNull
                    @Override
                    public ViewHolder2 onCreateViewHolder(@NonNull ViewGroup group, int i) {
                        View view = LayoutInflater.from(group.getContext())
                                .inflate(R.layout.comment_each, group,false);
                        return new ViewHolder2(view);
                    }
                };

                holder.replyRecycler.setAdapter(adapter2);

            }

            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int i) {
                View view = LayoutInflater.from(group.getContext())
                        .inflate(R.layout.comment_each, group,false);
                return new ViewHolder(view);
            }
        };

        commentRecycler.setAdapter(adapter);

and then added another viewholder class

 class ViewHolder extends RecyclerView.ViewHolder{
        TextView author, body, authorUid, docId, timestamp, pReply, upvotecount, downvotecount;
        TextInputLayout tilpReply;
        ImageView prof_img;
        Chip replyChip;
        ToggleButton upvote, downvote;
        ToggleButton replypop;
        Button replyBtn;
        CommentModel model;
        RecyclerView replyRecycler;

        public ViewHolder(@NonNull View itemView){...}
        public void bind(CommentModel commentModel){...}
    }
class ViewHolder2 extends RecyclerView.ViewHolder{
        TextView author, body, authorUid, docId, timestamp, pReply, upvotecount, downvotecount;
        TextInputLayout tilpReply;
        ImageView prof_img;
        Chip replyChip;
        ToggleButton upvote, downvote;
        ToggleButton replypop;
        Button replyBtn;
        CommentModel model;
        RecyclerView replyRecycler;

        public ViewHolder2(@NonNull View itemView){...}
        public void bind(CommentModel commentModel){...}
czrest
  • 1
  • 1
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Nov 21 '22 at 14:42
  • the code Ive shared is for the recyclerview of the parent which is the Comments.. I want to add a nested recyclerview below each comments which will be the replies but dont exactly know how since theres not much I could gather to make this work on firestore recycler adapter. Ive tried just duplicating the full firestorerecycleradapter Ive done inside the viewholder since inside the viewholder is where my second recyclerview is but I couldnt make it work – czrest Nov 21 '22 at 22:53

0 Answers0