I wanna implement both OnItemClickListener and OnItemLongClickListener on my Recycler View. I use the Android library BRVAH to set up the Adapter.
I'm expecting a custom declaration of OnItemLongClickListener, so that I could put it on my (already implemented), OnItemClickListener.
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
public void onItemClick(BaseQuicAdapter adapter, final View view, int position) {
.....
}
});
you have to make two interfaces to handle click event and long click event into the adapter for recyclview.
Like make below interface into adapter class..
onItemClickListner onItemClickListner;
onItemLongClickListner onItemLongClickListner;
public void setOnItemLongClickListner(RecyclerViewAdpater.onItemLongClickListner onItemLongClickListner) {
this.onItemLongClickListner = onItemLongClickListner;
}
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(String str);//pass your object types.
}
public interface onItemLongClickListner{
void onLongClick(String str);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
String data = mStringList.get(position); // if you pass object of class then create that class object.
holder.textView.setText(data);
// below code handle click event on recycler view item.
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick(data);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
onItemLongClickListner.onLongClick("Hiii");
return false;
}
});
}
then after in activity when define recycler view after adapter define and bind into recycler view then get both click event like below ..
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
@Override
public void onClick(String str) {
Toast.makeText(getApplicationContext(),"Value of Click"+str,Toast.LENGTH_SHORT).show();
}
});
recyclerViewAdpater.setOnItemLongClickListner(new RecyclerViewAdpater.onItemLongClickListner() {
@Override
public void onLongClick(String str) {
Toast.makeText(getApplicationContext(),"Value of Long Click"+str,Toast.LENGTH_SHORT).show();
}
});
You can use the following method of the library.
/**
* Register a callback to be invoked when an item in this RecyclerView has
* been long clicked and held
*
* @param listener The callback that will run
*/
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
}
So to implement it in your code
mAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
.....
}
Go into your adapter and find ViewHolder class and write following code
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//call onClick handler here
}
});
itemView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
//call onLongClick handler here //Here you will return true
return true;
}
});
}
}
then create two interface like
public interface OnMyLongClickListener{
void onLongClick(int pos);
}
public interface OnMyClickClickListener{
void onClick(int pos);
}
implement these two interfaces in your activity or fragment class and then call these interfaces methods in receptive events
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.