RecyclerViewAdapter内部的RecyclerView没有显示任何项目吗?

时间:2018-12-13 03:37:28

标签: android firebase android-recyclerview

我正在尝试在recyclerview内创建recyclerview,以便从Firebase检索数据。

到目前为止,我有两个recyclerview适配器。 GenreAdapter和AnimeInsideGenreAdapter类。

我能够将GenreAdapter数据显示到我的HomeFragment中,但是AnimeInsideGenreAdapter没有显示任何数据,我也不知道为什么。我一直在Google上寻找问题,但还是没走运。

这是我的课程和布局:

GenreAdapter.class 公共类GenreAdapter扩展了RecyclerView.Adapter {

private List<Genre> genreList;
private Context context;

private List<Anime> perAnimes;
private RecyclerView rvPerAnime;
private RecyclerView.Adapter adapterPerAnime;

class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder {

    FontAppCompatTextView genre_id, genre_title;
    LinearLayout main;

    public ViewHolder(final View itemView) {
        super(itemView);

        main = itemView.findViewById(R.id.main_layout);
        genre_id = itemView.findViewById(R.id.genre_id);
        genre_title = itemView.findViewById(R.id.genre_title);

    }

}

public GenreAdapter(List<Genre> genreList, Context context) {
    this.genreList = genreList;
    this.context = context;
}

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_genre, parent, false));
}

public void onBindViewHolder(ViewHolder holder, int position) {

    Genre genre = this.genreList.get(position);
    holder.genre_id.setText(genre.id);
    holder.genre_title.setText(genre.genre);

    perAnimes = new ArrayList<>();

    DatabaseReference databaseAnime = FirebaseDatabase.getInstance().getReference("anime");
    Query query_anime = databaseAnime.orderByChild("genreid").equalTo(genre.id);

    query_anime.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            perAnimes.clear();
            if (dataSnapshot.exists()) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Anime anime = snapshot.getValue(Anime.class);
                    perAnimes.add(anime);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    perAnimes.sort(new Comparator<Anime>() {
                        @Override
                        public int compare(Anime compare1, Anime compare2) {
                            return compare1.getTitle().compareToIgnoreCase(compare2.getTitle());
                        }
                    });
                }
                adapterPerAnime.notifyDataSetChanged();
            } else {
                perAnimes.clear();
                adapterPerAnime.notifyDataSetChanged();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    rvPerAnime = holder.itemView.findViewById(R.id.rvanime);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    rvPerAnime.setLayoutManager(linearLayoutManager);
    rvPerAnime.setHasFixedSize(true);

    adapterPerAnime = new AnimeInsideGenreAdapter(perAnimes, context);
    rvPerAnime.setAdapter(adapterPerAnime);

    Animation up_from_bottom = AnimationUtils.loadAnimation(context, R.anim.up_from_bottom);
    holder.itemView.startAnimation(up_from_bottom);

}

public int getItemCount() {
    return this.genreList.size();
}

}

item_genre.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.pixplicity.fontview.FontAppCompatTextView
        android:id="@+id/genre_title"
        style="@style/FontTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:singleLine="true"
        android:text="Genre Anime"
        android:textColor="#f59e24"
        android:textSize="14dp"
        android:textStyle="bold" />

    <com.pixplicity.fontview.FontAppCompatTextView
        android:id="@+id/genre_id"
        style="@style/FontTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="ID Genre"
        android:textColor="#7c7c7c"
        android:textSize="12dp"
        android:visibility="gone" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvanime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp" />

</LinearLayout>

AnimeInsideGenreAdapter.class

public class AnimeInsideGenreAdapter extends RecyclerView.Adapter<AnimeInsideGenreAdapter.ViewHolder> {

    List<Anime> perAnimes;
    private Context context;
    private Random mRandom = new Random();

    class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder {

        public FontAppCompatTextView anime_id, anime_title;
        public ImageView img_anime;
        public LinearLayout main;

        public ViewHolder(final View itemView) {
            super(itemView);

            main = itemView.findViewById(R.id.main_layout);
            anime_id = itemView.findViewById(R.id.anime_id);
            anime_title = itemView.findViewById(R.id.anime_title);
            img_anime = itemView.findViewById(R.id.img_anime);

            Animation up_from_bottom = AnimationUtils.loadAnimation(itemView.getContext(), R.anim.up_from_bottom);
            itemView.startAnimation(up_from_bottom);
        }

    }

    public AnimeInsideGenreAdapter(List<Anime> perAnimes, Context context) {
        this.perAnimes = perAnimes;
        this.context = context;
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_anime_inside_genre, parent, false));
    }

    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final Anime perAnime = this.perAnimes.get(position);

        holder.anime_id.setText(perAnime.getId());
        holder.anime_title.setText(perAnime.getTitle());

        Toast.makeText(context, perAnime.toString(), Toast.LENGTH_SHORT).show();

        Picasso.with(context).load(perAnime.getImgcover()).fit().into(holder.img_anime);
        holder.img_anime.getLayoutParams().width = getRandomIntInRange(270,200);
        holder.img_anime.getLayoutParams().height = 250;

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
        
            }
            
        });
    }

    public int getItemCount() {
        return this.perAnimes.size();
    }
    // Custom method to get a random number between a range
    protected int getRandomIntInRange(int max, int min){
        return mRandom.nextInt((max-min)+min)+min;
    }
}

item_anime_inside_genre.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <com.pixplicity.fontview.FontAppCompatTextView
            android:id="@+id/anime_id"
            style="@style/FontTextViewStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:singleLine="true"
            android:text="Anime ID"
            android:textColor="#f59e24"
            android:textSize="14dp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/img_anime"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"/>

        <com.pixplicity.fontview.FontAppCompatTextView
            android:id="@+id/anime_title"
            style="@style/FontTextViewStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Anime Title"
            android:textColor="#7c7c7c"
            android:textSize="12dp" />

    </LinearLayout>

</android.support.v7.widget.CardView>

流派类 公共类流派{

String id;
String genre;

public Genre() {

}

public Genre(String id, String genre) {
    this.id = id;
    this.genre = genre;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}}

Anime.class 公共课动漫{

String id;
String genreid;
String title;
String description;
String imgcover;

public Anime() {

}

public Anime(String id, String genreid, String title, String description, String imgcover) {
    this.id = id;
    this.genreid = genreid;
    this.title = title;
    this.description = description;
    this.imgcover = imgcover;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getGenreid() {
    return genreid;
}

public void setGenreid(String genreid) {
    this.genreid = genreid;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getImgcover() {
    return imgcover;
}

public void setImgcover(String imgcover) {
    this.imgcover = imgcover;
}}

我需要在每个流派适配器中显示Firebase的动漫列表。 任何帮助都感激不尽。 谢谢。

0 个答案:

没有答案