Ad

Recycler View Onclick To Open Dialog With Data Present On Card View

I'm trying to implement Onclick on my RecyclerView. I tried many ways of doing it, but it ends up crashing my application. I also would like to have a dialog when any of the cardView is clicked.

I'm using fireBase as my backend, and I'm quite new to this.

Here's my Adapter class:

public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {
    public List<BlogPost>blogList;
    public Context context;
    private FirebaseFirestore firebaseFirestore;

    private static final int SECOND_MILLIS = 1000;
    private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
    private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
    private static final int DAY_MILLIS = 24 * HOUR_MILLIS;
    String dateString;

    public  BlogRecyclerAdapter(List<BlogPost>blogList){
        this.blogList=blogList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item,parent,false);
        context=parent.getContext();
        firebaseFirestore=FirebaseFirestore.getInstance();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        String descData=blogList.get(position).getDesc();
        holder.setDescText(descData);
        String imageUrl=blogList.get(position).getImage_url();
        holder.setBlogImage(imageUrl);
        final String userId=blogList.get(position).getUser_id();
        firebaseFirestore.collection("Users").document(userId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if(task.isSuccessful()){
                    String username=task.getResult().getString("Name");
                    holder.setUserData(username);
                }
                else{

                }
            }
        });

        long miliseconds=blogList.get(position).getTimestamp().getTime();
        long now = System.currentTimeMillis();

        final long diff = now - miliseconds;
        if (diff < MINUTE_MILLIS) {
             dateString= "just now";
        } else if (diff < 2 * MINUTE_MILLIS) {
             dateString= "a minute ago";
        } else if (diff < 50 * MINUTE_MILLIS) {
             dateString= diff / MINUTE_MILLIS + " minutes ago";
        } else if (diff < 90 * MINUTE_MILLIS) {
             dateString= "an hour ago";
        } else if (diff < 24 * HOUR_MILLIS) {
             dateString= diff / HOUR_MILLIS + " hours ago";
        } else if (diff < 48 * HOUR_MILLIS) {
             dateString= "yesterday";
        } else {
             dateString= diff / DAY_MILLIS + " days ago";
        }
        // String dateString= DateFormat.format("MM/dd/yyyy",new Date(miliseconds)).toString();
        holder.setTime(dateString);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        private View mView;
        private TextView descView;
        private ImageView blogImageView;
        private TextView blogDate;
        private TextView blogUserName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mView=itemView;
        }

        public void setUserData(String name){
            blogUserName=mView.findViewById(R.id.blogUserName);
            blogUserName.setText(name);
        }

        public void setDescText(String descText){
            descView=mView.findViewById(R.id.blogDesc);
            descView.setText(descText);
        }

        public void setBlogImage(String downloaduri){
            blogImageView=mView.findViewById(R.id.blogImage);
            Glide.with(context).load(downloaduri).apply(RequestOptions.bitmapTransform(new BlurTransformation(30,3))).into(blogImageView);
        }

        public void setTime(String date){
            blogDate=mView.findViewById(R.id.blogDate);
            blogDate.setText(date);
        }
    }
}

I tried many methods of implementing, it but was unsuccessful.

Ad

Answer

Adding onClickListener to RecyclerView is a painful process than ListView. But there is a way to do it.

  1. Firstly, add this class to your adapter.

    public static class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;
    
    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    
        public void onLongItemClick(View view, int position);
    }
    
    GestureDetector mGestureDetector;
    
    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
    
            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && mListener != null) {
                    mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }
    
    @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
            return true;
        }
        return false;
    }
    
    @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
    
    @Override
    public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
    }
    
  2. Secondly, add this listener to your activity/fragment

    recyclerView.addOnItemTouchListener(new BlogRecyclerAdapter.RecyclerItemClickListener(getActivity(), recyclerView ,new BlogRecyclerAdapter.RecyclerItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {
    
                        // Do something here...
                    }
    
                    @Override public void onLongItemClick(View view, int position) {
    
                    }
                })
        );
    
Ad
source: stackoverflow.com
Ad