Вот мое действие, которое имеет вид рециркулятора.
public class DisplayActivity extends AppCompatActivity implements MyAdapter.MyViewHolderEventListener {
RecyclerView r;
private int c_menu_pos; //position of the item from list for which context menu was to be displayed
List<Data> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
//get data from db and set mList
//initialize adapter
//set recycler view with adapter, animator and layout manager
}
//implementing click listener
@Override
public void onItemClickListener(Data d, int pos) {
Log.d(TAG, "Killing data with id "+d.getData());
String txt = "DATA";
Toast.makeText(DisplayActivity.this, txt, Toast.LENGTH_LONG).show();
}
//implementing the long click listener call back
@Override
public boolean onItemLongClickListener(View v, Data d, int pos) {
Log.d(TAG, "Inside onItemLongClickListener");
c_menu_pos = pos;
registerForContextMenu(v);
v.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
Log.d(TAG, "Inside onCreateContextMenu");
//super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
getMenuInflater().inflate(R.menu.context_menu, contextMenu);
}
});
return true;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.c_delete:
Toast.makeText(DisplayActivity.this, mList.get(c_menu_pos).toString(), Toast.LENGTH_LONG).show();
break;
case R.id.c_cancel:
Toast.makeText(DisplayActivity.this, "Cancelled for id : "+c_menu_pos, Toast.LENGTH_LONG).show();
break;
}
return super.onContextItemSelected(item);
}
}
Вот логика / код для моего адаптера
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Data> mData; //initialized in the adapter constructor
public MyViewHolderEventListener mListener; //initialized in the adapter constructor
public static class MyViewHolder extends RecyclerView.ViewHolder{
View item;
TextView t1;
public AppViewHolder(View item){
super(item);
this.item = item;
t1 = (TextView)item.findViewById(R.id.text1);
}
}
//in onCreateViewHolder I inflate the card view
//in onBindViewHolder I initialize the views in my view holder with the data and also set click and long click listeners to my View Holder event listener methods
public interface MyViewHolderEventListener{
//handle on click event
void onItemClickListener(Data d, int pos);
//handle long click event
boolean onItemLongClickListener(View v, Data d, int pos);
}
}
Мое приложение работает нормально только при запуске приложения журналыпрочитайте: «Внутри onItemLongClickListener» Но не печатайте «Внутри onCreateContextMenu».Пожалуйста, помогите мне понять, что я делаю неправильно.