在我的项目中有很多RecyclerViewin我不知道为什么那个我没有显示我的RecyclerView的相同代码。我找不到问题是什么。所以 任何人都可以帮忙 提前谢谢
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_artists, container, false);
Artist_reclyerview = v.findViewById(R.id.artist_reclyerview);
artistPojos = new ArrayList<>();
getartist();
artistAdapter = new ArtistAdapter(context, artistPojos);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 2);
Artist_reclyerview.setLayoutManager(mLayoutManager);
Artist_reclyerview.setAdapter(artistAdapter);
return v;
}
public void getartist() {
musicResolver = getActivity().getContentResolver();
musicUri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
//content Url Meida(LIke media,ALbum )
String sortOrder = MediaStore.Audio.Artists.DEFAULT_SORT_ORDER + " ASC";
musicCursor = musicResolver.query(musicUri, null, null, null, sortOrder);
if (musicCursor != null && musicCursor.moveToFirst()) {
int media_id = musicCursor.getColumnIndex
(MediaStore.Audio.Media._ID);
int artistid = musicCursor.getColumnIndex(
MediaStore.Audio.Artists._ID);
int artistname = musicCursor.getColumnIndex
(MediaStore.Audio.Artists.ARTIST);
do {
String Media_id = musicCursor.getString(media_id);
String artistId = musicCursor.getString(artistid);
String artistName = musicCursor.getString(artistname);
Log.d("mediaid", "" + Media_id);
Log.d("artistid", "" + artistId);
Log.d("artistname", "" + artistName);
} while (musicCursor.moveToNext());
}
}
即RecyclerView xml。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:background="@color/viewBg"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/artist_reclyerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical" />
</RelativeLayout>
这是RecyclerView adpater类文件
public class ArtistAdapter extends RecyclerView.Adapter<ArtistAdapter.Holder> {
ArrayList<ArtistPojo> artistPojos;
Context context;
LayoutInflater inflater;
View v;
public ArtistAdapter(Context c, ArrayList<ArtistPojo> pojos) {
context = c;
artistPojos = pojos;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflate layout
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_artist_adapter, null);
return new ArtistAdapter.Holder(v);
}
@Override
public int getItemCount() {
return artistPojos.size();
}
@Override
public void onBindViewHolder(Holder holder, int position) {
final ArtistPojo artistPojo = artistPojos.get(position);
holder.artist.setText(artistPojo.getArtist());
holder.artist_img.setImageResource(R.drawable.splash);
}
public static class Holder extends RecyclerView.ViewHolder {
TextView artist;
ImageView artist_img, dot;
CardView Artist;
public Holder(View itemView) {
super(itemView);
//find by id to adpater file
artist = itemView.findViewById(R.id.artist_txttitile);
artist_img = itemView.findViewById(R.id.artist_img);
Artist = itemView.findViewById(R.id.card_view);
dot = itemView.findViewById(R.id.overflow);
}
}
}
还有RecyclerView xml adpater文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/reclerview_adpater"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="20dp">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="@dimen/card_margin"
android:elevation="3dp"
card_view:cardCornerRadius="@dimen/card_album_radius">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/artist_img"
android:layout_width="match_parent"
android:layout_height="@dimen/album_cover_height"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="fitXY" />
<TextView
android:id="@+id/artist_txttitile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/thumbnail"
android:paddingLeft="@dimen/album_title_padding"
android:paddingRight="@dimen/album_title_padding"
android:paddingTop="@dimen/album_title_padding"
android:textColor="@color/album_title"
android:textSize="@dimen/album_title" />
<TextView
android:id="@+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:paddingBottom="@dimen/songs_count_padding_bottom"
android:paddingLeft="@dimen/album_title_padding"
android:paddingRight="@dimen/album_title_padding"
android:textSize="@dimen/songs_count" />
<ImageView
android:id="@+id/overflow"
android:layout_width="@dimen/ic_album_overflow_width"
android:layout_height="@dimen/ic_album_overflow_height"
android:layout_alignParentRight="true"
android:layout_below="@id/thumbnail"
android:layout_marginTop="@dimen/ic_album_overflow_margin_top"
android:scaleType="centerCrop"
android:src="@drawable/ic_dots" />
</RelativeLayout>
</android.support.v7.widget.CardView>
那是我的ArtistPojo类
public class ArtistPojo {
Long Media_Id;
String Artist, ArtistId, Song;
public ArtistPojo(Long media_Id, String artist, String artistId, String song) {
Media_Id = media_Id;
Artist = artist;
ArtistId = artistId;
Song = song;
}
public Long getMedia_Id() {
return Media_Id;
}
public void setMedia_Id(Long media_Id) {
Media_Id = media_Id;
}
public String getArtist() {
return Artist;
}
public void setArtist(String artist) {
Artist = artist;
}
public String getArtistId() {
return ArtistId;
}
public void setArtistId(String artistId) {
ArtistId = artistId;
}
public String getSong() {
return Song;
}
public void setSong(String song) {
Song = song;
}
}
答案 0 :(得分:1)
您没有在 artistPojos
ArrayList
中添加任何项目,请检查
在你的arraylist中添加数据
do {
String Media_id = musicCursor.getString(media_id);
String artistId = musicCursor.getString(artistid);
String artistName = musicCursor.getString(artistname);
ArtistPojo pojo= new ArtistPojo();
pojo.setData("");// as per your getter and setter method here
artistPojos.add(pojo)
Log.d("mediaid", "" + Media_id);
Log.d("artistid", "" + artistId);
Log.d("artistname", "" + artistName);
} while (musicCursor.moveToNext());
并且还要将此更改为activity_artist_adapter
hight android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/reclerview_adpater"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">