Show details of an element clicked in recycliewiew

I can detect the position position of an individual recycler and can toast it when I click. Now I will not proceed to a new action and show the details of the clicked element, how can I do this? Say I'm showing a list of contact names and onclick. I do not open a new activity show that shows contact details ... At least how can I click on the contact name again when I click on this contact element, how are the current variables available when I click? I plan to bind these variables and send them with the intention and display there.

I know what I need 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 } } 
+6
source share
4 answers

Let's look at your own example contact list to explain the full concept to you. Suppose we have a custom Contact class that contains the string "Name", as shown below: -

 class Contact implements Parcelable{ String name; public void setName(String name) { this.name = name; } public String getName() { return name; } // Add your Parcelable code here } 

Now, in your activity, where you have a listener connected to your recyclerview, your code will look like this: -

 List<Contact> contacts = new ArrayList<>(); // You have to initialize your contacts list with data, I just gave you an example recyclerItemClickListener = new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Intent intent = new Intent(FirstActivity.this, SecondActivity.class); intent.putExtra("Key", contacts.get(position); startActivity(intent); } }); recyclerView.addOnItemTouchListener(recyclerItemClickListener); 

Updated answer: -

To use key values ​​in another activity, follow these steps: -

 Contact contact = getIntent().getParcelableExtra("Key"); 

The above line of code extracts the Contact object from the intent using your key.

+1
source

I had the same problem until I did this.

Created by 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 activity with 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 this helps

+1
source

You need to implement onClickListener for View in the owner class of RecyclerView.Adapter. First select this and start from now on working with the data available through the holder.

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView textView1 , textView2; public ViewHolder(View v) { super(v); textView1= (TextView) v.findViewById(R.id.textview1); textView2= (TextView) v.findViewById(R.id.textview2); v.setOnClickListener(this); } @Override public void onClick(View v) { ViewHolder h = (ViewHolder)v.getTag(); Intent next = new Intent(YourClass.this, ClassToOpen.class); next.putExtra("text1", h.textView1.getText().toString()); next.putExtra("text2", h.textView2.getText().toString()); startActivity(next); } } 

After that, in your onCreateViewHolder you need to set the tag on your view as follows.

  @Override public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View rowView = LayoutInflater.from(getActivity()).inflate(R.layout.row , parent , false); ViewHolder holder = new ViewHolder(rowView); rowView.setTag(holder); return holder; } 

Other implementations should be whatever.

0
source

I had the same problem.

The 1st approach applies all the necessary data with the intention as additional functions, and then begins the required activity with this intention. Inside a new activity, get all the extra features from intention. This approach works for a small amount of data, but not very good if you need to transfer a large amount of data to another activity.

The second approach I used is the following.

Create a model class that stores all the required data fields. Make an arraylist of your model class and fill your recyclerview with this arraylist. Now each element of your arraylist is an object of a model class.

Suppose

your model class is MyModel.java

your arraylist ArrayList <'MyModel> myArrayList

Now make a global reference to your model class. eg

MyModel model;

create setter and getter methods for the link above.

Now, since every element of your recyclerview is an object of a model class. when you click on an element, inside the onClick method, set the global link to the click object (model class object) above using the setter method. Then start a new activity (from within the onClick method) using the intent and inside onCreate of the newly created activity, access the global refernce. Thus, you can access the clicked element (model object) and get the required fields (data) from this object.

0
source

All Articles