Working context menu callbacks

This commit is contained in:
Daniele 2021-09-07 20:52:37 +02:00
parent bd3ca1740a
commit 9c83707bfc
3 changed files with 56 additions and 20 deletions

View File

@ -3,7 +3,11 @@
<component name="DesignSurface"> <component name="DesignSurface">
<option name="filePathToZoomLevelMap"> <option name="filePathToZoomLevelMap">
<map> <map>
<entry key="../../../../layout/custom_preview.xml" value="0.5661458333333333" />
<entry key="app/src/main/res/layout/activity_list_view.xml" value="0.5307291666666667" />
<entry key="app/src/main/res/layout/activity_main.xml" value="0.5307291666666667" />
<entry key="app/src/main/res/layout/item_geofav.xml" value="0.5307291666666667" /> <entry key="app/src/main/res/layout/item_geofav.xml" value="0.5307291666666667" />
<entry key="app/src/main/res/menu/list_context_menu.xml" value="0.41944444444444445" />
</map> </map>
</option> </option>
</component> </component>

View File

@ -23,14 +23,18 @@ package it.danieleverducci.nextcloudmaps.activity.main;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.text.Html; import android.text.Html;
import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Filter; import android.widget.Filter;
import android.widget.Filterable; import android.widget.Filterable;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -46,6 +50,8 @@ import it.danieleverducci.nextcloudmaps.model.Geofavorite;
public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.GeofavoriteViewHolder> implements Filterable { public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.GeofavoriteViewHolder> implements Filterable {
public static final String TAG = "GeofavoriteAdapter";
public static final int SORT_BY_TITLE = 0; public static final int SORT_BY_TITLE = 0;
public static final int SORT_BY_CREATED = 1; public static final int SORT_BY_CREATED = 1;
@ -56,6 +62,9 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
private List<Geofavorite> geofavoriteListFiltered = new ArrayList<>(); private List<Geofavorite> geofavoriteListFiltered = new ArrayList<>();
private int sortRule = SORT_BY_CREATED; private int sortRule = SORT_BY_CREATED;
// Contains the position of the element containing the overflow menu clicked
private int overflowMenuSelectedPosition = -1;
public GeofavoriteAdapter(Context context, ItemClickListener itemClickListener) { public GeofavoriteAdapter(Context context, ItemClickListener itemClickListener) {
this.context = context; this.context = context;
this.itemClickListener = itemClickListener; this.itemClickListener = itemClickListener;
@ -143,7 +152,7 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
} }
}; };
class GeofavoriteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener { class GeofavoriteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tv_title, tv_content; TextView tv_title, tv_content;
ImageView bt_context_menu; ImageView bt_context_menu;
ImageView bt_share; ImageView bt_share;
@ -170,22 +179,17 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.geofav_context_menu_bt: case R.id.geofav_context_menu_bt:
openContextMenu(view); onOverflowIconClicked(view, getAdapterPosition());
break; break;
case R.id.geofav_share_bt: case R.id.geofav_share_bt:
if (itemClickListener != null) if (itemClickListener != null)
itemClickListener.onItemShareClick(get(getAdapterPosition())); itemClickListener.onItemShareClick(get(getAdapterPosition()));
break; break;
default: default:
itemClickListener.onItemClick(view, getAdapterPosition()); if (itemClickListener != null)
itemClickListener.onItemClick(get(getAdapterPosition()));
} }
} }
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater menuInflater = new MenuInflater(context);
menuInflater.inflate(R.menu.list_context_menu, menu);
}
} }
private void performSort() { private void performSort() {
@ -196,17 +200,37 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
} }
} }
private void openContextMenu(View v) {
v.showContextMenu(); private void onOverflowIconClicked(View view, int position) {
//.showContextMenuForChild(v); // Save selected item
overflowMenuSelectedPosition = position;
// Open menu
PopupMenu popup = new PopupMenu(context, view);
popup.inflate(R.menu.list_context_menu);
popup.setOnMenuItemClickListener(this::optionsItemSelected);
popup.show();
}
private boolean optionsItemSelected(MenuItem item) {
if (overflowMenuSelectedPosition < 0) {
Log.e(TAG, "No overflow menu selected position saved!");
return false;
}
Geofavorite gf = get(overflowMenuSelectedPosition);
overflowMenuSelectedPosition = -1;
if (item.getItemId() == R.id.list_context_menu_detail && itemClickListener != null)
itemClickListener.onItemDetailsClick(gf);
if (item.getItemId() == R.id.list_context_menu_delete)
itemClickListener.onItemDeleteClick(gf);
return true;
} }
public interface ItemClickListener { public interface ItemClickListener {
void onItemClick(View view, int position); void onItemClick(Geofavorite item);
void onItemShareClick(Geofavorite item); void onItemShareClick(Geofavorite item);
void onItemDetailsClick(Geofavorite item);
void onItemDeleteClick(Geofavorite item);
} }
public interface ContextMenuClickListener {
void onContextMenuClick();
}
} }

View File

@ -21,6 +21,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Toast; import android.widget.Toast;
@ -103,8 +104,7 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti
rvItemClickListener = new ItemClickListener() { rvItemClickListener = new ItemClickListener() {
@Override @Override
public void onItemClick(View view, int position) { public void onItemClick(Geofavorite geofavorite) {
Geofavorite geofavorite = geofavoriteAdapter.get(position);
Intent i = new Intent(); Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW); i.setAction(Intent.ACTION_VIEW);
i.setData(geofavorite.getGeoUri()); i.setData(geofavorite.getGeoUri());
@ -122,14 +122,22 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti
i.putExtra(Intent.EXTRA_TEXT, shareMessage ); i.putExtra(Intent.EXTRA_TEXT, shareMessage );
startActivity(Intent.createChooser(i, getString(R.string.share_via))); startActivity(Intent.createChooser(i, getString(R.string.share_via)));
} }
@Override
public void onItemDetailsClick(Geofavorite item) {
Log.d("MENU", "Details "+item.getName());
}
@Override
public void onItemDeleteClick(Geofavorite item) {
Log.d("MENU", "Delete "+item.getName());
}
}; };
geofavoriteAdapter = new GeofavoriteAdapter(getApplicationContext(), rvItemClickListener); geofavoriteAdapter = new GeofavoriteAdapter(getApplicationContext(), rvItemClickListener);
recyclerView.setAdapter(geofavoriteAdapter); recyclerView.setAdapter(geofavoriteAdapter);
geofavoriteAdapter.setSortRule(sortRule); geofavoriteAdapter.setSortRule(sortRule);
registerForContextMenu(recyclerView);
swipeRefresh = findViewById(R.id.swipe_refresh); swipeRefresh = findViewById(R.id.swipe_refresh);
swipeRefresh.setOnRefreshListener(() -> presenter.getGeofavorites()); swipeRefresh.setOnRefreshListener(() -> presenter.getGeofavorites());