Created
September 21, 2018 06:31
-
-
Save shibbirweb/13f6b06ddd3f396a238fb7456a6cdbf9 to your computer and use it in GitHub Desktop.
[Android] Add Context Menu to RecyclerView with click method by interface (Control from YourActivity.java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context=".YourActivity"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/rv_your_activity_myRecylerView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
</android.support.v7.widget.RecyclerView> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<array name="context_menu_array"> | |
<item>Edit</item> | |
<item>Delete</item> | |
</array> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import package, classes etc; | |
public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder> { | |
private Context context; | |
private List<ModelClass> allData = new ArrayList<ModelClass>(); | |
private OnItemClickListener onItemClickListener; | |
private OnCreateContextMenu onCreateContextMenu; | |
private OnContextMenuItemClickListener onContextMenuItemClickListener; | |
CustomRecyclerAdapter(Context context, List<ModelClass> allData) { | |
this.context = context; | |
this.allData = allData; | |
} | |
@NonNull | |
@Override | |
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false); | |
return new ViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | |
holder.nameTv.setText(allData.get(position).getName()); | |
holder.phoneTv.setText(allData.get(position).getPhone()); | |
} | |
@Override | |
public int getItemCount() { | |
return allData.size(); | |
} | |
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener { | |
TextView nameTv, phoneTv; | |
ViewHolder(View itemView) { | |
super(itemView); | |
nameTv = itemView.findViewById(R.id.tv_row_item_name); | |
phoneTv = itemView.findViewById(R.id.tv_row_item_phone); | |
itemView.setOnClickListener(this); //register on item click listener | |
itemView.setOnCreateContextMenuListener(this); //register context menu listener for RecyclerView | |
} | |
@Override | |
public void onClick(View v) { | |
// for on item click listener | |
if (onItemClickListener != null) { | |
onItemClickListener.onItemClick(itemView, getAdapterPosition()); | |
} | |
} | |
@Override | |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | |
//for create context menu | |
if (onCreateContextMenu != null) { | |
onCreateContextMenu.onCreateContextMenu(menu, v, menuInfo, getAdapterPosition(), onMenuItemClickListener); | |
} | |
} | |
//context menu item click listener | |
private MenuItem.OnMenuItemClickListener onMenuItemClickListener = new MenuItem.OnMenuItemClickListener() { | |
@Override | |
public boolean onMenuItemClick(MenuItem item) { | |
if (onContextMenuItemClickListener != null) { | |
onContextMenuItemClickListener.onContextMenuItemClick(item, getAdapterPosition()); | |
} | |
return true; | |
} | |
}; | |
} | |
/* | |
===========================><============================ | |
* Custom interface for handle event from other class file | |
===========================><============================ | |
*/ | |
//Interface for onItemClickListener; | |
interface OnItemClickListener { | |
void onItemClick(View view, int position); | |
} | |
public void setOnItemClickListener(OnItemClickListener onItemClickListener) { | |
this.onItemClickListener = onItemClickListener; | |
} | |
//Interface for onCreateContextMenu | |
interface OnCreateContextMenu { | |
void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo, int position, MenuItem.OnMenuItemClickListener menuItemClickListener); | |
} | |
public void setOnCreateContextMenu(OnCreateContextMenu onCreateContextMenu) { | |
this.onCreateContextMenu = onCreateContextMenu; | |
} | |
//Interface for onContextMenuItemClickListener | |
interface OnContextMenuItemClickListener { | |
void onContextMenuItemClick(MenuItem menuItem, int position); | |
} | |
public void setOnContextMenuItemClickListener(OnContextMenuItemClickListener onContextMenuItemClickListener) { | |
this.onContextMenuItemClickListener = onContextMenuItemClickListener; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ModelClass { | |
private String name, phone; | |
public ModelClass() { | |
} | |
public ModelClass(String name, String phone) { | |
this.name = name; | |
this.phone = phone; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getPhone() { | |
return phone; | |
} | |
public void setPhone(String phone) { | |
this.phone = phone; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
app:cardElevation="3dp" | |
app:cardUseCompatPadding="true" | |
app:contentPadding="8dp" | |
android:clickable="true" | |
android:focusable="true" | |
android:foreground="?android:attr/selectableItemBackground" | |
app:cardCornerRadius="8dp"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="8dp" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/tv_row_item_name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/row_sample_name" | |
android:textSize="24sp" | |
android:textColor="@android:color/black"/> | |
<TextView | |
android:id="@+id/tv_row_item_phone" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/row_sample_number" | |
android:textSize="24sp" | |
android:textColor="@android:color/black"/> | |
</LinearLayout> | |
</android.support.v7.widget.CardView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class YourActivity extends AppCompatActivity { | |
private RecyclerView recyclerView; | |
private List<ModelClass> allData = new ArrayList<ModelClass>(); | |
private CustomRecyclerAdapter recyclerAdapter; | |
private String[] contextMenuArray; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_your); | |
recyclerView = (RecyclerView) findViewById(R.id.rv_your_activity_myRecylerView); | |
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext())); | |
allData.add(new ModelClass("Shibbir Ahmed", "0123456789")); | |
allData.add(new ModelClass("Rizwan", "9876543210")); | |
recyclerAdapter = new CustomRecyclerAdapter(this, allData); | |
recyclerView.setAdapter(recyclerAdapter); | |
contextMenuArray = getResources().getStringArray(R.array.context_menu_array); // init array from arrays.xml | |
//item click listener | |
recyclerAdapter.setOnItemClickListener(new CustomRecyclerAdapter.OnItemClickListener() { | |
@Override | |
public void onItemClick(View view, int position) { | |
// Write your item click event here | |
Toast.makeText(YourActivity.this, "Clicked on "+allData.get(position).getName(), Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
//on create context menu | |
recyclerAdapter.setOnCreateContextMenu(new CustomRecyclerAdapter.OnCreateContextMenu() { | |
@Override | |
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo, int position, MenuItem.OnMenuItemClickListener menuItemClickListener) { | |
// Create your context menu here | |
menu.setHeaderTitle(allData.get(position).getName()); // context menu header | |
for (int i = 0; i < contextMenuArray.length; i++) { | |
menu.add(Menu.NONE, i, i, contextMenuArray[i]).setOnMenuItemClickListener(menuItemClickListener); // created menu with click listener | |
} | |
} | |
}); | |
//on context menu item click listener | |
recyclerAdapter.setOnContextMenuItemClickListener(new CustomRecyclerAdapter.OnContextMenuItemClickListener() { | |
@Override | |
public void onContextMenuItemClick(MenuItem menuItem, int position) { | |
int menuItemId = menuItem.getItemId(); | |
String menuItemName = contextMenuArray[menuItemId]; | |
switch (menuItemName) { | |
case "Edit": | |
//write your edit click event | |
Toast.makeText(YourActivity.this, "Edit: "+allData.get(position).getName(), Toast.LENGTH_SHORT).show(); | |
break; | |
case "Delete": | |
//write your delete click event | |
Toast.makeText(YourActivity.this, "Delete: "+ allData.get(position).getName(), Toast.LENGTH_SHORT).show(); | |
break; | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment