[Android] Fix item selection for TV AudioPlayer

Geoffrey Métais git at videolan.org
Tue Nov 25 11:38:14 CET 2014


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Nov 25 11:29:55 2014 +0100| [dadede97fb6027cdd374296a29ea679cb8a8c1dc] | committer: Geoffrey Métais

Fix item selection for TV AudioPlayer

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

 .../tv/res/drawable/audio_item_selector.xml        |    7 +++++
 .../gui/tv/audioplayer/AudioPlayerActivity.java    |   28 ++++++++------------
 .../vlc/gui/tv/audioplayer/PlaylistAdapter.java    |   28 +++++++++++++++-----
 3 files changed, 40 insertions(+), 23 deletions(-)

diff --git a/vlc-android/tv/res/drawable/audio_item_selector.xml b/vlc-android/tv/res/drawable/audio_item_selector.xml
new file mode 100644
index 0000000..49c92b7
--- /dev/null
+++ b/vlc-android/tv/res/drawable/audio_item_selector.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<selector xmlns:android=
+    "http://schemas.android.com/apk/res/android">
+    <item android:state_activated="true"
+        android:drawable="@color/primary_dark_material_light" />
+    <item android:drawable="@color/primary_material_dark" />
+</selector>
\ No newline at end of file
diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
index 98f0ae8..4c422d6 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
@@ -26,17 +26,14 @@ import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.R;
 import org.videolan.vlc.audio.AudioServiceController;
 import org.videolan.vlc.gui.audio.AudioUtil;
-import org.videolan.vlc.gui.tv.audioplayer.PlaylistAdapter.ViewHolder;
 import org.videolan.vlc.interfaces.IAudioPlayer;
 import org.videolan.vlc.util.AndroidDevices;
 
-import android.annotation.TargetApi;
 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
-import android.support.v7.widget.RecyclerView.Adapter;
 import android.view.InputDevice;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
@@ -46,11 +43,11 @@ import android.widget.ProgressBar;
 import android.widget.TextView;
 
 public class AudioPlayerActivity extends Activity implements AudioServiceController.AudioServiceConnectionListener, IAudioPlayer{
-	public static final String TAG = "AudioPlayerActivity";
+	public static final String TAG = "VLC/AudioPlayerActivity";
 
     private AudioServiceController mAudioController;
     private RecyclerView mRecyclerView;
-    private Adapter<ViewHolder> mAdapter;
+    private PlaylistAdapter mAdapter;
     private LinearLayoutManager mLayoutManager;
     private ArrayList<String> mLocations;
 
@@ -116,9 +113,9 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
 	@Override
 	public void onConnectionSuccess() {
 		ArrayList<String> medialocations = (ArrayList<String>) mAudioController.getMediaLocations();
-		if (!mLocations.isEmpty() && !mLocations.equals(medialocations))
-			mAudioController.load(mLocations, 0, true);
-		else {
+		if (!mLocations.isEmpty() && !mLocations.equals(medialocations)) {
+            mAudioController.load(mLocations, 0, true);
+        } else {
 			mLocations = medialocations;
 			update();
 			mAdapter = new PlaylistAdapter(mLocations);
@@ -144,6 +141,7 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
 				mCover.setImageResource(R.drawable.background_cone);
 			else
 				mCover.setImageBitmap(cover);
+            selectItem(mLocations.indexOf(mAudioController.getCurrentMediaLocation()));
 		}
 	}
 
@@ -192,7 +190,6 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
 		}
 	}
 
-    @TargetApi(12) //only active for Android 3.1+
     public boolean dispatchGenericMotionEvent(MotionEvent event){
 
 		InputDevice mInputDevice = event.getDevice();
@@ -278,19 +275,16 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
 		selectItem(--mSelectedItem);
 	}
 
-	private void selectItem(int position){
-		mSelectedItem = position;
+	private void selectItem(final int position){
+        if (position >= mLayoutManager.getChildCount())
+            return;
+        mSelectedItem = position;
 		mRecyclerView.stopScroll();
 		mLayoutManager.scrollToPosition(position);
 		mRecyclerView.post(new Runnable() {
 			@Override
 			public void run() {
-				View v;
-				for (int i = 0 ; i< mAdapter.getItemCount() ; ++i){
-					v = mLayoutManager.findViewByPosition(i);
-					if (v != null)
-						v.setSelected( i == mSelectedItem);
-				}
+                mAdapter.setSelection(position);
 			}
 		});
 	}
diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/PlaylistAdapter.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/PlaylistAdapter.java
index e887364..8dfa9b9 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/PlaylistAdapter.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/PlaylistAdapter.java
@@ -19,20 +19,23 @@
  *****************************************************************************/
 package org.videolan.vlc.gui.tv.audioplayer;
 
-import java.util.ArrayList;
-
-import org.videolan.libvlc.Media;
-import org.videolan.vlc.MediaLibrary;
-
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 
+import org.videolan.libvlc.Media;
+import org.videolan.vlc.MediaLibrary;
+
+import java.util.ArrayList;
+
 public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHolder> {
+	public static final String TAG = "VLC/PlaylistAdapter";
+
     private ArrayList<String> mDataset;
 	private static MediaLibrary sMediaLibrary = MediaLibrary.getInstance();
+    private int mSelectedItem = -1;
 
     public static class ViewHolder extends RecyclerView.ViewHolder {
         public TextView mTitleTv;
@@ -54,8 +57,11 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
     public PlaylistAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                    int viewType) {
         View v = LayoutInflater.from(parent.getContext())
-                               .inflate(android.R.layout.simple_list_item_2, parent, false);
+                               .inflate(android.R.layout.simple_list_item_activated_2, parent, false);
 
+        v.setClickable(true);
+        v.setFocusable(true);
+        v.setFocusableInTouchMode(true);
         ViewHolder vh = new ViewHolder(v);
         return vh;
     }
@@ -65,10 +71,20 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
         Media media = sMediaLibrary.getMediaItem(mDataset.get(position));
         holder.mTitleTv.setText(media.getTitle());
         holder.mArtistTv.setText(media.getArtist());
+        holder.itemView.setActivated(position == mSelectedItem);
     }
 
     @Override
     public int getItemCount() {
         return mDataset.size();
     }
+
+    public void setSelection(int pos){
+        if (pos == mSelectedItem)
+            return;
+        int previous = mSelectedItem;
+        mSelectedItem = pos;
+        notifyItemChanged(previous);
+        notifyItemChanged(mSelectedItem);
+    }
 }
\ No newline at end of file



More information about the Android mailing list