How to add a dynamic card view on top of recycler view in android
<p>I am creating an application which displays posts. These posts have comments,
when a user logs in to the application I am querying database and I am displaying the comments. But when a user enters a new comment, I have display it dynamically to the user and then later When the user logs in again I will query the comments from database. How can I display a card view dynamically when user clicks submit button? all suggestions are welcome.</p>

<p>My Adapter Class:</p>

<pre><code>public class ToadlineCommentAdapter extends RecyclerView.Adapter&lt;ToadlineCommentAdapter.MyViewHolder&gt; {

    private ClickListener clickListener;
    private SwipeRefreshLayout.OnRefreshListener clickListener1;
    private LayoutInflater inflater;
    Context mContext;
    List&lt;CommentDataStore&gt; data = Collections.EMPTY_LIST;

    public ToadlineCommentAdapter(Context context, List&lt;CommentDataStore&gt; data) {
        inflater = LayoutInflater.from(context);
        this.data = data;
        mContext = context;


    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.custom_comment_layout, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {

        return data.size();
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setClickListener1(SwipeRefreshLayout.OnRefreshListener clickListener1) {
        this.clickListener1 = clickListener1;
    }


    // View Holder object for Recycler View
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

        TextView  commentUserName, commentDays, commentDescription, commentAgreeCount, commentDisAgreeCount;
        ImageView commentImage;
        public MyViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener((View.OnClickListener) this);
            commentImage = (ImageView) itemView.findViewById(R.id.textViewNoOfDays);
            commentDescription = (TextView) itemView.findViewById(R.id.textViewCommentDescription);
            commentAgreeCount = (TextView) itemView.findViewById(R.id.textViewAgreeCount);
            commentDisAgreeCount = (TextView) itemView.findViewById(R.id.textViewDisAgreeCount);
        }

        @Override
        public void onClick(View view) {

            if (clickListener != null) {
                clickListener.itemClicked(view, getPosition());
            }
        }

        @Override
        public void onRefresh() {

            onClick(itemView);

        }
    }


    public interface ClickListener {

        public void itemClicked(View view, int position);
    }
}
</code></pre>

<p>Let  me know if you need more information.</p>

<p>My Activity:</p>

<pre><code>    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_comment);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerViewTimeline);
    mComments = (EditText) findViewById(R.id.textViewNewCommentDescription);
    mProfileImage = (ImageView) findViewById(R.id.imageViewUserNewComment);
    mSubmit = (Button) findViewById(R.id.buttonCommentSubmit);

    commentProfileImageHashMap = (HashMap&lt;String, ArrayList&lt;String&gt;&gt;) getIntent().getExtras().getSerializable("CommentHashMapProfileImage");
    commentDaysHashMap = (HashMap&lt;String, ArrayList&lt;String&gt;&gt;) getIntent().getExtras().getSerializable("CommentHashMapDays");
    commentDescriptionHashMap = (HashMap&lt;String, ArrayList&lt;String&gt;&gt;) getIntent().getExtras().getSerializable("CommentHashMapDescription");
    commentUserNameHashMap = (HashMap&lt;String, ArrayList&lt;String&gt;&gt;) getIntent().getExtras().getSerializable("CommentHashMapUserName");
    commentAgreeCountHashMap = (HashMap&lt;String, ArrayList&lt;String&gt;&gt;) getIntent().getExtras().getSerializable("CommentHasMapAgree");
    commentDisAgreeCountHashMap = (HashMap&lt;String, ArrayList&lt;String&gt;&gt;) getIntent().getExtras().getSerializable("CommentHashMapDisAgree");

    ToadlineCommentAdapter adapter1 = new ToadlineCommentAdapter(getApplicationContext(), getData2());
    recyclerView.setAdapter(adapter1);
    adapter1.setClickListener(this);
    adapter1.notifyDataSetChanged();
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));


  mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enteredCommentText = mComments.getText().toString();

                if (enteredCommentText != null) {
                    if (enteredCommentText.equals("") || enteredCommentText.startsWith(" ")) {
                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.empty_comment_text), Toast.LENGTH_SHORT).show();
                        return;
                    } else {

                        new RegisterCheck().execute();

                    }
                }
            }
        });

     }

    public static List&lt;CommentDataStore&gt; getData2() {

    List&lt;CommentDataStore&gt; data = new ArrayList&lt;&gt;();

    Bitmap[] commentProfileImage = commentProfileImageDownload;
    ArrayList&lt;String&gt; commentUserName1 = newCommentUserName;
    ArrayList&lt;String&gt; commentDays1 = newCommentDays;
    ArrayList&lt;String&gt; commentDescription1 = newCommentDescription;
    ArrayList&lt;String&gt; commentAgreeCount1 = newCommentAgreeCount;
    ArrayList&lt;String&gt; commentDisAgreeCount1 = newCommentDisAgreeCount;

    for (int i = 0; i &lt; commentProfileImage.length &amp;&amp; i &lt; commentUserName1.size() &amp;&amp; i &lt; commentDays1.size() &amp;&amp; i &lt; commentDescription1.size(); i++) {
    CommentDataStore current = new CommentDataStore();

    current.commentProfileImageLink = commentProfileImage[i];
    current.commentUserName = commentUserName1.get(i);
    current.commentDays = commentDays1.get(i);
    current.commentDescription = commentDescription1.get(i);
    current.commentAgreeCount = commentAgreeCount1.get(i);
    current.commentDisAgreeCount = commentDisAgreeCount1.get(i);
    data.add(current);
    }
    return data;
    }


   public class RegisterCheck extends AsyncTask&lt;String, String, String&gt; implements ToadlineCommentAdapter.ClickListener {

        private HttpURLConnection conn;
        private String
                text;

        @Override
        protected String doInBackground(String... strings) {


     if (response != null) {

     JSONObject jsonObject = new JSONObject(text);

    commentContent = jsonObject.getString("content");
    commenterId = jsonObject.getString("commenterId");
    commenterName = jsonObject.getString("commenterName");
    commenterPhotos = jsonObject.getString("commenterPhotos");
    commentCreatedDate = jsonObject.getString("dateCreated");
    newCommentDescription.add(commentContent);
    newCommentUserName.add(commenterName);

    ToadlineCommentAdapter adapter1 = new ToadlineCommentAdapter(getApplicationContext(), getData2());
    recyclerView.setAdapter(adapter1);
    adapter1.setClickListener(CommentActivity.this);
    adapter1.notifyDataSetChanged();
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    }
     }
    }

    }
</code></pre>