[Android] Toggle focus between recyclerview and controls

Geoffrey Métais git at videolan.org
Mon Feb 16 12:18:30 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Feb 13 16:23:29 2015 +0100| [543d32e1c6471dfa23a18e75392a495dcfa059fb] | committer: Geoffrey Métais

Toggle focus between recyclerview and controls

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

 .../gui/tv/audioplayer/AudioPlayerActivity.java    |   34 ++++++++++++--------
 .../vlc/gui/tv/audioplayer/PlaylistAdapter.java    |   15 ++++++---
 2 files changed, 32 insertions(+), 17 deletions(-)

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 a9086a2..cef179c 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
@@ -43,7 +43,7 @@ import android.widget.ImageView;
 import android.widget.ProgressBar;
 import android.widget.TextView;
 
-public class AudioPlayerActivity extends Activity implements AudioServiceController.AudioServiceConnectionListener, IAudioPlayer{
+public class AudioPlayerActivity extends Activity implements AudioServiceController.AudioServiceConnectionListener, IAudioPlayer, View.OnFocusChangeListener {
     public static final String TAG = "VLC/AudioPlayerActivity";
 
     private AudioServiceController mAudioController;
@@ -55,8 +55,7 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
     //PAD navigation
     private static final int JOYSTICK_INPUT_DELAY = 300;
     private long mLastMove;
-    private int mSelectedItem = 0;
-    private int mCurrentlyPlaying;
+    private int mCurrentlyPlaying, mPositionSaved = 0;
 
     private TextView mTitleTv, mArtistTv;
     private ImageView mPlayPauseButton, mCover;
@@ -85,7 +84,7 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
         mPlayPauseButton = (ImageView)findViewById(R.id.button_play);
         mProgressBar = (ProgressBar)findViewById(R.id.media_progress);
         mCover = (ImageView)findViewById(R.id.album_cover);
-
+        findViewById(R.id.button_next).setOnFocusChangeListener(this);
     }
 
     public void onStart(){
@@ -196,8 +195,8 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
     }
 
     public void playSelection() {
-        mAudioController.playIndex(mSelectedItem);
-        mCurrentlyPlaying = mSelectedItem;
+        mAudioController.playIndex(mAdapter.getmSelectedItem());
+        mCurrentlyPlaying = mAdapter.getmSelectedItem();
     }
 
     public boolean dispatchGenericMotionEvent(MotionEvent event){
@@ -260,15 +259,15 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
     }
 
     private void selectNext() {
-        if (mSelectedItem >= mAdapter.getItemCount()-1)
+        if (mAdapter.getmSelectedItem() >= mAdapter.getItemCount()-1)
             return;
-        selectItem(++mSelectedItem);
+        selectItem(mAdapter.getmSelectedItem()+1);
     }
 
     private void selectPrevious() {
-        if (mSelectedItem < 1)
+        if (mAdapter.getmSelectedItem() < 1)
             return;
-        selectItem(--mSelectedItem);
+        selectItem(mAdapter.getmSelectedItem()-1);
     }
 
     private void selectItem(final int position){
@@ -277,14 +276,23 @@ public class AudioPlayerActivity extends Activity implements AudioServiceControl
         mRecyclerView.post(new Runnable() {
             @Override
             public void run() {
-                if (position > mLayoutManager.findLastCompletelyVisibleItemPosition()
-                        || position < mLayoutManager.findFirstCompletelyVisibleItemPosition()) {
+                if (position != -1 && (position > mLayoutManager.findLastCompletelyVisibleItemPosition()
+                        || position < mLayoutManager.findFirstCompletelyVisibleItemPosition())) {
                     mRecyclerView.stopScroll();
                     mRecyclerView.smoothScrollToPosition(position);
                 }
                 mAdapter.setSelection(position);
             }
         });
-        mSelectedItem = position;
+    }
+
+    @Override
+    public void onFocusChange(View v, boolean hasFocus) {
+        if (hasFocus) {
+            if (mAdapter.getmSelectedItem() != -1)
+                mPositionSaved = mAdapter.getmSelectedItem();
+            selectItem(-1);
+        } else if (!mPlayPauseButton.hasFocus())
+            selectItem(mPositionSaved);
     }
 }
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 b2670bd..627552c 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
@@ -32,7 +32,7 @@ import org.videolan.vlc.util.Util;
 
 import java.util.ArrayList;
 
-public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHolder> implements View.OnClickListener {
+public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHolder> implements View.OnClickListener{
     public static final String TAG = "VLC/PlaylistAdapter";
 
     private AudioPlayerActivity mAudioPlayerActivity;
@@ -86,13 +86,20 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
         return mDataset.size();
     }
 
-    public void setSelection(int pos){
+    public int getmSelectedItem(){
+        return mSelectedItem;
+    }
+
+    public void setSelection(int pos) {
         if (pos == mSelectedItem)
             return;
         int previous = mSelectedItem;
         mSelectedItem = pos;
-        notifyItemChanged(previous);
-        notifyItemChanged(mSelectedItem);
+        if (previous != -1)
+            notifyItemChanged(previous);
+        if (pos != -1){
+            notifyItemChanged(mSelectedItem);
+        }
     }
 
     @Override



More information about the Android mailing list