android - 在 CardStackView 库上扩展类 androidx.cardview.widget.CardView 时出错

标签 android swipecardview

我正在尝试实现 this图书馆。此处编写的示例代码是用 Kotlin 编写的,但我的项目是用 Java 编写的,因此我尝试将代码从 Kotlin 转换为 Java。但是我收到以下错误:

android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.cardview.widget.CardView Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.cardview.widget.CardView Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method)

在给cardview充气

class CardStackAdapter extends RecyclerView.Adapter<CardStackAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Spot> listItem;

    public CardStackAdapter(Context applicationContext, ArrayList<Spot> listItem) {
        this.context = applicationContext;
        this.listItem = listItem;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);  // ----> I am getting error here..
        return new ViewHolder(context, view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final String name = listItem.get(position).name;
        String url = listItem.get(position).URL;
        String city = listItem.get(position).city;
        holder.name.setText(name);
        holder.city.setText(city);
        Picasso.get().load(url).into(holder.image);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
            }
        });

    }

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

    public void setItem(ArrayList<Spot> listItem){
        this.listItem = listItem;
    }

    public ArrayList<Spot> getItem(){
        return listItem;
    }




    class ViewHolder extends RecyclerView.ViewHolder {
         TextView name;
         TextView city;
         ImageView image;

        ViewHolder(Context context, View view) {
            super(view);
            this.name =  view.findViewById(R.id.item_name);
            this.city =  view.findViewById(R.id.item_city);
            this.image = view.findViewById(R.id.item_image);
        }
    }

}

此卡片 View 的 XML 代码是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="?attr/selectableItemBackground"
    android:foreground="?attr/selectableItemBackground"
    app:cardUseCompatPadding="true"
    app:cardPreventCornerOverlap="false"
    app:cardCornerRadius="8dp"
    app:cardBackgroundColor="@android:color/white">

    <com.makeramen.roundedimageview.RoundedImageView
        android:id="@+id/item_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        app:riv_corner_radius="8dp"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:padding="16dp"
        android:background="@drawable/gradation_black">

        <TextView
            android:id="@+id/item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:textSize="26sp"/>

        <TextView
            android:id="@+id/item_city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:textSize="20sp"/>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/left_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/overlay_black">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/skip_white_120dp"
            android:layout_gravity="center"/>

    </FrameLayout>

    <FrameLayout
        android:id="@+id/right_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/overlay_black">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/like_white_120dp"
            android:layout_gravity="center"/>

    </FrameLayout>

    <FrameLayout
        android:id="@+id/top_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/bottom_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</androidx.cardview.widget.CardView>

我已将此项目上传到 Github Repository .欢迎提出任何建议。谢谢!

最佳答案

删除附图中标记在这里的两行**,因为这两行包含在旧的 appcompact 支持库中,不包含在 androidx.appcompact 中,并且 View 持有者无法膨胀,因为这 2行,我遇到了同样的问题,我尝试通过多种方式解决它,将卡片 View 更改为线性并使其工作,然后尝试从卡片 View 中删除一些行,现在它可以完美地工作了

使用这个:

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="@android:color/white">

代替

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/selectableItemBackground"
android:foreground="?attr/selectableItemBackground"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="@android:color/white">

see the image i marked lines here

关于android - 在 CardStackView 库上扩展类 androidx.cardview.widget.CardView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58250346/

相关文章:

android - VideoView 循环播放视频 补充

Android MediaPlayer setDataSource() 抛出 IllegalStateException 错误

flutter - 如何在 flutter 中创建类似 Tinder 的堆叠卡片?

android - 启动android应用时出错

android - 将 OkHTTP 3 与摘要一起使用

java - SQLite异常: No such table even if there is table