android - RecyclerView 未显示(正在显示)

标签 android android-layout android-fragments android-recyclerview

我的项目中有很多 RecyclerView,我不明白为什么这与我私下使用的代码相同,但没有显示我的 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 适配器文件。

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

最佳答案

您没有在 artistPojos 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 高度 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">

关于android - RecyclerView 未显示(正在显示),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48031498/

相关文章:

Android水平 ScrollView

android - 如何在 React Native Android 中设置按钮的高度

android - 导航架构组件 singleTask 或 singleInstance 在返回堆栈中

android - 以编程方式设置 View 宽度

java - Android:添加一个简单的 fragment

android - fragment 之间的过渡动​​画

android - Fragment$Companion 无法在 BottomNavigationView 监听器上转换为 androidx.fragment.app.Fragment

android - 如何检测数据类 Kotlin 中更改的值?

android - 需要字符串解码帮助

java - 在另一个 View 周围的圆圈中动态添加 View