[Android] Add a popup menu to the video list

Ludovic Fauvet git at videolan.org
Thu Oct 18 18:43:19 CEST 2012


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Thu Oct 18 17:32:25 2012 +0200| [9f869c5981bbfbdce319bc6396de6575126999b2] | committer: Ludovic Fauvet

Add a popup menu to the video list

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=9f869c5981bbfbdce319bc6396de6575126999b2
---

 .../res/drawable-hdpi/dropdown_ic_arrow.png        |  Bin 0 -> 405 bytes
 vlc-android/res/layout/video_list_item.xml         |   18 ++++++-
 .../videolan/vlc/gui/video/VideoGridFragment.java  |   54 +++++++++++++++-----
 .../videolan/vlc/gui/video/VideoListAdapter.java   |   18 ++++++-
 4 files changed, 73 insertions(+), 17 deletions(-)

diff --git a/vlc-android/res/drawable-hdpi/dropdown_ic_arrow.png b/vlc-android/res/drawable-hdpi/dropdown_ic_arrow.png
new file mode 100644
index 0000000..b659926
Binary files /dev/null and b/vlc-android/res/drawable-hdpi/dropdown_ic_arrow.png differ
diff --git a/vlc-android/res/layout/video_list_item.xml b/vlc-android/res/layout/video_list_item.xml
index b602da5..5d1bc6d 100644
--- a/vlc-android/res/layout/video_list_item.xml
+++ b/vlc-android/res/layout/video_list_item.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout
+<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/layout_item"
     android:layout_width="fill_parent"
@@ -7,8 +7,10 @@
     android:orientation="horizontal"
     android:gravity="center_vertical">
     <LinearLayout
+        android:id="@+id/ml_item_layout_thumbnail"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
+        android:layout_alignParentLeft="true"
         android:orientation="vertical"
         android:background="@drawable/dropshadow">
         <ImageView
@@ -30,6 +32,8 @@
     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
+        android:layout_toRightOf="@+id/ml_item_layout_thumbnail"
+        android:layout_centerVertical="true"
         android:orientation="vertical">
         <TextView
             android:id="@+id/ml_item_title"
@@ -48,4 +52,14 @@
             android:textColor="@color/list_subtitle"
             android:paddingLeft="12dip" />
     </LinearLayout>
-</LinearLayout>
\ No newline at end of file
+    <ImageView
+        android:id="@+id/ml_item_more"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:paddingBottom="8dip"
+        android:src="@drawable/dropdown_ic_arrow"
+        android:layout_alignParentBottom="true"
+        android:layout_alignParentRight="true"
+        android:clickable="true"
+        android:background="@android:drawable/list_selector_background"/>
+</RelativeLayout>
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index ecc8cbb..69a1af1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -38,6 +38,7 @@ import org.videolan.vlc.gui.CommonDialogs;
 import org.videolan.vlc.gui.PreferencesActivity;
 import org.videolan.vlc.interfaces.ISortable;
 
+import android.annotation.TargetApi;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.BroadcastReceiver;
@@ -61,6 +62,8 @@ import android.view.ViewGroup;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.GridView;
 import android.widget.LinearLayout;
+import android.widget.PopupMenu;
+import android.widget.PopupMenu.OnMenuItemClickListener;
 import android.widget.TextView;
 
 public class VideoGridFragment extends SherlockGridFragment implements ISortable {
@@ -97,7 +100,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        mVideoAdapter = new VideoListAdapter(getActivity());
+        mVideoAdapter = new VideoListAdapter(getActivity(), this);
         mMediaLibrary = MediaLibrary.getInstance(getActivity());
         setListAdapter(mVideoAdapter);
     }
@@ -239,28 +242,20 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         VideoPlayerActivity.start(getActivity(), item.getLocation());
     }
 
-    @Override
-    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
-        MenuInflater inflater = getActivity().getMenuInflater();
-        inflater.inflate(R.menu.video_list, menu);
-    }
-
-    @Override
-    public boolean onContextItemSelected(MenuItem menu) {
-        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menu.getMenuInfo();
+    private boolean handleContextItemSelected(MenuItem menu, int position) {
         switch (menu.getItemId())
         {
         case R.id.video_list_play:
-            playVideo(info.position);
+            playVideo(position);
             return true;
         case R.id.video_list_info:
             Intent intent = new Intent(getActivity(), MediaInfoActivity.class);
             intent.putExtra("itemLocation",
-                    mVideoAdapter.getItem(info.position).getLocation());
+                    mVideoAdapter.getItem(position).getLocation());
             startActivity(intent);
             return true;
         case R.id.video_list_delete:
-            final int positionDelete = info.position;
+            final int positionDelete = position;
             AlertDialog alertDialog = CommonDialogs.deleteMedia(
                     getActivity(),
                     mVideoAdapter.getItem(positionDelete).getLocation(),
@@ -273,9 +268,42 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
             alertDialog.show();
             return true;
         }
+        return false;
+    }
+
+    @Override
+    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+        MenuInflater inflater = getActivity().getMenuInflater();
+        inflater.inflate(R.menu.video_list, menu);
+    }
+
+    @Override
+    public boolean onContextItemSelected(MenuItem menu) {
+        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menu.getMenuInfo();
+        if (handleContextItemSelected(menu, info.position))
+            return true;
         return super.onContextItemSelected(menu);
     }
 
+    @TargetApi(11)
+    public void onContextPopupMenu(View anchor, final int position) {
+        if (!Util.isHoneycombOrLater()) {
+            // Call the "classic" context menu
+            anchor.performLongClick();
+            return;
+        }
+
+        PopupMenu popupMenu = new PopupMenu(getActivity(), anchor);
+        popupMenu.getMenuInflater().inflate(R.menu.video_list, popupMenu.getMenu());
+        popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+            @Override
+            public boolean onMenuItemClick(MenuItem item) {
+                return handleContextItemSelected(item, position);
+            }
+        });
+        popupMenu.show();
+    }
+
     /**
      * Handle changes on the list
      */
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 14f13dd..8fcb392 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -34,6 +34,7 @@ import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.GridView;
@@ -49,9 +50,11 @@ public class VideoListAdapter extends ArrayAdapter<Media>
     private int mSortDirection = 1;
     private int mSortBy = SORT_BY_TITLE;
     private boolean mListMode = false;
+    private VideoGridFragment mFragment;
 
-    public VideoListAdapter(Context context) {
+    public VideoListAdapter(Context context, VideoGridFragment fragment) {
         super(context, 0);
+        mFragment = fragment;
     }
 
     public final static String TAG = "VLC/MediaLibraryAdapter";
@@ -123,7 +126,7 @@ public class VideoListAdapter extends ArrayAdapter<Media>
      * Display the view of a file browser item.
      */
     @Override
-    public View getView(int position, View convertView, ViewGroup parent) {
+    public View getView(final int position, View convertView, ViewGroup parent) {
         ViewHolder holder;
         View v = convertView;
         if (v == null || (((ViewHolder)v.getTag()).listmode != mListMode)) {
@@ -187,6 +190,17 @@ public class VideoListAdapter extends ArrayAdapter<Media>
 
         holder.subtitle.setText(text);
 
+        /* popup menu */
+        final ImageView more = (ImageView)v.findViewById(R.id.ml_item_more);
+        if (more != null && mFragment != null) {
+            more.setOnClickListener(new OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    mFragment.onContextPopupMenu(more, position);
+                }
+            });
+        }
+
         return v;
     }
 



More information about the Android mailing list