BaseAdapter and ContextMenu

Hi community stackoverflow,

Basically, I have a gallery displaying some images using gridView + imageView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <GridView android:id="@+id/PhoneImageGrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="12dp" android:horizontalSpacing="12dp" android:columnWidth="90dp" android:stretchMode="columnWidth" android:gravity="center" /> <ImageView android:id="@+id/thumbImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:scaleType="centerCrop" /> 

I would like to use setOnLongClick for each image displayed by the adapter. This works well, however, when you click long on the imageView, I would like to display ContextMenu with some elements (i.e. you long click on imageView, the MenU context is displayed with some elements: Image information, send this image ...). Unfortunately, I cannot figure out how to inflate this menu in the adapter. (This is probably not the best way to do this)

I have the following lines in my main activity

 _adapter = new ImageAdapter(activity,storedObjects.getAlbums()); imagegrid.setAdapter(_adapter); 

My adapter (removed some unnecessary lines)

  public class ImageAdapter extends BaseAdapter { private Albums albums; private Context context; private LayoutInflater inflater; public ImageAdapter(Context context, Albums albums) { this.albums = albums; this.context = context; inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); if(albums.getAlbumsListSize() == 0) { Toast.makeText(context, "There is no album to display", Toast.LENGTH_LONG).show(); } } public View getView(final int position, View view, ViewGroup parent) { ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.galleryitem, null); holder.imageview = (ImageView) view.findViewById(R.id.thumbImage); holder.checkbox = (CheckBox) view.findViewById(R.id.itemCheckBox); holder.textview = (TextView) view.findViewById(R.id.album_name); holder.checkbox.setChecked(true); //Bitmap loadingBM = BitmapFactory.decodeResource(context.getResources(),R.drawable.loading_image); //holder.imageview.setImageBitmap(loadingBM); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.imageview.setClickable(true); holder.imageview.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View v) { Log.v(TAG,"onLongClick ok !"); return false; } }); imageDownloader.download(this.context, albums.getAllAlbums().get(position).getThumbnailUri(), holder.imageview); return view; } 

Questions:

  • setOnLongClickListener works correctly when I click on an image, my log is displayed in logcat, however, how to create a menu for each image?

Apparently, I can override onCreateContextMenu in my main activity. I think I could pass each ImageView to onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo), but how?

I would be very grateful if you could help me with this.

Thank you very much

Floren Valdeliev

+4
source share
2 answers

Instead of setOnLongClickListener on an ImageView call registerForContextMenu using a GridView . Then do onCreateContextMenu and onContextItemSelected .

Here is a simple ListActivity to show you how it works.

 public class GreetingActivity extends ListActivity { private static final String[] mGreetings = { "Hello", "Goodbye" }; private static final String[] mPeople = { "Alice", "Bob", "Charlie" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPeople); setListAdapter(adapter); ListView listView = getListView(); registerForContextMenu(listView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { for (int i = 0; i < mGreetings.length; ++i) { String greeting = mGreetings[i]; menu.add(v.getId(), i, ContextMenu.NONE, "Say " + greeting); } } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); int adapterPosition = menuInfo.position; String person = mPeople[adapterPosition]; int menuItemId = item.getItemId(); String greeting = mGreetings[menuItemId]; String msg = String.format("%s, %s!", greeting, person); Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); return true; } } 
+2
source

Thanks a lot @chiuki, it works as expected

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... final GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid); registerForContextMenu(imagegrid); storedObjects.storeThumbnailsURI(); _adapter = new ImageAdapter(activity,storedObjects.getAlbums()); imagegrid.setAdapter(_adapter); } @Override public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; Log.v("context menu","context menu"); menu.setHeaderTitle("Context Menu"); menu.add(0, START_SLIDESHOW_ON_THIS_ALBUM, 0, "Start SlideShow for this Album"); menu.add(0, DOWNLOAD_WHOLE_ALBUM, 0, "Download this Album"); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case START_SLIDESHOW_ON_THIS_ALBUM: selectThisAlbumOnly(info); startSlideShow(); break; case DOWNLOAD_WHOLE_ALBUM: break; } return true; } 

In the adapter, make sure you do not have setClickable = true

Greetings

Florent

+1
source

Source: https://habr.com/ru/post/1385409/


All Articles