我有一个 ListFragment,它使用 cursorLoader 和自定义适配器来填充列表中的视图。该列表包含特定乐队的专辑,理想情况下,列表顶部(标题)将显示乐队名称和“所有歌曲”或类似内容。
问题是,当我使用 addHeaderView() 将标题添加到列表时,适配器将标题视为列表中的另一个项目,并尝试使用理想情况下仅填充标题下方行的信息来填充它。
我尝试在适配器的 bindView 中使用以下内容:
if (cursor.getPosition() == 0)) {
return; }
else {bind the rest of the views}
但它似乎表现得很奇怪 - 有时不绑定前两行中的任何一个..
下面是一些代码片段:
在 listFragment 内:
albumsAdapter = new AlbumAdapter(getActivity(), null, 0);
setListAdapter(null);
if (showHeader) {
ViewGroup parent = (ViewGroup) getActivity().findViewById(
R.id.relativeLayoutTest);
View view = (View) getActivity().getLayoutInflater().inflate(
R.layout.albumitem, parent, true);
TextView textView = (TextView) getActivity().findViewById(
R.id.artistTitle);
textView.setText("Artist");
getListView().addHeaderView(view);
}
setListAdapter(albumsAdapter);
和自定义适配器:
package another.music.player;
/* imports etc
*/
class AlbumAdapter extends CursorAdapter {
public AlbumAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
private static Uri currentSongUri;
@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView albumArt = (ImageView) view.getTag(R.id.albumArt);
TextView text1 = (TextView) view.getTag(R.id.artistTitle);
TextView text2 = (TextView) view.getTag(R.id.albumTitle);
albumArt.setImageBitmap(null);
text1.setText(cursor.getString(cursor
.getColumnIndex(AudioColumns.ARTIST)));
text2.setText(cursor.getString(cursor
.getColumnIndex(AudioColumns.ALBUM)));
String currentAlbumId = cursor.getString(cursor
.getColumnIndex(BaseColumns._ID));
Integer currentAlbumIdLong = Integer.parseInt(currentAlbumId);
Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
currentSongUri = ContentUris.withAppendedId(artworkUri,
currentAlbumIdLong);
ImageLoader imageLoader = new ImageLoader();
imageLoader.load(currentSongUri, albumArt, context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.albumitem, null);
view.setTag(R.id.albumArt, view.findViewById(R.id.albumArt));
view.setTag(R.id.artistTitle, view.findViewById(R.id.artistTitle));
view.setTag(R.id.albumTitle, view.findViewById(R.id.albumTitle));
return view;
}
}