Android adding a popup context menu to a RecyclerView
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(); | |
} | |
} |
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