如何在Fragment中显示GridView?

时间:2015-08-08 13:40:22

标签: android android-layout android-gridview android-cardview

我希望在相册艺术,专辑名称和歌曲数量的网格中显示专辑。

这些是我的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/content_frame"
android:layout_height="match_parent"
android:background="@color/metalList">


<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid"
    android:layout_width="fill_parent"
    android:numColumns="auto_fit"
    android:layout_height="wrap_content"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:layout_gravity="right|top"/>


</FrameLayout>

和个别项目:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
android:layout_width="160dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="@color/metalList"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:elevation="4dp"
xmlns:android="http://schemas.android.com/apk/res/android">
            <ImageView
                android:id="@+id/albumart"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:layout_gravity="top"
                android:layout_margin="0dp"
                android:src="@drawable/musicicon"
                />
            <TextView
                android:id="@+id/album_name"
                android:layout_height="25dp"
                android:layout_width="match_parent"
                android:textColor="@color/metalTextB"
                android:layout_below="@id/albumart"
                android:layout_marginLeft="5dp"
                android:textSize="16sp"
                android:typeface="sans"
                android:text="Album"
                android:scrollHorizontally="true"
                android:ellipsize="end"
                android:maxLines="1"/>
            <TextView
                android:id="@+id/songs_num"
                android:layout_width="match_parent"
                android:layout_height="15dp"
                android:layout_below="@id/album_name"
                android:layout_marginLeft="5dp"
                android:textColor="@color/metalTextS"
                android:textSize="10sp"
                android:text="artist"
                android:typeface="sans"
                android:scrollHorizontally="true"
                android:ellipsize="end"
                android:maxLines="1"/>
</LinearLayout>

以下是片段和活动的代码

Albums.java     public class Albums extends Fragment实现了LoaderManager.LoaderCallbacks {

AlbumsAdapter mAdapter;

private static final String ARG_POSITION = "position";

private int position;

public static Albums newInstance(int position) {
    Albums f = new Albums();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.albums, container, false);
    GridView grid = (GridView) this.getActivity().findViewById(R.id.grid);
    mAdapter = new AlbumsAdapter(getActivity(), null);
    grid.setAdapter(mAdapter);
    return myFragmentView;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    getLoaderManager().initLoader(0, null, this);
}


static final String[] ALBUM_SUMMARY_PROJECTION = { MediaStore.Audio.Albums._ID,MediaStore.Audio.Albums.ALBUM_ART, MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ARTIST};



public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String select = null;
    return new CursorLoader(getActivity(), MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,ALBUM_SUMMARY_PROJECTION, select, null, null);
}



public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
}



public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}
}

public class AlbumsAdapter extends CursorAdapter {

private final LayoutInflater mInflater;

public AlbumsAdapter(Context context, Cursor c) {
    super(context, c);
    mInflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ImageView albumArt = (ImageView) view.findViewById(R.id.albumart);
    albumArt.setScaleType(ImageView.ScaleType.FIT_XY);
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    Bitmap img = BitmapFactory.decodeFile(path);
    if(img !=null)
        albumArt.setImageBitmap(img);
    else{
        Bitmap bit = getDefaultAlbumArt(context);
        albumArt.setImageBitmap(bit);
    }

    TextView albumTitle = (TextView) view.findViewById(R.id.album_name);
    albumTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));

    TextView artistName = (TextView) view.findViewById(R.id.songs_num);
    artistName.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)));

}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = mInflater.inflate(R.layout.grid_item, parent, false);
    return view;
}
public Bitmap getDefaultAlbumArt(Context context) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    try {
        bm = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.musicicon, options);
    } catch (Error ee) {
    } catch (Exception e) {
    }
    return bm;
}

}

AlbumsAdapter.java     公共类AlbumsAdapter扩展了CursorAdapter {

private final LayoutInflater mInflater;

public AlbumsAdapter(Context context, Cursor c) {
    super(context, c);
    mInflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ImageView albumArt = (ImageView) view.findViewById(R.id.albumart);
    albumArt.setScaleType(ImageView.ScaleType.FIT_XY);
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    Bitmap img = BitmapFactory.decodeFile(path);
    if(img !=null)
        albumArt.setImageBitmap(img);
    else{
        Bitmap bit = getDefaultAlbumArt(context);
        albumArt.setImageBitmap(bit);
    }

    TextView albumTitle = (TextView) view.findViewById(R.id.album_name);
    albumTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));

    TextView artistName = (TextView) view.findViewById(R.id.songs_num);
    artistName.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)));

}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = mInflater.inflate(R.layout.grid_item, parent, false);
    return view;
}
public Bitmap getDefaultAlbumArt(Context context) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    try {
        bm = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.musicicon, options);
    } catch (Error ee) {
    } catch (Exception e) {
    }
    return bm;
}

}

我收到带有这些代码的“java.lang.NullPointerException”。

1 个答案:

答案 0 :(得分:2)

请你改变下面的内容吗?

View myFragmentView = inflater.inflate(R.layout.albums, container, false);
GridView grid = (GridView) this.getActivity().findViewById(R.id.grid);

View myFragmentView = inflater.inflate(R.layout.albums, container, false);
GridView grid = (GridView) myFragmentView.findViewById(R.id.grid);

<强>原因:

在片段中,您必须将子视图绑定到片段视图。

完成