Handle On Item Long Click On Recycler View
Solution 1:
This has already been answered here. Anyway, you can do it like this:
classViewHolderextendsRecyclerView.ViewHolderimplementsView.OnClickListener, View.OnLongClickListener {
privateArticle article;
privateTextView nameTextView;
publicViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
nameTextView = (TextView) itemView.findViewById(R.id.grid_item_article_name_textView);
}
publicvoidbind(Article article) {
this.article = article;
nameTextView.setText(article.getName());
}
@OverridepublicvoidonClick(View view) {
// Context context = view.getContext();// article.getName()
}
@OverridepublicbooleanonLongClick(View view) {
// Handle long click// Return true to indicate the click was handledreturntrue;
}
}
Update: if you're using Kotlin do:
innerclassViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
View.OnClickListener, View.OnLongClickListener {
init {
itemView.setOnClickListener(this)
itemView.setOnLongClickListener(this)
}
privatelateinitvar article: Article
privateval titleTextView: TextView = itemView.findViewById(R.id.item_article_title_textView)
funbind(article: Article) {
this.article = article
titleTextView.text = article.title
}
overridefunonClick(view: View) {
listener.onItemClick(article)
}
overridefunonLongClick(view: View): Boolean {
Toast.makeText(view.context, "long click", Toast.LENGTH_SHORT).show()
// Return true to indicate the click was handledreturntrue
}
}
Solution 2:
I did in this way:
staticclassViewHolderextendsRecyclerView.ViewHolder {
TextView tvName;
ViewHolder(View v) {
super(v);
tvName = (TextView) v.findViewById(R.id.textView_Username);
//Single Tapup
v.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
Toast.makeText(v.getContext(), "Position is " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
//Long Press
v.setOnLongClickListener(newView.OnLongClickListener() {
@OverridepublicbooleanonLongClick(View v) {
Toast.makeText(v.getContext(), "Position is " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
returnfalse;
}
});
}
}
Solution 3:
First you have to register your Activity to listen longClick events from the recyclerView
(so you don't have to use any kind of onLongClickListener
...):
registerForContextMenu(recyclerView);
Then you create a menu resource (context_menu.xml):
<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:title="Mostra documento"android:id="@+id/context_menu_documents_fragment_view"></item><itemandroid:title="Aggiungi ad un contenitore"android:id="@+id/context_menu_documents_fragment_add_to_box"></item><itemandroid:title="Elimina documento"android:id="@+id/context_menu_documents_fragment_delete"></item></menu>
In the activity where you registered for context menu you override this methods:
@OverridepublicvoidonCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Inflate Menu from xml resourceMenuInflater menuInflater = getActivity().getMenuInflater();
menuInflater.inflate(R.menu.context_menu_documents_fragment, menu);
}
@OverridepublicbooleanonContextItemSelected(MenuItem item) {
Toast.makeText(getActivity(), " User selected something ", Toast.LENGTH_LONG).show();
returnfalse;
}
This is very important, you have to modify the code in your RecyclerView
adapter like this:
@OverridepublicvoidonBindViewHolder(final DocumentViewHolder viewHolder, int position) {
...
viewHolder.itemView.setLongClickable(true);
...
}
Now you are able to show contextual menu and intercept user selection! But you aren't able to know which item the user clicked, to do this you have to use a custom RecyclerView like this (original code from Renaud Cerrato):
publicclassContextMenuRecyclerViewextendsRecyclerView {
private RecyclerContextMenuInfo mContextMenuInfo;
publicContextMenuRecyclerView(Context context) {
super(context);
}
publicContextMenuRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicContextMenuRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Overrideprotected ContextMenu.ContextMenuInfo getContextMenuInfo() {
return mContextMenuInfo;
}
@OverridepublicbooleanshowContextMenuForChild(View originalView) {
finalintlongPressPosition= getChildAdapterPosition(originalView);
if (longPressPosition >= 0) {
finallonglongPressId= getAdapter().getItemId(longPressPosition);
mContextMenuInfo = newRecyclerContextMenuInfo(longPressPosition, longPressId);
returnsuper.showContextMenuForChild(originalView);
}
returnfalse;
}
publicstaticclassRecyclerContextMenuInfoimplementsContextMenu.ContextMenuInfo {
publicRecyclerContextMenuInfo(int position, long id) {
this.position = position;
this.id = id;
}
finalpublicint position;
finalpubliclong id;
}
}
In the previous onContextItemSelected()
method you can know the recyclerView
item id and position using this code:
ContextMenuRecyclerView.RecyclerContextMenuInfoinfo= (ContextMenuRecyclerView.RecyclerContextMenuInfo) item.getMenuInfo();
Finally you have to modify the getItemId() method in your recyclerView adapter and the layout file to make sure that you use your recyclerView and not the android one!
Solution 4:
I was struggling hard to fetch the position of item on click, this worked for me :
publicvoidonClick(View view) {
ViewHolderholder=(ViewHolder)view.getTag();
intposition= holder.getLayoutPosition();
Log.d("testing ","pos" +position);
}
Post a Comment for "Handle On Item Long Click On Recycler View"