首页 > 解决方案 > 如何在 DrawerLayout 内构建滑动 CardView?

问题描述

我正在努力创造CardView可水平滑动的东西。

几天来,我一直在互联网上搜索并在 YouTube 上观看教程。在尝试了这些可能的解决方案和建议后,我的应用程序要么出现我无法解决的错误,要么崩溃。

所以,这是我的应用程序的屏幕截图。两个白色空格是CardView我正在处理的。

在此处输入图像描述

我的目标类似于 Google Play 商店中的滑动卡片:

在此处输入图像描述

这是我的一部分activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<TextView
            android:id="@+id/jdCampusText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:text="JD Campus"
            android:textSize="26sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="@+id/jdCampusCard"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.332" />

        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/jdCampusCard"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:layout_marginStart="8dp"
            android:layout_marginTop="0dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            card_view:cardCornerRadius="4dp"
            card_view:layout_constraintBottom_toBottomOf="parent"
            card_view:layout_constraintEnd_toEndOf="parent"
            card_view:layout_constraintStart_toStartOf="parent"
            card_view:layout_constraintTop_toTopOf="parent">
        </android.support.v7.widget.CardView>

        <TextView
            android:id="@+id/bsCampusText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:text="BS Campus"
            android:textSize="26sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/bsCampusCard"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.657" />

        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/bsCampusCard"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            card_view:cardCornerRadius="4dp"
            card_view:layout_constraintBottom_toBottomOf="parent"
            card_view:layout_constraintEnd_toEndOf="parent"
            card_view:layout_constraintHorizontal_bias="0.0"
            card_view:layout_constraintStart_toStartOf="parent"
            card_view:layout_constraintTop_toTopOf="parent"
            card_view:layout_constraintVertical_bias="0.884">
        </android.support.v7.widget.CardView>

    </android.support.constraint.ConstraintLayout>
</android.support.v4.widget.DrawerLayout>

它不需要是CardView,任何可滑动的,可以onClick是对我来说都很好。

标签: androidandroid-cardviewswipecardview

解决方案


推荐阅读