Working context menu callbacks
This commit is contained in:
		
							
								
								
									
										4
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										4
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							| @@ -3,7 +3,11 @@ | ||||
|   <component name="DesignSurface"> | ||||
|     <option name="filePathToZoomLevelMap"> | ||||
|       <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/menu/list_context_menu.xml" value="0.41944444444444445" /> | ||||
|       </map> | ||||
|     </option> | ||||
|   </component> | ||||
|   | ||||
| @@ -23,14 +23,18 @@ package it.danieleverducci.nextcloudmaps.activity.main; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import android.text.Html; | ||||
| import android.util.Log; | ||||
| import android.view.ContextMenu; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.Menu; | ||||
| import android.view.MenuInflater; | ||||
| import android.view.MenuItem; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.Filter; | ||||
| import android.widget.Filterable; | ||||
| import android.widget.ImageView; | ||||
| import android.widget.PopupMenu; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| 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 static final String TAG = "GeofavoriteAdapter"; | ||||
|  | ||||
|     public static final int SORT_BY_TITLE = 0; | ||||
|     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 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) { | ||||
|         this.context = context; | ||||
|         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; | ||||
|         ImageView bt_context_menu; | ||||
|         ImageView bt_share; | ||||
| @@ -170,22 +179,17 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter. | ||||
|         public void onClick(View view) { | ||||
|             switch (view.getId()) { | ||||
|                 case R.id.geofav_context_menu_bt: | ||||
|                     openContextMenu(view); | ||||
|                     onOverflowIconClicked(view, getAdapterPosition()); | ||||
|                     break; | ||||
|                 case R.id.geofav_share_bt: | ||||
|                     if (itemClickListener != null) | ||||
|                         itemClickListener.onItemShareClick(get(getAdapterPosition())); | ||||
|                     break; | ||||
|                 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() { | ||||
| @@ -196,17 +200,37 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter. | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void openContextMenu(View v) { | ||||
|         v.showContextMenu(); | ||||
|         //.showContextMenuForChild(v); | ||||
|  | ||||
|     private void onOverflowIconClicked(View view, int position) { | ||||
|         // 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 { | ||||
|         void onItemClick(View view, int position); | ||||
|         void onItemClick(Geofavorite item); | ||||
|         void onItemShareClick(Geofavorite item); | ||||
|         void onItemDetailsClick(Geofavorite item); | ||||
|         void onItemDeleteClick(Geofavorite item); | ||||
|     } | ||||
|  | ||||
|     public interface ContextMenuClickListener { | ||||
|         void onContextMenuClick(); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -21,6 +21,7 @@ import android.content.Intent; | ||||
| import android.content.SharedPreferences; | ||||
| import android.os.Bundle; | ||||
| import android.text.TextUtils; | ||||
| import android.util.Log; | ||||
| import android.view.View; | ||||
| import android.widget.Toast; | ||||
|  | ||||
| @@ -103,8 +104,7 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti | ||||
|  | ||||
|         rvItemClickListener = new ItemClickListener() { | ||||
|             @Override | ||||
|             public void onItemClick(View view, int position) { | ||||
|                 Geofavorite geofavorite = geofavoriteAdapter.get(position); | ||||
|             public void onItemClick(Geofavorite geofavorite) { | ||||
|                 Intent i = new Intent(); | ||||
|                 i.setAction(Intent.ACTION_VIEW); | ||||
|                 i.setData(geofavorite.getGeoUri()); | ||||
| @@ -122,14 +122,22 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti | ||||
|                 i.putExtra(Intent.EXTRA_TEXT, shareMessage ); | ||||
|                 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); | ||||
|         recyclerView.setAdapter(geofavoriteAdapter); | ||||
|         geofavoriteAdapter.setSortRule(sortRule); | ||||
|  | ||||
|         registerForContextMenu(recyclerView); | ||||
|  | ||||
|         swipeRefresh = findViewById(R.id.swipe_refresh); | ||||
|         swipeRefresh.setOnRefreshListener(() -> presenter.getGeofavorites()); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user