如何使用 RecyclerView 和 CardView

How to use RecyclerView and CardView

我在 android 开发方面没有太多经验,因此尝试在我的应用程序中实现 RecyclerView。 android studio 的版本没有 Android L 也没有安装选项。每次它说 android.support.widget.v7.RecyclerView 未使用并从导入包中禁用它。我也在 Gradle.build 中对布局文件进行了引用,但我的问题仍然存在,有人帮忙吗?

跟随这条线

  • Material 设计中的 CardView 和 RecyclerView

http://icetea09.com/blog/2014/12/19/android-cardview-and-recyclerview-in-material-design/

将这些添加到 依赖项:

    compile 'com.android.support:cardview-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'

并更新模块 build.gradle

中的 gradle
  • 每个示例:

CardView

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:contentPadding="16dp"
    card_view:cardElevation="2dp"
    card_view:cardCornerRadius="5dp">

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

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Headline"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Title" />

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Body1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Content here" />

    </LinearLayout>

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

回收视图:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

根据 documentation,您必须向 gradle 文件添加特殊依赖项:

dependencies {
    ...
    compile 'com.android.support:appcompat-v7:27.0.1'
    compile 'com.android.support:cardview-v7:27.0.1'
    compile 'com.android.support:recyclerview-v7:27.0.1'

}

要使用它,请始终以 android.support.v7.widget.

作为前缀

RecyclerView 使用适配器,将列表项传递给它。适配器 returns 您选择传递给 RecyclerView 的列表项类型的对象。在此 post 中,您将学习如何在您的应用中实现 RecyclerView,以及如何传递 CardView 对象以在其中滚动。

步骤: 1) 构建一个 CardView 2) 建立一个数据模型来填充你的 CardView 3) 构建一个 RecyclerView 4) 构建一个将数据连接到 RecyclerView 的适配器 5) 将适配器设置为您的 RecyclerView

本教程将帮助您按照以下步骤在 Android 应用中使用 RecyclerView 和 CardView:

https://knowledgecollisions.wordpress.com/2016/03/29/using-recyclerview-and-cardview-in-your-android-app/

您可以在没有 CardView 的情况下使用 Recyclerview。但是 CardView 提供了更多功能来设计列表行。

如何在 ANDROID

中使用 RECYCLER VIEW 的说明

以下是解释如何使用 Recycler View 的步骤

  • 转到应用程序 gradle 文件

  • 添加Recycler View的依赖 compile 'com.android.support:recyclerview-v7:25.3.1'(在创建项目时使用最新的一个依赖项),并同步项目

  • 在您的 activity/ 片段文件中使用带有此标签的 Recycler View -- recycler 视图在 v7 小部件支持文件中可用

  • 转到您各自的 Java 文件并获取对此回收站视图的引用

  • 创建一个新的布局 (xml) 文件来创建您想要在回收视图中渗透的自定义视图,并将其命名为 custom_row.xml

  • 创建一个Javaclass并将其命名为RecyclerviewHolder并使用RecyclerView.ViewHolder扩展它并创建一个超级构造函数,然后访问您的自定义-row.xml 文件

  • 中的所有视图
  • 现在创建另一个 Java class 命名为 RecylerAdapterMyRecyclerAdapter 并扩展通过 RecyclerView.Adapter 并在此处传递您的 RecyclerviewHolder class

    class RecyclerAdapter extends RecyclerView.Adapter<RecyclerviewHolder>

  • 然后创建一个构造函数,按alt+enter就会生成三个方法,即你只需要覆盖RecyclerView.Adapter的这三个方法class

这三种方法是:-

{ onCreateViewHolder , OnBindViewHolder , getCount }

  • OnCreateViewHolder - 一种方法,您将在其中传递自定义视图布局以使用 LayoutInflater 对其进行渗透,在此方法中,您只需要创建 RecyclerviewHolder class 之后你将 return 这个视图对象在这个方法中。

  • OnBindViewHolder - 您可以在此处对您的视图执行任何操作,只需前缀 holder。添加到您所有的视图名称,并使用它们执行任何操作或处理任何事件

  • getCount - 在这里我们将 return 计算您想要渗透自定义视图的次数

  • 现在转到您的 Activity 或片段文件并创建一个 RecyclerAdapter Class 对象并将此适配器设置在您的回收者观点 .

implementation 'com.android.support:design:29+'

根据 build.grade(:app) 文件中的 targetSdkVersion 更改了版本,在我的例子中是 29