这是自定义ListView
的实现方式。此Listview
有ImageView
和TextView
。
public class TracksActivityFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
{
// Songs list
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
ListView lv;
public SongsManager manager = new SongsManager();
SongsAdapter mAdapter;
ListView lc;
private static final String ARG_POSITION = "position";
private int position;
public static TracksActivityFragment newInstance(int position)
{
TracksActivityFragment f = new TracksActivityFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_tracks_activity, container, false);
return v;
}@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
mAdapter = new SongsAdapter(getActivity(), null);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
static final String[] SONGS_SUMMARY_PROJECTION = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ALBUM_ID,MediaStore.Audio.Media.TITLE};
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String select = null;
return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,SONGS_SUMMARY_PROJECTION, select, null, MediaStore.Audio.Media.TITLE + " ASC");
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
@Override
public void onResume() {
//onResume happens after onStart and onActivityCreate
lv = getListView();
setListAdapter(mAdapter);
registerForContextMenu(lv);
getLoaderManager().initLoader(0, null, this);
//final String[] F= new String[size];
ArrayList<String> F = new ArrayList<String>();
Cursor c= getActivity().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,SONGS_SUMMARY_PROJECTION, null, null, MediaStore.Audio.Media.TITLE + " ASC");
//String[] Song = new String[size];
int i=0;
if (c.moveToFirst()) {
do {
F.add(c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));//MediaStore.Audio.Media.TITLE;
//Toast.makeText(getApplicationContext(),F[i] +" ", Toast.LENGTH_LONG).show();
i++;
} while (c.moveToNext());
}
final int size = F.size();//mAdapter.getCount();
final int Q[] = new int[size];
for(int j=0;j<size;j++){
Q[j] = manager.getSongIndex(F.get(j),getContext());
}
// listening to single listitem click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Another way around to play a song
// getting listitem index
TextView textView = (TextView) view.findViewById(R.id.title);
// Starting new intent
Intent i = new Intent(getContext(), MusicPlayerActivity.class);
Log.d("TAG", "onItemClick");
//// Sending songIndex to PlayerActivity
i.putExtra("size",size);
i.putExtra("queue",Q);
i.putExtra("filtered",true);
// Toast.makeText(getApplicationContext(), "Qi:" + Q[0], Toast.LENGTH_LONG).show();
i.putExtra("start", position);
startActivityForResult(i, 7);
}
//Another way around
});
lc= lv;
super.onResume() ;
}
}
这是使用的适配器:
public class SongsAdapter extends CursorAdapter {
private final LayoutInflater nInflater;
public SongsAdapter(Context context, Cursor c) {
super(context, c);
nInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// cursor.moveToFirst();
TextView songTitle = (TextView) view.findViewById(R.id.title);
songTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
//TextView artist = (TextView) view.findViewById(R.id.songArtist);
//artist.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
ImageView albumArt = (ImageView) view.findViewById(R.id.icon);
albumArt.setScaleType(ImageView.ScaleType.FIT_XY);
Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Bitmap img = getAlbumart(context,albumId);
if(img != null)
albumArt.setImageBitmap(img);
else{
Bitmap def = getDefaultAlbumArt(context);
albumArt.setImageBitmap(def);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = nInflater.inflate(R.layout.custom_list, parent, false);
return view;
}
public Bitmap getAlbumart(Context context, Long album_id)
{
Bitmap bm = null;
try
{
final Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null)
{
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
public Bitmap getDefaultAlbumArt(Context context) {
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
try {
bm = BitmapFactory.decodeResource(context.getResources(),
R.drawable.default_art, options);
} catch (Error ee) {
} catch (Exception e) {
}
return bm;
}
}
它本身“有效”,但它滞后。我认为这是由于背景图像的缩放,然后我在删除背景图像后运行它但它仍然滞后。
你能建议我做什么吗?