Commit dadede97 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Fix item selection for TV AudioPlayer

parent 60cb48cd
<?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
......@@ -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);
}
});
}
......
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment