Android中的卡片视图问题,我的recyclerview正常工作,但在recyclerview中看不到卡片视图

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

我正在使用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
1个回答
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="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:id="@+id/card_view_id"
        android:layout_width="120dp"


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

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

</androidx.constraintlayout.widget.ConstraintLayout>
© www.soinside.com 2019 - 2024. All rights reserved.