膨胀具有嵌套RecyclerView的布局,需要再次膨胀

时间:2018-11-22 15:00:40

标签: android firebase firebase-realtime-database android-recyclerview

我有一个初始的recyclerview,用于查看注释,我可以通过扩大布局从Firebase数据库中检索这些注释。夸张的布局还有另一个recyclerview,用于评论评论。目前,我能够在数据库中成功保存对评论的答复。我的问题是我如何才能在“ Comments inflated layout” recyclerview中检索对评论的答复。回复的recyclerview位于注释的第一个初始放大布局内。

This image recyclerview is for the comments

this is the inflated layout for comments with the recyclerview for replies

this is the supposed inflated layout which should be inflated for the replies

Database Structure

下面是我的代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
 firebaseAuth = FirebaseAuth.getInstance();
    currentUserID = firebaseAuth.getCurrentUser().getUid();
    UserBids = FirebaseDatabase.getInstance().getReference().child("All Bids");
    UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");


    CommentsRecyclerview = findViewById(R.id.Comments_recycler_view);
    CommentsRecyclerview.setHasFixedSize(true);
    CommentsRecyclerview.setNestedScrollingEnabled(false);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    CommentsRecyclerview.setLayoutManager(linearLayoutManager);

@Override
protected void onResume() {
    super.onResume();


    WorkingDataForDisplayingDetails.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                Object userid = dataSnapshot.child("currentUserid").getValue();
                if (userid != null) {
                    postedUserID = userid.toString();


                    Query query = UserBids.child("Comments").child(PostKey).child(postedUserID);
                    FirebaseRecyclerOptions<Comments> options = new FirebaseRecyclerOptions.Builder<Comments>()
                            .setQuery(query, Comments.class)
                            .build();

                    final FirebaseRecyclerAdapter<Comments, MyViewHolder> adapter
                            = new FirebaseRecyclerAdapter<Comments, MyViewHolder>(options) {
                        @Override
                        protected void onBindViewHolder(@NonNull final MyViewHolder holder, int position, @NonNull final Comments model) {

                            UserBids.child(PostKey).child(postedUserID).addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                    if (dataSnapshot.hasChild(model.getCurrentuserid())) {
                                        holder.bidder.setText("Bidder");
                                    }
                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError databaseError) {

                                }
                            });


                            holder.name.setText(model.getUserName());
                            holder.comment.setText(model.getComment());
                            Picasso.get().load(model.getImage()).placeholder(R.drawable.profile).into(holder.userimg);

                            holder.reply.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    postKeyForReply = getRef(holder.getAdapterPosition()).getKey();

                                    Log.d(TAG, "onClick: inside reply onclick");

                                    holder.replyCommentInput.setVisibility(View.VISIBLE);
                                    holder.replyCommentSendButton.setVisibility(View.VISIBLE);

                                    holder.replyCommentInput.setFocusableInTouchMode(true);
                                    holder.replyCommentInput.requestFocus();

                                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                    imm.showSoftInput(holder.replyCommentInput, InputMethodManager.SHOW_FORCED);

                                }
                            });


                            holder.replyCommentSendButton.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String checkValidReply = holder.replyCommentInput.getText().toString();
                                    if (TextUtils.isEmpty(checkValidReply)) {
                                        Toast.makeText(AllUsersComments.this, "Please write something", Toast.LENGTH_SHORT).show();
                                    } else {

                                        final String randomKeyForReply = UserBids.push().getKey();

                                        HashMap replyMap = new HashMap();
                                        replyMap.put("currentuserid", currentUserID);
                                        replyMap.put("currentusername", saveFirstNameforReplier);
                                        replyMap.put("reply", checkValidReply);

                                        UserBids.child("Reply").child(postKeyForReply)
                                                .child(currentUserID).child(randomKeyForReply).updateChildren(replyMap)
                                                .addOnCompleteListener(new OnCompleteListener() {
                                                    @Override
                                                    public void onComplete(@NonNull Task task) {
                                                        if (task.isSuccessful()) {
                                                            Toast.makeText(AllUsersComments.this, "Comment Posted", Toast.LENGTH_SHORT).show();
                                                            holder.replyCommentInput.setText("");
                                                            SharedPreferences sharedPref =
                                                                    PreferenceManager
                                                                            .getDefaultSharedPreferences(AllUsersComments.this);
                                                            SharedPreferences.Editor editor = sharedPref.edit();

                                                            editor.putString("postkeyReply", postKeyForReply);
                                                            editor.putString("currentuserid", currentUserID);
                                                            editor.commit();

                                                        } else {
                                                            Toast.makeText(AllUsersComments.this, "An Error has occurred", Toast.LENGTH_SHORT).show();
                                                        }
                                                    }
                                                });

                                    }

                                }
                            });



                            String givenDateString = model.getDate();
                            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy, HH:mm");
                            sdf.setTimeZone(TimeZone.getTimeZone("Asia/Bahrain"));

                            long timeInMilliseconds = 0;
                            try {
                                Date mDate = sdf.parse(givenDateString);
                                timeInMilliseconds = mDate.getTime();
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            final CharSequence ch = DateUtils.getRelativeTimeSpanString(timeInMilliseconds, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS);
                            holder.commentTime.setText(ch);

                        }

                        @NonNull
                        @Override
                        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_section_individual_row_layout, parent, false);
                            MyViewHolder viewHolder = new MyViewHolder(view);
                            return viewHolder;
                        }
                    };
                    CommentsRecyclerview.setAdapter(adapter);
                    adapter.startListening();


                }
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });



}


public static class MyViewHolder extends RecyclerView.ViewHolder {

    public AppCompatTextView name;
    public AppCompatTextView reply;
    public AppCompatImageView replyCommentSendButton;
    public AppCompatEditText replyCommentInput;
    public AppCompatTextView bidder;
    public AppCompatTextView comment;
    public AppCompatTextView commentTime;
    public CircleImageView userimg;

    public MyViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.CommentersName);
        reply = itemView.findViewById(R.id.ReplyButton);
        bidder = itemView.findViewById(R.id.checkBidder);
        comment = itemView.findViewById(R.id.CommentersComment);
        userimg = itemView.findViewById(R.id.UserImageInComment);
        commentTime = itemView.findViewById(R.id.commentTimeAgo);
        replyCommentSendButton = itemView.findViewById(R.id.replyCommentButton);
        replyCommentInput = itemView.findViewById(R.id.replyCommentInputInReply);


    }
}

0 个答案:

没有答案