0

I am trying to add a button in the end of my recycler view and make that add button appear as a fragment. Previously it worked. But I don't know why it doesn't work at the moment. I've changed it into Frame Layout according to google and some StackOverflow question but it is still not showing up. When I put a toast inside the onClick function, the toast showed up. It's weird. Is it wrong somewhere else?

MessageAdapter.java

@Override
    protected void onBindViewHolder(final MessagesHolder holder, int position, Messages model) {
        holder.textViewMessages.setText(model.getMessages());
        holder.textViewUser.setText(model.getUser());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd yyyy, hh:mm a");
        String testtimeStamp = (model.getTimePosted().toDate().toString());
        String testtimeStamp2 = simpleDateFormat.format(new Date(testtimeStamp));
        holder.timeStamp.setText(testtimeStamp2);
        holder.bAdd.setVisibility(View.GONE);
        holder.container.setVisibility(View.GONE);

        //Compare size and add button at buttom of view
        if(position==getItemCount()-1){
            holder.bAdd.setVisibility(View.VISIBLE);
        }
        holder.bAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.container.setVisibility(View.VISIBLE);
                AppCompatActivity activity = (AppCompatActivity)v.getContext();
                Fragment replyFrag = new AddReplyFragment();
                Bundle extras = new Bundle();
                extras.putString("USER","Test_User");
                replyFrag.setArguments(extras);
                replyFrag.setArguments(extrasFromTitle);
                activity.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.replyFragment,replyFrag).commit();
            }
        });
    }

    @NonNull
    @Override
    public MessagesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforuminterface,parent,false);
        EmojiCompat.init(new BundledEmojiCompatConfig(view.getContext()));
        return new MessagesHolder(view);
    }

    class MessagesHolder extends RecyclerView.ViewHolder{
        EmojiTextView textViewMessages;
        EmojiTextView textViewUser;
        TextView timeStamp;
        Button bAdd;
        FrameLayout container;
        public MessagesHolder(@NonNull View itemView) {
            super(itemView);
            textViewMessages = itemView.findViewById(R.id.tvDescription_reply);
            textViewUser = itemView.findViewById(R.id.tvUsername);
            timeStamp = itemView.findViewById(R.id.tvTimestampMessages);
            bAdd = itemView.findViewById(R.id.bAdd);
            container = itemView.findViewById(R.id.replyFragment);
        }
    }

AddReplyFragment.java

public class AddReplyFragment extends Fragment {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    Button bAddReply;
    EditText reply;
    Timestamp now = Timestamp.now();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_add_reply_fragment,container,false);
        final String username = getArguments().getString("USER");
        final String forum_title = getArguments().getString("TITLE");
        final String forum_type = getArguments().getString("FORUM_TYPE");
        final String forum_id = getArguments().getString("FORUM_ID");

        reply = (EditText)view.findViewById(R.id.etReply);
        bAddReply = (Button)view.findViewById(R.id.bAddReply);
        String userReply = reply.getText().toString();
        final Map<String, Object> reply = new HashMap<>();
        reply.put("messages",userReply);
        reply.put("timePosted",now);
        reply.put("user",username);

        bAddReply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.collection(forum_type).document(forum_id).collection("messages").add(reply);
            }
        });

        return view;
    }
}

cardviewforuminterface.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="4">

    <LinearLayout
        android:id="@+id/reply_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <androidx.emoji.widget.EmojiTextView
            android:id="@+id/tvDescription_reply"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="description"
            android:textSize="15sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/users_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="3">

        <androidx.emoji.widget.EmojiTextView
            android:id="@+id/tvUsername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="name"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvTimestampMessages"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="timestamp"
            android:textIsSelectable="false"
            android:textSize="15sp" />

    </LinearLayout>

    <Button
        android:id="@+id/bAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Add Reply" />

    <FrameLayout
        android:id="@+id/replyFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </FrameLayout>

</LinearLayout>

activity_add_reply_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical"
    tools:context=".Fragment.AddReplyFragment">

    <TextView
        android:id="@+id/tvReply"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center"
        android:padding="10dp"
        android:text="Your Reply"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/etReply"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="Type your reply here"
        android:inputType="textPersonName"
        android:padding="10dp" />

    <Button
        android:id="@+id/bAddReply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:text="Submit Reply" />
</FrameLayout>
Kdon Patel
  • 107
  • 1
  • 15

1 Answers1

0

The code I added in my adapter.

holder.container.setId(newContainerId);
final int newContainerId = getUniqueId();

public int getUniqueId(){
        return (int) SystemClock.currentThreadTimeMillis();
    }

This is my last code

public class MessagesAdapter extends FirestoreRecyclerAdapter<Messages, MessagesAdapter.MessagesHolder> {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    MessagesAdapter context;
    Bundle extrasFromInterface;

    public MessagesAdapter(@NonNull FirestoreRecyclerOptions<Messages> options,Bundle bundle) {
        super(options);
        extrasFromInterface = bundle;
    }

    @Override
    protected void onBindViewHolder(final MessagesHolder holder, int position, Messages model) {
        final int newContainerId = getUniqueId();

        holder.textViewMessages.setText(model.getMessages());
        holder.textViewUser.setText(model.getUser());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd yyyy, hh:mm a");
        String uncovertedTimeStamp = (model.getTimePosted().toDate().toString());
        String ConvertedTimeStamp = simpleDateFormat.format(new Date(uncovertedTimeStamp));
        holder.timeStamp.setText(ConvertedTimeStamp);
        holder.bAdd.setVisibility(View.GONE);
        holder.container.setVisibility(View.GONE);



        //Compare size and add button at buttom of view
        if(position==getItemCount()-1){
            holder.bAdd.setVisibility(View.VISIBLE);
        }
        holder.bAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.container.setId(newContainerId);
                holder.container.setVisibility(View.VISIBLE);
                AppCompatActivity activity = (AppCompatActivity)v.getContext();
                Fragment replyFrag = new AddReplyFragment();
                Bundle extras = new Bundle();
                extras.putString("USER","Test_User");
                replyFrag.setArguments(extras);
                replyFrag.setArguments(extrasFromInterface);
                activity.getSupportFragmentManager().beginTransaction()
                        .replace(newContainerId,replyFrag).commit();
            }
        });
    }

    public int getUniqueId(){
        return (int) SystemClock.currentThreadTimeMillis();
    }

    @NonNull
    @Override
    public MessagesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforuminterface,parent,false);
        EmojiCompat.init(new BundledEmojiCompatConfig(view.getContext()));
        context = this;
        return new MessagesHolder(view);
    }

    class MessagesHolder extends RecyclerView.ViewHolder{
        EmojiTextView textViewMessages;
        EmojiTextView textViewUser;
        TextView timeStamp;
        Button bAdd;

        FrameLayout container;
        public MessagesHolder(@NonNull View itemView) {
            super(itemView);
            textViewMessages = itemView.findViewById(R.id.tvDescription_reply);
            textViewUser = itemView.findViewById(R.id.tvUsername);
            timeStamp = itemView.findViewById(R.id.tvTimestampMessages);
            bAdd = itemView.findViewById(R.id.bAdd);
            container = itemView.findViewById(R.id.replyFragment);
        }
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }
}