improve performance of ShowQueueAdapter
Using a copy of the timeline and re-creating all entries on every view update scales very poorly (what was i thinking?!) The new code holds a reference to the playback service where it can query the queue on demand
This commit is contained in:
parent
1e1fe4a375
commit
331fbe94d9
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013-2014 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
* Copyright (C) 2013-2016 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -21,7 +21,7 @@ import android.content.Context;
|
||||
import android.app.Activity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.view.LayoutInflater;
|
||||
import android.widget.TextView;
|
||||
|
||||
@ -31,30 +31,86 @@ import android.text.style.ForegroundColorSpan;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableStringBuilder;
|
||||
|
||||
public class ShowQueueAdapter
|
||||
extends ArrayAdapter<Song>
|
||||
{
|
||||
|
||||
public class ShowQueueAdapter extends BaseAdapter {
|
||||
/**
|
||||
* The resource to pass to the inflater
|
||||
*/
|
||||
private int mResource;
|
||||
/**
|
||||
* The position we are going to mark as 'active'
|
||||
*/
|
||||
private int mHighlightRow;
|
||||
/**
|
||||
* The context to use
|
||||
*/
|
||||
private Context mContext;
|
||||
/**
|
||||
* The playback service reference to query
|
||||
*/
|
||||
private PlaybackService mService;
|
||||
|
||||
public ShowQueueAdapter(Context context, int resource) {
|
||||
super(context, resource);
|
||||
super();
|
||||
mResource = resource;
|
||||
mContext = context;
|
||||
mHighlightRow = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells the adapter to highlight a specific row id
|
||||
* Set this to -1 to disable the feature
|
||||
* Configures our data source
|
||||
*
|
||||
* @param service the playback service instance to use
|
||||
* @param pos the row to highlight, setting this to -1 disables the feature
|
||||
*/
|
||||
public void highlightRow(int pos) {
|
||||
public void setData(PlaybackService service, int pos) {
|
||||
mService = service;
|
||||
mHighlightRow = pos;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of songs in this adapter
|
||||
*
|
||||
* @return the number of songs in this adapter
|
||||
*/
|
||||
@Override
|
||||
public int getCount() {
|
||||
return (mService == null ? 0 : mService.getTimelineLength());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the song at given position
|
||||
*
|
||||
* @param pos the position to query
|
||||
* @return a Song object
|
||||
*/
|
||||
@Override
|
||||
public Song getItem(int pos) {
|
||||
return mService.getSongByQueuePosition(pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the item id at `pos'
|
||||
*
|
||||
* @param pos the position to query
|
||||
* @return the song.id at this position
|
||||
*/
|
||||
@Override
|
||||
public long getItemId(int pos) {
|
||||
return getItem(pos).id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns always `true' as song.id's are stable
|
||||
*/
|
||||
@Override
|
||||
public boolean hasStableIds() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the view
|
||||
*/
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
DraggableRow row;
|
||||
|
@ -182,19 +182,11 @@ public class ShowQueueFragment extends Fragment
|
||||
public void refreshSongQueueList(final boolean scroll) {
|
||||
getActivity().runOnUiThread(new Runnable(){
|
||||
public void run() {
|
||||
int i, stotal, spos;
|
||||
stotal = mService.getTimelineLength(); /* Total number of songs in queue */
|
||||
spos = mService.getTimelinePosition(); /* Current position in queue */
|
||||
|
||||
mListAdapter.clear(); /* Flush all existing entries... */
|
||||
mListAdapter.highlightRow(spos); /* and highlight current position */
|
||||
|
||||
for(i=0 ; i<stotal; i++) {
|
||||
mListAdapter.add(mService.getSongByQueuePosition(i));
|
||||
}
|
||||
int pos = mService.getTimelinePosition();
|
||||
mListAdapter.setData(mService, pos);
|
||||
|
||||
if(scroll)
|
||||
scrollToCurrentSong(spos);
|
||||
scrollToCurrentSong(pos);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user