[Android] HistoryFragment: add a context menu

Edward Wang git at videolan.org
Sun Jul 21 20:17:47 CEST 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Thu Jul  4 21:44:03 2013 -0400| [4161cd02306e99f5523850d86b42fd3a018fe126] | committer: Edward Wang

HistoryFragment: add a context menu

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

 vlc-android/res/menu/history_view.xml              |    9 +++++
 .../src/org/videolan/vlc/gui/HistoryFragment.java  |   40 +++++++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/vlc-android/res/menu/history_view.xml b/vlc-android/res/menu/history_view.xml
new file mode 100644
index 0000000..900a632
--- /dev/null
+++ b/vlc-android/res/menu/history_view.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android" >
+    <item
+        android:id="@+id/history_view_play"
+        android:title="@string/play" />
+    <item
+        android:id="@+id/history_view_delete"
+        android:title="@string/delete" />
+</menu>
diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
index fc6b98b..fec29a4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
@@ -20,15 +20,21 @@
  *****************************************************************************/
 package org.videolan.vlc.gui;
 
+import org.videolan.libvlc.LibVLC;
 import org.videolan.vlc.AudioServiceController;
 import org.videolan.vlc.R;
 import org.videolan.vlc.gui.audio.AudioPlayerActivity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.ListView;
 
 import com.actionbarsherlock.app.SherlockListFragment;
@@ -54,19 +60,51 @@ public class HistoryFragment extends SherlockListFragment {
     {
         View v = inflater.inflate(R.layout.history_list, container, false);
         setListAdapter(mHistoryAdapter);
+        final ListView listView = (ListView)v.findViewById(android.R.id.list);
+        registerForContextMenu(listView);
         return v;
     }
 
     @Override
+    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+        MenuInflater menuInflater = getActivity().getMenuInflater();
+        menuInflater.inflate(R.menu.history_view, menu);
+    }
+
+    @Override
     public void onListItemClick(ListView l, View v, int p, long id) {
+        playListIndex(p);
+    }
+
+    private void playListIndex(int position) {
         AudioServiceController audioController = AudioServiceController.getInstance();
 
-        audioController.load(mHistoryAdapter.getAllURIs(), p, true, true);
+        audioController.load(mHistoryAdapter.getAllURIs(), position, true, true);
         Intent intent = new Intent(getActivity(), AudioPlayerActivity.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         startActivity(intent);
     }
 
+    @Override
+    public boolean onContextItemSelected(MenuItem item) {
+        if(!getUserVisibleHint()) return super.onContextItemSelected(item);
+
+        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
+        if(info == null) // info can be null
+            return super.onContextItemSelected(item);
+        int id = item.getItemId();
+
+        if(id == R.id.history_view_play) {
+            playListIndex(info.position);
+            return true;
+        } else if(id == R.id.history_view_delete) {
+            LibVLC.getExistingInstance().removeIndex(info.position);
+            mHistoryAdapter.notifyDataSetChanged();
+            return true;
+        }
+        return super.onContextItemSelected(item);
+    }
+
     public void refresh() {
         Log.d(TAG, "Refreshing view!");
         if( mHistoryAdapter != null )



More information about the Android mailing list