best way to get RecycleView detail in another view - android

I'm implementing onClick on my main layout that contain all my views, in order to pass data to another view e.g:
holder.wholeView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, String.valueOf(position), Toast.LENGTH_SHORT).show();
//pass data with something like eventBus and transition to another view
}
});
I think there is a better way to do that?

Use this:
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = 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 && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
and from your activity where you're using your Recycler view use it like this:
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, mRecyclerView,
new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
// use this position to fetch the data from your list which you used to populate your recylcer view
}
#Override
public void onLongClick(View view, int position) {
}
}));

You should have the list of items from which the recycler view items are made. Now simply use this
myList.get(holder.getAdapterPosition());
This will return you the object of the clicked position.
Hope this helps.

Related

addOnItemTouchListener in recyclerview getting queued

i am showing json data in recyclerview. when i click any item of the recyclerview it should showing some new data. its working fine but the problem is, when i click its getting queued, like if i click tow item one after another, recyclerview will show items for twice.how to solve this issue?
i have tried by setting addOnItemTouchListener null after completion the click, but its showing null pointer exception on next click.
my click listener class below:
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = 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 && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
You can also use recycle view click listener below is the class and you can esaily use it.
public class RecyclerOnClickListener 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 RecyclerOnClickListener(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){}
}
In Activity also you can use it like this
recyclerview_productList.addOnItemTouchListener(
new RecyclerOnClickListener(AgentCustomerActivity.this, recyclerview_productList, new RecyclerOnClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(AgentCustomerActivity.this, AgentCutomerOrderDetailActivity.class);
intent.putExtra("agent_cutomerId", list.get(position).getCustomerId());
intent.putExtra("agent_cutomerName", list.get(position).getCustomerName());
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
#Override
public void onLongItemClick(View view, int position) {
}
}));

How to set onItemClickListener for RecyclerView? [duplicate]

This question already has answers here:
Why doesn't RecyclerView have onItemClickListener()?
(31 answers)
Closed 6 years ago.
I'm trying to implement a recyclerView but how can I set an onItemClickListener for my recyclerView?
There are already some answers on SO but they are briefer than what I just need to get the onItemClickListener for RecyclerView.
What is the easiest way to get the onItemClickListener for RecyclerView?
In Android, RecyclerView doesn’t have any listeners like in ListView. So you need to create your own class by extending the RecyclerView.OnItemTouchListener.
Create a class RecyclerTouchListener.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = 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 && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildAdapterPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
Then in activity, Override the onClick method.
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(context, recyclerView, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
// Toast.makeText(getActivity(), position+ " is selected successfully", Toast.LENGTH_SHORT).show();
//handle click event
}
#Override
public void onLongClick(View view, int position) {
}
}));
By this way, you can handle recyclerview item click event.
define a class named RecyclerItemClickListener with this code
class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
interface OnItemClickListener {
void onItemClick(View view, int position);
}
private GestureDetector mGestureDetector;
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 your activity for use this code
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(MainActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
}
}));

how to handle both RecyclerView on item click listener for its rows and the views inside it

I have recyclerView which contains some text view and 2 image view.
I would like to do a task when user clicks on each row and another task when user clicks on each of image views .. but I can only handle one of them at the same time.
Now if I set recylerview.addOnItemTouchListener I can't handle images views onclicks with setting on clicklistener for them in the adapter because when user clicks on image view the row's click listener will trigger.
Below code is my class for handling recycler view items
public 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){}
and this is my holder inside adapter
public MyViewHolder(View view) {
super(view);
tv_word = (TextView) view.findViewById(R.id.tv_word_show);
tv_english = (TextView) view.findViewById(R.id.tv_english_show);
tv_translation = (TextView) view.findViewById(R.id.tv_tranlation_show);
tv_numbershow = (TextView) view.findViewById(R.id.tv_numershow);
fav = (ImageView) view.findViewById(R.id.iv_fav);
more = (ImageView) view.findViewById(R.id.iv_more);
fav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext,"show some text for test",Toast.LENGTH_LONG).show();
}
});
I appreciate any help for my problem
Create Interface in you adapter class.
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public interface ClickListener {
void ItemClicked(View v, int position);
}
Declare the object on top like this:
ClickListener clickListener;
holder.view.setOnclickListener(new OnclickListener(
#Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.ItemClicked(v, getAdapterPosition());
}
}
);
Implement this interface in class and intialize the clicklistener where you are setting adapter to recyclerview.
adapter.setClickListener(this);
When you will implement this in class you will get following method:
#Override
public void ItemClicked(View v, int position) {
}
Apply switch case from v.getId() you can handle as many click listeners you need to implement.

itemclick listener for recyclerview in android

I am using android recyclerview with cardview, which populates with the data from server. I want to set itemclick event for each item and need to display a dialog with a text. this text also getting from server. it will be different for diferent items.
when I am trying like given,
mItem=lists.getRemarks();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new SweetAlertDialog(v.getContext())
.setTitleText("Remark")
.setContentText(mItem)
.show();
}
});
it only gives the last details
somehow i solved it by setting setContentText(mItem) to setContentText(lists.getRemarks())
Recyclerview does not have its item click listener so I have one custom class to detect it
RecyclerItemClickListner
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener
{
public static interface OnItemClickListener
{
public void onItemClick(View view, int position);
public void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private 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 childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if(childView != null && mListener != null)
{
mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView));
}
}
});
}
#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.getChildPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent){}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Use it
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
// do whatever you want on position clicked
}
#Override
public void onItemLongClick(View view, int position) {
// do whatever you want on position long press
}
}));

How to get row of particular position of recycler view in android?

In my application I need to get a complete row of recycler view (becuase I want to get some view of that row) of a particular position.I searched we can get position by method getAdapterPosition().Now after getting position via this method I want to get complete row of that position.How can I achieve it?
Edit: I have some view on every row and I have set onclick listner on buttons in click of that I want get complete row or top layout (parent layout).What I understand is when on click is called it return parent view on which button is wrapped but not complete row.
I used one class for getting views(and Click and Long clicks too)
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = 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 && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
and to use this
/**
* Recycler view item click handler
*/
rvDraft.addOnItemTouchListener(new RecyclerTouchListener(getActivity(),
rvDraft, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, final int position) {
// click operation
}
#Override
public void onLongClick(View view, final int position) {
Dialogs dialog = new Dialogs();
dialog.showAlertForAction(getActivity(), getString(R.string.are_u_sure_want_to_delete), new AlertInterface() {
#Override
public void buttonYesClick() {
// long press operation
}
#Override
public void buttonNoClick() {
}
});
}
}));
Hope this will help you .
Use a global variable View selectedView. In your custom ViewHolder class, assuming the row item to be a LinearLayout set add the following lines
rowItemLayout = (LinearLayout) itemView.findViewById(R.id.row_item);
rowItemLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedView = rowItemLayout;
..
}
});
Now you can fetch that view using
public View getSelectedView() {
return selectedView;
}
I have got solution to this problem now. First get the recycler view in adapter using below method in adapter class
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;
}
and on click event use below code
RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForLayoutPosition(getAdapterPosition());
and then cast the holder to the actual object as mentioned below
MessageViewHolder messageViewHolder = (MessageViewHolder) holder;
Congrats!! now you have a complete row of that particular position. Happy coding
You can try
getBindingAdapterPosition(view);
getAbsoluteAdapterPosition() ;
getchildAdapterPosition(view);

Categories

Resources