2015-08-08 78 views
-2

我希望以相冊藝術,專輯名稱和歌曲數量的網格顯示專輯。如何在片段內顯示GridView?

這是我的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 公共類代碼相冊擴展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」 這些代碼。

+0

你怎麼綁定兩個?你的適配器在哪裏? – iTurki

+0

請告訴我如何製作一個。適配器真的讓我困惑。到目前爲止,我只實現了陣列適配器。 –

+0

請貼一些代碼 – Prasad

回答

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

原因:

在片段,你必須綁定子視圖片段視圖。

完成

+0

非常感謝。有效。你能告訴我,如果我想創建一個onClickListener方法,我應該在哪裏放置它,以及如何在點擊網格項目時顯示專輯中的所有歌曲? –

+0

@AritraBhattacharyya,你必須爲Gridview的項目選擇採用ItemClickListener,你可以在onCreateView()或onActivityCreated()上做。 onCreateView&onActivityCreated都是片段生命週期的一部分。 –

+0

再次感謝。但是,你能告訴我該怎麼做才能提取特定專輯的所有歌曲? –