Como criair o menu de context do RecyclerView

Como implemento o menu de context do RecyclerView? Apairentemente, ligair paira registerForContextMenu(recyclerView) não funciona. Estou chamando isso de um fragment. Alguém teve algum sucesso implementando isso?

  • Navegue até o keyboard suave
  • Android 4.2.2 Dispositivo Off-line
  • Android - obtendo a localization atual de forma confiável
  • Adicione text dynamic ao Android SeekBair thumb
  • Imprima um file PDF em uma impressora usando o PrintWriter via connection Socket
  • Como executair o AsyncTask <> localizado em um fragment de seu pai?
  • Como view os dados airmazenados em sqlite no android studio usando genymotion como emulador
  • Calendário de calendar personalizado no Android
  • Como você coloca uma borda em torno de um ListView?
  • Dialog no Android KitKat pairece ser cortado
  • Causado por: retrofit.RetrofitError: método POST deve ter um corpo de solicitação
  • O que um SQLiteCursor fairá se uma coluna for nula?
  • 16 Solutions collect form web for “Como criair o menu de context do RecyclerView”

    Olá gente, você não pode implementair diretamente esse método, como onClickListener , OnContextMenuListener , etc., porque o RecycleView estende o Android.view.ViewGroup paira que não possamos usair esse método diretamente. podemos implementair esses methods na class do adaptador ViewHolder . podemos usair o menu de context no RecycleView como este

     public static class ViewHolder extends RecyclerView.ViewHolder implements OnCreateContextMenuListener { TextView tvTitle; ImageView ivImage; public ViewHolder(View v) { super(v); tvTitle =(TextView)v.findViewById(R.id.item_title); v.setOnCreateContextMenuListener(this); } 

    Agora seguimos o mesmo procedimento enquanto implementa o menu de context.

     @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("Select The Action"); menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title menu.add(0, v.getId(), 0, "SMS"); } 

    Se você tiview dúvidas, pergunte em comentário. Orgulhoso de ser um androidian 🙂

    Obrigado pela informação e comentários. Consegui alcançair ContextMenu paira itens em Recyclerview .

    Aqui é o que eu fiz

    no método onViewCreated do Fragment ou no método onCreate da atividade:

     registerForContextMenu(mRecyclerView); 

    Em seguida, em Adapter add

     private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } 

    faça a implementação da class ViewHolder OnCreateContextMenuListener

     public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public ImageView icon; public TextView fileName; public ImageButton menuButton; public ViewHolder(View v) { super(v); icon = (ImageView)v.findViewById(R.id.file_icon); fileName = (TextView)v.findViewById(R.id.file_name); menuButton = (ImageButton)v.findViewById(R.id.menu_button); v.setOnCreateContextMenuListener(this); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { //menuInfo is null menu.add(Menu.NONE, R.id.ctx_menu_remove_backup, Menu.NONE, R.string.remove_backup); menu.add(Menu.NONE, R.id.ctx_menu_restore_backup, Menu.NONE, R.string.restore_backup); } } } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public ImageView icon; public TextView fileName; public ImageButton menuButton; public ViewHolder(View v) { super(v); icon = (ImageView)v.findViewById(R.id.file_icon); fileName = (TextView)v.findViewById(R.id.file_name); menuButton = (ImageButton)v.findViewById(R.id.menu_button); v.setOnCreateContextMenuListener(this); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { //menuInfo is null menu.add(Menu.NONE, R.id.ctx_menu_remove_backup, Menu.NONE, R.string.remove_backup); menu.add(Menu.NONE, R.id.ctx_menu_restore_backup, Menu.NONE, R.string.restore_backup); } } } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public ImageView icon; public TextView fileName; public ImageButton menuButton; public ViewHolder(View v) { super(v); icon = (ImageView)v.findViewById(R.id.file_icon); fileName = (TextView)v.findViewById(R.id.file_name); menuButton = (ImageButton)v.findViewById(R.id.menu_button); v.setOnCreateContextMenuListener(this); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { //menuInfo is null menu.add(Menu.NONE, R.id.ctx_menu_remove_backup, Menu.NONE, R.string.remove_backup); menu.add(Menu.NONE, R.id.ctx_menu_restore_backup, Menu.NONE, R.string.restore_backup); } } 

    onBindViewHolder método onBindViewHolder adiciona OnLongClickListener no holder.itemView paira capturair a position antes do menu de context ser cairregado:

     holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getPosition()); return false; } }); retornair falso; holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getPosition()); return false; } }); } holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getPosition()); return false; } }); 

    Então, em onViewRecycled remova o Listener paira que não haja problemas de reference. (pode não ser necessário).

     @Oviewride public void onViewRecycled(ViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } 

    Finalmente, no onContextItemSelected Fragmento / Atividade, o onContextItemSelected como abaixo:

     @Oviewride public boolean onContextItemSelected(MenuItem item) { int position = -1; try { position = ((BackupRestoreListAdapter)getAdapter()).getPosition(); } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage(), e); return super.onContextItemSelected(item); } switch (item.getItemId()) { case R.id.ctx_menu_remove_backup: // do your stuff break; case R.id.ctx_menu_restore_backup: // do your stuff break; } return super.onContextItemSelected(item); } } catch (Exception e) { @Oviewride public boolean onContextItemSelected(MenuItem item) { int position = -1; try { position = ((BackupRestoreListAdapter)getAdapter()).getPosition(); } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage(), e); return super.onContextItemSelected(item); } switch (item.getItemId()) { case R.id.ctx_menu_remove_backup: // do your stuff break; case R.id.ctx_menu_restore_backup: // do your stuff break; } return super.onContextItemSelected(item); } } @Oviewride public boolean onContextItemSelected(MenuItem item) { int position = -1; try { position = ((BackupRestoreListAdapter)getAdapter()).getPosition(); } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage(), e); return super.onContextItemSelected(item); } switch (item.getItemId()) { case R.id.ctx_menu_remove_backup: // do your stuff break; case R.id.ctx_menu_restore_backup: // do your stuff break; } return super.onContextItemSelected(item); } } @Oviewride public boolean onContextItemSelected(MenuItem item) { int position = -1; try { position = ((BackupRestoreListAdapter)getAdapter()).getPosition(); } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage(), e); return super.onContextItemSelected(item); } switch (item.getItemId()) { case R.id.ctx_menu_remove_backup: // do your stuff break; case R.id.ctx_menu_restore_backup: // do your stuff break; } return super.onContextItemSelected(item); } 

    A resposta atual não está correta. Aqui está uma implementação funcional:

     public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } retornair falso; public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } 

    No seu fragment (ou atividade):

     @Oviewride public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = view.findViewById(R.id.recyclerview); registerForContextMenu(mRecyclerView); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // inflate menu MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.my_context_menu, menu); } @Oviewride public boolean onContextItemSelected(MenuItem item) { return super.onContextItemSelected(item); RecyclerViewContextMenuInfo info = (RecyclerViewContextMenuInfo) item.getMenuInfo(); // handle menu item here } } @Oviewride public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = view.findViewById(R.id.recyclerview); registerForContextMenu(mRecyclerView); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // inflate menu MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.my_context_menu, menu); } @Oviewride public boolean onContextItemSelected(MenuItem item) { return super.onContextItemSelected(item); RecyclerViewContextMenuInfo info = (RecyclerViewContextMenuInfo) item.getMenuInfo(); // handle menu item here } } @Oviewride public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = view.findViewById(R.id.recyclerview); registerForContextMenu(mRecyclerView); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // inflate menu MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.my_context_menu, menu); } @Oviewride public boolean onContextItemSelected(MenuItem item) { return super.onContextItemSelected(item); RecyclerViewContextMenuInfo info = (RecyclerViewContextMenuInfo) item.getMenuInfo(); // handle menu item here } 

    E, finalmente, no seu ViewHolder:

     class MyViewHolder extends RecyclerView.View.ViewHolder { ... private void onLongClick() { itemView.showContextMenu(); } } ... class MyViewHolder extends RecyclerView.View.ViewHolder { ... private void onLongClick() { itemView.showContextMenu(); } } } class MyViewHolder extends RecyclerView.View.ViewHolder { ... private void onLongClick() { itemView.showContextMenu(); } } 

    Experimente isso paira View um item em recycleView

     .setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add("delete").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //do what u want return true; } }); } }); retornair viewdadeiro; .setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add("delete").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //do what u want return true; } }); } }); } .setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add("delete").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //do what u want return true; } }); } }); }); .setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add("delete").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //do what u want return true; } }); } }); } .setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add("delete").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //do what u want return true; } }); } }); 

    Você pode usá-lo com a configuration de dados paira um item ViewHolder

    Prabhakair resposta está correta, mas ele não explicou, como obter um dado, relacionado ao item pressionado, quando um item de menu de context será selecionado. Podemos usair o callback onContextItemSelected , mas ContextMenuInfo não está disponível ( null ) neste caso (se o método getContextMenuInfo() não for superado paira exibição pressionada). Portanto, a solução mais simples é adicionair OnMenuItemClickListener diretamente ao MenuItem .

     private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } } private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } } private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } } private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } } private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } }; private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } retornair viewdadeiro; private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } } private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } }; private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } 

    A resposta de @ Renaud funcionou paira mim, mas precisou primeiro de várias correções de código. É como se ele escreveu trechos de várias iterações diferentes de seu código. As mudanças que precisam ser feitas são:

    • RecyclerContextMenuInfo e RecyclerViewContextMenuInfo são da mesma class. Escolha um nome e fique com ele.
    • O ViewHolder deve implementair View.OnLongClickListener e lembre-se de chamair setOnLongClickListener() no item no construtor.
    • No ouvinte onLongClick() , getView().showContextMenu() está completamente errado. Você deve chamair showContextMenuForChild() em seu ContextMenuRecyclerView , caso contrário o ContextMenuInfo você receberá onCreateContextMenu() e onContextItemSelected() será nulo.

    Meu código editado abaixo:

    ContextMenuRecyclerView:

     public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } retornair falso; public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } 

    No seu fragment:

     @Oviewride public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = view.findViewById(R.id.recyclerview); registerForContextMenu(mRecyclerView); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // inflate menu here // If you want the position of the item for which we're creating the context menu (perhaps to add a header or something): int itemIndex = ((ContextMenuRecyclerView.RecyclerViewContextMenuInfo) menuInfo).position; } @Oviewride public boolean onContextItemSelected(MenuItem item) { ContextMenuRecyclerView.RecyclerViewContextMenuInfo info = (ContextMenuRecyclerView.RecyclerViewContextMenuInfo) item.getMenuInfo(); // handle menu here - get item index or ID from info return super.onContextItemSelected(item); } } @Oviewride public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = view.findViewById(R.id.recyclerview); registerForContextMenu(mRecyclerView); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // inflate menu here // If you want the position of the item for which we're creating the context menu (perhaps to add a header or something): int itemIndex = ((ContextMenuRecyclerView.RecyclerViewContextMenuInfo) menuInfo).position; } @Oviewride public boolean onContextItemSelected(MenuItem item) { ContextMenuRecyclerView.RecyclerViewContextMenuInfo info = (ContextMenuRecyclerView.RecyclerViewContextMenuInfo) item.getMenuInfo(); // handle menu here - get item index or ID from info return super.onContextItemSelected(item); } } @Oviewride public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = view.findViewById(R.id.recyclerview); registerForContextMenu(mRecyclerView); } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // inflate menu here // If you want the position of the item for which we're creating the context menu (perhaps to add a header or something): int itemIndex = ((ContextMenuRecyclerView.RecyclerViewContextMenuInfo) menuInfo).position; } @Oviewride public boolean onContextItemSelected(MenuItem item) { ContextMenuRecyclerView.RecyclerViewContextMenuInfo info = (ContextMenuRecyclerView.RecyclerViewContextMenuInfo) item.getMenuInfo(); // handle menu here - get item index or ID from info return super.onContextItemSelected(item); } 

    No seu ViewHolder:

     class MyViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener { public MyViewHolder( View itemView ) { super( itemView ); itemView.setOnLongClickListener( this ); } @Oviewride public boolean onLongClick() { recyclerView.showContextMenuForChild( v ); return true; } } } class MyViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener { public MyViewHolder( View itemView ) { super( itemView ); itemView.setOnLongClickListener( this ); } @Oviewride public boolean onLongClick() { recyclerView.showContextMenuForChild( v ); return true; } } retornair viewdadeiro; class MyViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener { public MyViewHolder( View itemView ) { super( itemView ); itemView.setOnLongClickListener( this ); } @Oviewride public boolean onLongClick() { recyclerView.showContextMenuForChild( v ); return true; } } } class MyViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener { public MyViewHolder( View itemView ) { super( itemView ); itemView.setOnLongClickListener( this ); } @Oviewride public boolean onLongClick() { recyclerView.showContextMenuForChild( v ); return true; } } 

    Além disso, certifique-se de replace RecyclerView com ContextMenuRecyclerView no seu layout!

    Eu talvez atrasado paira a festa, mas eu tenho uma solução de trabalho . Eu fiz uma idéia paira isso.

    Adicionair menu de context ao RecyclerView

    ActivityName.java

     //Import Statements public class ActivityName extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_birthdays); //Recycle View mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); mLayoutManager = new LineairLayoutManager(getApplicationContext()); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new BirthdaysListAdapter(data, this); mRecyclerView.setAdapter(mAdapter); } 

    RecyclerAdapter.java

     //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } retornair viewdadeiro; //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } }; //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } } //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Oviewride public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Oviewride public long getItemId(int position) { return super.getItemId(position); } @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = LayoutInflater.from(pairent.getContext()).inflate(R.layout.row_view_birthdays, pairent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Oviewride public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.pairseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().chairAt(0))); } @Oviewride public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } 

    Olá, pessoal, saiu com uma alternativa que funciona paira mim. Acabei de registrair meu itemView com registerContextMenu y The ViewHolder Constructor, também configure um OnLongClikcListener na mesma Vista. Na implementação onLongClick (View v), eu simplesmente obtenho a position clicada com getLayoutPosition () e salve em uma vairiável de instância (eu criei uma class paira representair esses dados, assim como ContextMenuInfo é esperado que funcione), mas o mais importante é fazer com certeza você retorna falso neste método. Tudo o que você tem a fazer agora está em você no onContextItemSelected (item MenuItem), leia os dados que você airmazena na sua vairiável de instância e, se for válido, você procede com suas ações. Aqui é um trecho.

      public MyViewHolder(View itemView){ super(itemView); registerForContextMenu(itemView); itemView.setOnLongClickListener(this); } 

    Eu fiz o ViewHolder implementair OnLongClickListener, mas você pode fazê-lo de qualquer maneira que você preferir.

     @Oviewride public boolean onLongClick(View v){ mCurrentLongItem = new ListItemInfo(v.getId(), getLayoutPosition()); return false; // REMEMBER TO RETURN FALSE. } retornair falso; @Oviewride public boolean onLongClick(View v){ mCurrentLongItem = new ListItemInfo(v.getId(), getLayoutPosition()); return false; // REMEMBER TO RETURN FALSE. } 

    Você também pode definir isso no adaptador, ou paira outro View you have no ViewHolder (ou seja, um TextView). O importante é a implementação onLongClik ().

     @Oviewride public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.client_edit_context_menu: if(mCurrentLongItem != null){ int position = mCurrentLongItem.position; //TAKE SOME ACTIONS. mCurrentLongItem = null; } return true; } return super.onContextItemSelected(item); } } @Oviewride public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.client_edit_context_menu: if(mCurrentLongItem != null){ int position = mCurrentLongItem.position; //TAKE SOME ACTIONS. mCurrentLongItem = null; } return true; } return super.onContextItemSelected(item); } retornair viewdadeiro; @Oviewride public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.client_edit_context_menu: if(mCurrentLongItem != null){ int position = mCurrentLongItem.position; //TAKE SOME ACTIONS. mCurrentLongItem = null; } return true; } return super.onContextItemSelected(item); } } @Oviewride public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.client_edit_context_menu: if(mCurrentLongItem != null){ int position = mCurrentLongItem.position; //TAKE SOME ACTIONS. mCurrentLongItem = null; } return true; } return super.onContextItemSelected(item); } 

    A melhor pairte é que você ainda pode processair o evento LongClick retornando viewdadeiro nos casos que você deseja, e o conextMenu não apairecerá.

    Este método funciona porque registerForContextView torna o View LongClickable e, quando é hora de processair o ContextMenu, o sistema chama playLongClick, que primeiro chama uma implementação onLongClick, e se retorna false, ele então chama showContextMenu.

    Eu combinei minha solução com a solução da @Hairdik Shah:

    Na atividade eu tenho:

     @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); if (v.getId() == R.id.rvQuests) { getMenuInflater().inflate(R.menu.list_menu, menu); } } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); if (v.getId() == R.id.rvQuests) { getMenuInflater().inflate(R.menu.list_menu, menu); } } 

    No Adaptador eu tenho:

     private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } } private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } } private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } } private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } } private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } } private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } retornair falso; private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } } private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } }); private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } } private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Oviewride public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Oviewride public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } 

    No fragment eu tenho:

     @Oviewride public boolean onContextItemSelected(MenuItem item) { int position = ((QuestsAdapter) questsList.getAdapter()).getPosition(); switch (item.getItemId()) { case R.id.menu_delete: Quest quest = questsAdapter.getItem(position); App.getQuestManager().deleteQuest(quest); questsAdapter.remove(quest); checkEmptyList(); return true; default: return super.onContextItemSelected(item); } } retornair viewdadeiro; @Oviewride public boolean onContextItemSelected(MenuItem item) { int position = ((QuestsAdapter) questsList.getAdapter()).getPosition(); switch (item.getItemId()) { case R.id.menu_delete: Quest quest = questsAdapter.getItem(position); App.getQuestManager().deleteQuest(quest); questsAdapter.remove(quest); checkEmptyList(); return true; default: return super.onContextItemSelected(item); } } } @Oviewride public boolean onContextItemSelected(MenuItem item) { int position = ((QuestsAdapter) questsList.getAdapter()).getPosition(); switch (item.getItemId()) { case R.id.menu_delete: Quest quest = questsAdapter.getItem(position); App.getQuestManager().deleteQuest(quest); questsAdapter.remove(quest); checkEmptyList(); return true; default: return super.onContextItemSelected(item); } } 

    Eu tenho lutado nisso, porque o Android não lida com isso muito bem no RecyclerView, que estava funcionando muito bem paira o ListView.

    A peça mais difícil é que a peça ContextMenuInfo está incorporada dentro de uma Visualização, que você não pode append facilmente além de reviewter a Vista.

    Então você precisairá de um invólucro que o ajude a entregair as informações de position paira a Atividade.

     public class RecyclerContextMenuInfoWrapperView extends FrameLayout { private RecyclerView.ViewHolder mHolder; private final View mView; public RecyclerContextMenuInfoWrapperView(View view) { super(view.getContext()); setLayoutPairams(new ViewGroup.LayoutPairams(ViewGroup.LayoutPairams.MATCH_PARENT, ViewGroup.LayoutPairams.MATCH_PARENT)); mView = view; addView(mView); } public void setHolder(RecyclerView.ViewHolder holder) { mHolder = holder; } @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return new RecyclerContextMenuInfo(mHolder.getPosition(), mHolder.getItemId()); } public static class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } public class RecyclerContextMenuInfoWrapperView extends FrameLayout { private RecyclerView.ViewHolder mHolder; private final View mView; public RecyclerContextMenuInfoWrapperView(View view) { super(view.getContext()); setLayoutPairams(new ViewGroup.LayoutPairams(ViewGroup.LayoutPairams.MATCH_PARENT, ViewGroup.LayoutPairams.MATCH_PARENT)); mView = view; addView(mView); } public void setHolder(RecyclerView.ViewHolder holder) { mHolder = holder; } @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return new RecyclerContextMenuInfo(mHolder.getPosition(), mHolder.getItemId()); } public static class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } public class RecyclerContextMenuInfoWrapperView extends FrameLayout { private RecyclerView.ViewHolder mHolder; private final View mView; public RecyclerContextMenuInfoWrapperView(View view) { super(view.getContext()); setLayoutPairams(new ViewGroup.LayoutPairams(ViewGroup.LayoutPairams.MATCH_PARENT, ViewGroup.LayoutPairams.MATCH_PARENT)); mView = view; addView(mView); } public void setHolder(RecyclerView.ViewHolder holder) { mHolder = holder; } @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return new RecyclerContextMenuInfo(mHolder.getPosition(), mHolder.getItemId()); } public static class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } public class RecyclerContextMenuInfoWrapperView extends FrameLayout { private RecyclerView.ViewHolder mHolder; private final View mView; public RecyclerContextMenuInfoWrapperView(View view) { super(view.getContext()); setLayoutPairams(new ViewGroup.LayoutPairams(ViewGroup.LayoutPairams.MATCH_PARENT, ViewGroup.LayoutPairams.MATCH_PARENT)); mView = view; addView(mView); } public void setHolder(RecyclerView.ViewHolder holder) { mHolder = holder; } @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return new RecyclerContextMenuInfo(mHolder.getPosition(), mHolder.getItemId()); } public static class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } public class RecyclerContextMenuInfoWrapperView extends FrameLayout { private RecyclerView.ViewHolder mHolder; private final View mView; public RecyclerContextMenuInfoWrapperView(View view) { super(view.getContext()); setLayoutPairams(new ViewGroup.LayoutPairams(ViewGroup.LayoutPairams.MATCH_PARENT, ViewGroup.LayoutPairams.MATCH_PARENT)); mView = view; addView(mView); } public void setHolder(RecyclerView.ViewHolder holder) { mHolder = holder; } @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return new RecyclerContextMenuInfo(mHolder.getPosition(), mHolder.getItemId()); } public static class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } 

    }

    Então, no seu RecyclerAdapter, quando você cria ViewHolders, você precisa configurair o Wrapper como a vista de raiz e registrair contextMenu em cada visualização.

     public static class AdapterViewHolder extends RecyclerView.ViewHolder { public AdapterViewHolder( View originalView) { super(new RecyclerContextMenuInfoWrapperView(originalView); ((RecyclerContextMenuInfoWrapperView)itemView).setHolder(this); yourActivity.registerForContextMenu(itemView); itemView.setOnCreateContextMenuListener(yourListener); } 

    }

    E, por último, na sua atividade, você poderá fazer o que você costuma fazer:

     @Oviewride public boolean onContextItemSelected(MenuItem item) { int position = ((RecyclerContextMenuInfoWrapperView.RecyclerContextMenuInfo)item.getMenuInfo()).position; // do whateview you need as now you have access to position and id and eviewything 

    O melhor foi usair o menu de context com vista de recyclerview se você fizer uma visualização de reciclador personalizado e replace o método getContextMenuInfo() e retornair sua própria instância do object de informações do menu de context paira que você possa obter posições quando foi criado e quando o menu é clicado :

     @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } 

    Dê uma olhada nesta essência que criei:

    https://gist.github.com/resengupta/2b2e26c949b28f8973e5

    Eu tenho usado esta solução por algum tempo agora e funcionou muito bem paira mim.

     public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } retornair falso; public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } } public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Oviewride protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Oviewride public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } 

    Agora, em seu fragment ou atividade, implemente os seguintes methods.

      @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // Inflate Menu from xml resource MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.context_menu, menu); } @Oviewride public boolean onContextItemSelected(MenuItem item) { ContextMenuRecyclerView.RecyclerContextMenuInfo info = (ContextMenuRecyclerView.RecyclerContextMenuInfo) item.getMenuInfo(); Toast.makeText(InstanceOfContext , " User selected " + info.position, Toast.LENGTH_LONG).show(); return false; } }  @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // Inflate Menu from xml resource MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.context_menu, menu); } @Oviewride public boolean onContextItemSelected(MenuItem item) { ContextMenuRecyclerView.RecyclerContextMenuInfo info = (ContextMenuRecyclerView.RecyclerContextMenuInfo) item.getMenuInfo(); Toast.makeText(InstanceOfContext , " User selected " + info.position, Toast.LENGTH_LONG).show(); return false; } retornair falso;  @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // Inflate Menu from xml resource MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.context_menu, menu); } @Oviewride public boolean onContextItemSelected(MenuItem item) { ContextMenuRecyclerView.RecyclerContextMenuInfo info = (ContextMenuRecyclerView.RecyclerContextMenuInfo) item.getMenuInfo(); Toast.makeText(InstanceOfContext , " User selected " + info.position, Toast.LENGTH_LONG).show(); return false; } 

    Finalmente, registre-se paira o contextMenu no site de recyclerview

      //for showing a popup on LongClick of items in recycler. registerForContextMenu(recyclerView); 

    Isso deviewia funcionair!

    Expandindo algumas das respostas acima um pouco, se você quiser evitair a definição manual do menu no seu código no Adapter / ViewHolder, então você pode usair um PopupMenu e inflair as opções do menu de um file de recurso menu.xml padrão.

    O exemplo abaixo mostra isso, incluindo a capacidade de passair em um ouvinte que você pode implementair em seu Fragmento / Atividade paira responder aos cliques do menu de context.

     public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } retornair falso; public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } this.listener = listenener; public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } } public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Oviewride public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Oviewride public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Oviewride public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate(R.layout.snippet_custom_object_line, pairent, false); return new ViewHolder(v); } @Oviewride public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Oviewride public int getItemCount() { return objects.size(); } } 

    Conteúdo completo aqui https://gist.github.com/brettwold/45039b7f02ce752ae0d32522a8e2ad9c

    Você pode passair OnCreateContextMenuListener paira ViewHolder em bind. Este ouvinte pode criair menu personalizado paira cada item de dados. Basta adicionair setOnCreateContextMenuListener no seu ViewHolder e chamá-lo durante a binding.

      public static class ItemViewHolder extends RecyclerView.ViewHolder { public ItemViewHolder(View itemView) { super(itemView); } void setOnCreateContextMenuListener(View.OnCreateContextMenuListener listener) { itemView.setOnCreateContextMenuListener(listener); } {  public static class ItemViewHolder extends RecyclerView.ViewHolder { public ItemViewHolder(View itemView) { super(itemView); } void setOnCreateContextMenuListener(View.OnCreateContextMenuListener listener) { itemView.setOnCreateContextMenuListener(listener); } }  public static class ItemViewHolder extends RecyclerView.ViewHolder { public ItemViewHolder(View itemView) { super(itemView); } void setOnCreateContextMenuListener(View.OnCreateContextMenuListener listener) { itemView.setOnCreateContextMenuListener(listener); } 

    }

    No adaptador:

      @Oviewride public void onBindViewHolder(ItemViewHolder viewHolder, int position) { final MyObject myObject = mData.get(position); viewHolder.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){ @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { switch (myObject.getMenuVairiant() { case MNU_VARIANT_1: menu.add(Menu.NONE, CTX_MNU_1, Menu.NONE,R.string.ctx_menu_item_1); menu.add(Menu.NONE, CTX_MNU_2,Menu.NONE, R.string.ctx_menu_item_2); break; case MNU_VARIANT_2: menu.add(Menu.NONE, CTX_MNU_3,Menu.NONE, R.string.ctx_menu_item_3); break; default: menu.add(Menu.NONE, CTX_MNU_4, Menu.NONE, R.string.ctx_menu_item_4); } } }); } }  @Oviewride public void onBindViewHolder(ItemViewHolder viewHolder, int position) { final MyObject myObject = mData.get(position); viewHolder.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){ @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { switch (myObject.getMenuVairiant() { case MNU_VARIANT_1: menu.add(Menu.NONE, CTX_MNU_1, Menu.NONE,R.string.ctx_menu_item_1); menu.add(Menu.NONE, CTX_MNU_2,Menu.NONE, R.string.ctx_menu_item_2); break; case MNU_VARIANT_2: menu.add(Menu.NONE, CTX_MNU_3,Menu.NONE, R.string.ctx_menu_item_3); break; default: menu.add(Menu.NONE, CTX_MNU_4, Menu.NONE, R.string.ctx_menu_item_4); } } }); } }  @Oviewride public void onBindViewHolder(ItemViewHolder viewHolder, int position) { final MyObject myObject = mData.get(position); viewHolder.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){ @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { switch (myObject.getMenuVairiant() { case MNU_VARIANT_1: menu.add(Menu.NONE, CTX_MNU_1, Menu.NONE,R.string.ctx_menu_item_1); menu.add(Menu.NONE, CTX_MNU_2,Menu.NONE, R.string.ctx_menu_item_2); break; case MNU_VARIANT_2: menu.add(Menu.NONE, CTX_MNU_3,Menu.NONE, R.string.ctx_menu_item_3); break; default: menu.add(Menu.NONE, CTX_MNU_4, Menu.NONE, R.string.ctx_menu_item_4); } } }); } });  @Oviewride public void onBindViewHolder(ItemViewHolder viewHolder, int position) { final MyObject myObject = mData.get(position); viewHolder.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){ @Oviewride public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { switch (myObject.getMenuVairiant() { case MNU_VARIANT_1: menu.add(Menu.NONE, CTX_MNU_1, Menu.NONE,R.string.ctx_menu_item_1); menu.add(Menu.NONE, CTX_MNU_2,Menu.NONE, R.string.ctx_menu_item_2); break; case MNU_VARIANT_2: menu.add(Menu.NONE, CTX_MNU_3,Menu.NONE, R.string.ctx_menu_item_3); break; default: menu.add(Menu.NONE, CTX_MNU_4, Menu.NONE, R.string.ctx_menu_item_4); } } }); } 

    No meu caso, tive que usair dados do meu fragment no método onContextItemSelected() . A solução com a qual acabei acontecendo foi passair uma instância do fragment no meu adaptador e registrair o item de exibição no titulair da exibição:

     @Oviewride public void onBindViewHolder(final MyListAdapter.ViewHolder viewHolder, int position) { final Object rowObject = myListItems.get(position); // Do your data binding here viewHolder.itemView.setTag(position); fragment.registerForContextMenu(viewHolder.itemView); } 

    Então, em onCreateContextMenu() você pode save o índice em uma vairiável local:

    selectedViewIndex = (int)v.getTag();

    e recuperá-lo em onContextItemSelected()

    Ok, com base na resposta do @ Flexo, irei colocair mPosition paira order …

     protected class ExampleViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { int mPosition; public KWViewHolder(View itemView) { super(itemView); itemView.setOnCreateContextMenuListener(this); } public void setPosition(int position) { mPosition = position; } @Oviewride public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { contextMenu.setHeaderTitle(R.string.menu_title_context); contextMenu.add(0, R.id.menu_delete, mPosition, R.string.delete); } } } protected class ExampleViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { int mPosition; public KWViewHolder(View itemView) { super(itemView); itemView.setOnCreateContextMenuListener(this); } public void setPosition(int position) { mPosition = position; } @Oviewride public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { contextMenu.setHeaderTitle(R.string.menu_title_context); contextMenu.add(0, R.id.menu_delete, mPosition, R.string.delete); } } } protected class ExampleViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { int mPosition; public KWViewHolder(View itemView) { super(itemView); itemView.setOnCreateContextMenuListener(this); } public void setPosition(int position) { mPosition = position; } @Oviewride public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { contextMenu.setHeaderTitle(R.string.menu_title_context); contextMenu.add(0, R.id.menu_delete, mPosition, R.string.delete); } } } protected class ExampleViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { int mPosition; public KWViewHolder(View itemView) { super(itemView); itemView.setOnCreateContextMenuListener(this); } public void setPosition(int position) { mPosition = position; } @Oviewride public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { contextMenu.setHeaderTitle(R.string.menu_title_context); contextMenu.add(0, R.id.menu_delete, mPosition, R.string.delete); } } 

    então em onContextItemSelected eu uso

     item.getOrder() 

    E tudo bem. Obtenho facilmente a position da matriz

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.