卡视图未显示,我的回收站视图正在运行,但在回收站视图中看不到卡视图

问题描述 投票:0回答:3

我正在使用android studio来构建应用程序。由于我是android studio开发的新手,因此无法解决问题。我的应用程序运行正常。但卡片视图在recyclerview中不可见。请帮我。这是我的回收站适配器查看代码

public class RecyclerViewAdapter extends  RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private Context mContext;
private List<Upload> uploads;

public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
    this.mContext = mContext;
    this.uploads = uploads;
}

public RecyclerViewAdapter(Context mContext) {
    this.mContext = mContext;
}

public void setList(List<Upload> uploads) {
    this.uploads = uploads;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view;
    LayoutInflater inflater = LayoutInflater.from(mContext);
    view = inflater.inflate(R.layout.card_view_item, parent, false);

    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    final Upload upload = uploads.get(position);
    holder.tv_book_title.setText(upload.getName());

    Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
}

@Override
public int getItemCount() {
    return uploads == null ? 0 : uploads.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView tv_book_title;
    ImageView imd_book_thumbnail;
    CardView cardView;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        tv_book_title = itemView.findViewById(R.id.book_title_id);
        imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
        cardView = itemView.findViewById(R.id.card_view_id);
    }
}

}

这是我的名片视图XML文件代码

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:id="@+id/card_view_id"


android:layout_width="120dp"
android:layout_height="190dp"
android:foreground="?android:attr/selectableItemBackground"
android:layout_margin="5dp"
app:cardCornerRadius="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:id="@+id/book_img_id"
        android:scaleType="centerCrop"
        android:padding="2dp"
        android:background="#2d2">

    </ImageView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#2d2d2d"
        android:id="@+id/book_title_id"
        android:text="@string/title"
        android:textSize="13sp"
        android:layout_gravity="center" >

    </TextView>


</LinearLayout>

android firebase-realtime-database android-recyclerview recycler-adapter android-cardview
3个回答
0
投票

您必须用另一个视图包装cardView。我建议您像这样使用ConstraintLayout:

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

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
    android:id="@+id/card_view_id"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_marginStart="4dp"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="4dp"
    android:layout_marginBottom="4dp"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="4dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/book_img_id"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:background="#2d2"
            android:padding="2dp"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/book_title_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:text="Title"
            android:textColor="#2d2d2d"
            android:textSize="13sp" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

0
投票

尝试检查布局并检查cardView在哪里或它的大小。

在这里检查:https://developer.android.com/studio/debug/layout-inspector


0
投票

在我看来,设置数据的方法中

public void setList(List<Upload> uploads) {
    this.uploads = uploads;
}

您没有调用notifyDataSetChanged(),因此不会再次调用方法getItemCount,并且您将有空的RV,因此没有显示任何项目。

© www.soinside.com 2019 - 2024. All rights reserved.