0

これは、cursorLoader とカスタム アダプターを使用してリスト内のビューを設定します。リストには特定のバンドのアルバムが含まれており、リストの上部 (ヘッダー) にバンド名と「すべての曲」などを表示するのが理想的です。

問題は、 addHeaderView() を使用してヘッダーをリストに追加すると、アダプターがヘッダーをリスト内の別の項目として扱い、理想的にはヘッダーの下の行のみを埋める情報で埋めようとすることです。

アダプターのbindView内で次を使用してみました:

if (cursor.getPosition() == 0)) {
return; }
else {bind the rest of the views}

しかし、それは奇妙な動作をしているようです - 最初の2行のいずれかをバインドしないことがあります..

以下は、いくつかのコード スニペットです。

リストフラグメント内:

    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;
}

}
4

2 に答える 2

1

ご覧のとおり、headerView はアダプターと同じレイアウト ファイルを使用しています。その名前と内部のビュー ID を変更します。これで問題は解決します。

于 2012-08-21T14:17:35.750 に答える