如何优化ListView
的Android
正在填充使用CursorAdapter
?我正尝试创建一个MediaPlayer
,该列表随歌曲标题和专辑封面出现,但列表中的滚动非常缓慢。 如何优化List
和recycle view
呢?在Android中使用CursorAdapter优化ListView?
好的,我在这里添加我的代码.....所以我让这个类扩展了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;
}
你使用ViewBinder ? – pskink 2015-02-09 19:25:05
你是指cursorAdapter中的bindView方法? – 2015-02-09 19:43:01
我们应该看看你的代码,以了解如何对你有所帮助 – 2015-02-09 19:44:37