Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show the details of item clicked in recyclerview

Tags:

android

I am able to detect the individual recycler view item position and able to toast it on clicked. Now I wan't to move on to a new activity and show the details of the item clicked, how can I do that? Say, I am displaying Contact names list and onclick I wan't to open a new activity show that contact's details... At least, how can I toast the contact name again on clicking on that contact item, how are the current variables on clicking available to me? I am planning to bundle those variables and send them with intent and display there.

i know i have to implement here

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {  
private OnItemClickListener mListener;

public interface OnItemClickListener {
    public void onItemClick(View view, int position){
      //i know i have to implement here 

    }


}
like image 338
Dolle Tulsi Avatar asked Nov 23 '15 12:11

Dolle Tulsi


1 Answers

I had the same problemm untill i did this.

Created a custome RecyclerListener:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
        });
    }

    @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 false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

then in the activity with the recyclerView:

private void registerCallClickBack() {

        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity().getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(this, DetailActivity.class);
                intent.putExtra("contact_name", customList.get(position).getName());
                intent.putExtra("contact_image", customList.get(position).getImage());
                intent.putExtra("contact_tel", customList.get(position).getMobile());
                intent.putExtra("contact_email", customList.get(position).getEmail());
                startActivity(intent);
            }
        }));
    }

where customList is my ArrayList of contacts.

Hope it helps

like image 134
Kostas Drak Avatar answered Oct 01 '22 00:10

Kostas Drak