Last active
December 14, 2022 17:52
-
-
Save brettwold/45039b7f02ce752ae0d32522a8e2ad9c to your computer and use it in GitHub Desktop.
Android adding a popup context menu to a RecyclerView
This file contains hidden or 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 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); | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
int position = getAdapterPosition(); | |
if (listener != null) { | |
listener.onCustomerSelected(objects.get(position)); | |
} | |
} | |
@Override | |
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(); | |
} | |
@Override | |
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); | |
} | |
@Override | |
public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.snippet_custom_object_line, parent, false); | |
return new ViewHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { | |
CustomObject object = objects.get(position); | |
holder.name.setText(object.getName()); | |
holder.value.setText(object.getValue()); | |
} | |
@Override | |
public int getItemCount() { | |
return objects.size(); | |
} | |
} |
This file contains hidden or 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 CustomFragment extends Fragment implements CustomAdapter.OnItemSelectedListener { | |
@BindView(R.id.custom_objects) | |
RecyclerView objects; | |
private List<CustomObject> objects; | |
@Override | |
public void onResume() { | |
super.onResume(); | |
setupObjectList(); | |
} | |
private void setupObjectList() { | |
objects.setLayoutManager(new LinearLayoutManager(getContext())); | |
CustomAdapter customAdapter = new CustomAdapter(objects, this, true); | |
objects.setAdapter(customAdapter); | |
} | |
@Override | |
public void onSelected(CustomObject object) { | |
// Do something on select | |
} | |
@Override | |
public void onMenuAction(CustomObject object, MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.menu_custom_edit: | |
// Add edit screen | |
break; | |
case R.id.menu_custom_delete: | |
objects.remove(object); | |
setupObjectList(); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment