如何优化使用ListView
填充的Android
中的CursorAdapter
?我正在尝试创建一个MediaPlayer
,列表会显示歌曲标题和专辑封面,但列表中的滚动速度非常慢。
如何优化其中的List
和recycle view
?
好吧我在这里添加我的代码.....所以我让这个类扩展CursorAdapter
然后我在类中创建了它的对象我希望它也填充它...然后我设置了一个类型对于每个类,并根据该类型填充特定版本。
public class PopulatingListAdapter extends CursorAdapter {
private final static int ALL_SONGS_TYPE = 0;
private final static int ALBUM_SONGS = 1;
private final static int ARTIST_SONGS = 2;
private final static int ALBUM_TYPE = 3;
private final static int ARTIST_TYPE = 4;
private final LayoutInflater myInflater;
private int typeOfList;
public void setType(int type){
typeOfList=type;
}
View myView=null;
public PopulatingListAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
// TODO Auto-generated constructor stub
myInflater = LayoutInflater.from(context);
}
public void initializingVariables(Activity activity){
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
if(typeOfList==ALL_SONGS_TYPE)
{
TextView songTitleNameAllSongs = (TextView) view.findViewById(R.id.all_song_title);
songTitleNameAllSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
TextView songDisplayNameAllSongs = (TextView) view.findViewById(R.id.all_song_display);
songDisplayNameAllSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
ImageView albumArtInAllSongs = (ImageView) view.findViewById(R.id.album_art_all_songs_single_row);
Cursor myCursor;
String[] projection = {MediaStore.Audio.Albums._ID,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ALBUM_ART};
myCursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection, null, null, null);
int row=0;
if(myCursor.moveToFirst())
{
while(row<myCursor.getCount())
{
if(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)).equals(myCursor.getString(myCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM))))
{
Bitmap bmp = BitmapFactory.decodeFile(myCursor.getString(myCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)));
if(bmp!=null)
albumArtInAllSongs.setImageBitmap(bmp);
else
albumArtInAllSongs.setImageDrawable(context.getResources().getDrawable(R.drawable.head_10));
}
myCursor.moveToPosition(++row);
}
}
}else if(typeOfList==ALBUM_TYPE)
{
TextView albumCoverTitleInAllAlbums = (TextView) view.findViewById(R.id.album_cover_title);
albumCoverTitleInAllAlbums.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
TextView artistAlbumTitleInAllAlbums = (TextView) view.findViewById(R.id.artist_album_title);
artistAlbumTitleInAllAlbums.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)));
ImageView albumArtInAllAlbum = (ImageView) view.findViewById(R.id.album_single_row_album_art);
Bitmap bmp = BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)));
if(bmp!=null)
albumArtInAllAlbum.setImageBitmap(bmp);
else
albumArtInAllAlbum.setImageDrawable(context.getResources().getDrawable(R.drawable.head_10));
}else if(typeOfList==ARTIST_TYPE)
{
TextView artistNameInAllArtist = (TextView) view.findViewById(R.id.artists_single_name);
artistNameInAllArtist.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST)));
}else if(typeOfList==ALBUM_SONGS)
{
TextView albumSongTitleNameInAlbumSongs = (TextView) view.findViewById(R.id.albums_songs_name_of_song);
albumSongTitleNameInAlbumSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
TextView albumSongDisplayNameInAlbumSongs = (TextView) view.findViewById(R.id.albums_songs_name_of_album);
albumSongDisplayNameInAlbumSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
}else if(typeOfList==ARTIST_SONGS){
TextView artistSongTitleNameInArtistSongs = (TextView) view.findViewById(R.id.artist_song_title_name);
artistSongTitleNameInArtistSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
TextView artistSongDisplayNameInArtistSongs = (TextView) view.findViewById(R.id.artist_song_artist_name);
artistSongDisplayNameInArtistSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
if(typeOfList==ALL_SONGS_TYPE)
{
myView = myInflater.inflate(R.layout.all_songs_single_row, parent, false);
}else if(typeOfList==ALBUM_TYPE)
{
myView = myInflater.inflate(R.layout.album_single_row, parent, false);
}else if(typeOfList==ARTIST_TYPE)
{
myView = myInflater.inflate(R.layout.artists_single_row, parent, false);
}else if(typeOfList==ALBUM_SONGS)
{
myView = myInflater.inflate(R.layout.albums_songs_single_row, parent, false);
}else if(typeOfList==ARTIST_SONGS)
{
myView = myInflater.inflate(R.layout.artists_song_single_row, parent, false);
}
return myView;
}
答案 0 :(得分:0)
看起来你在主(UI)线程上进行了大量的图像和音频文件下载。为了提高性能,应该在后台线程上下载大文件,这样它们不会减慢用户界面的速度。当后台线程完成其任务时,您需要一种机制让它们通知您的活动并传递加载的数据。有几种方法可以实现这一目标,但完整的描述对于这个论坛来说太长了。
以下是一些有助于后台加载的研究课程:
Loader
AsyncTask
请查看这些教程以获取更多信息:
Processing Bitmaps Off the UI Thread
Loading Data in the Background