简体   繁体   English

如何在Android中为RecyclerView添加分隔线?

[英]How do I go about adding dividers for RecyclerView in android?

Currently, my RecyclerView is displaying correctly, but does not have dividers between each data set/item. 当前,我的RecyclerView正确显示,但是每个数据集/项目之间没有分隔符。

This is the code I'm using for my RecyclerView, with the layout of the the items displayed in the code snippet below. 这是我用于RecyclerView的代码,项目的布局显示在下面的代码片段中。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/relativeLayout">

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

</RelativeLayout>

This is the code which determines how the items are arranged. 这是确定项目排列方式的代码。

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

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/groupName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Group Name"
            />

        <TextView
            android:id="@+id/groupDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Group Date"
            />

        <TextView
            android:id="@+id/groupLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Group Location"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/className"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="right"
            android:text="Class Name"
            />
    </LinearLayout>

</LinearLayout>

Here's a picture of what the app looks like, with the RecyclerView: http://imgur.com/WnJXkuV 这是带有RecyclerView的应用程序外观的图片: http : //imgur.com/WnJXkuV

You can add Divider as below 您可以如下添加分频器

recyclerView.addItemDecoration
            (new DividerItemDecoration(MyActivity.this,
                    DividerItemDecoration.VERTICAL_LIST));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android Studio中将图像添加到位图? - How do I go about adding images to a bitmap in Android Studio? 如何像微软待办那样在空的 RecyclerView 中显示分隔符? - How can I show dividers in an empty RecyclerView like in Microsoft To Do? 在 GridLayout RecyclerView 中添加分隔线 - Adding dividers in GridLayout RecyclerView 我该如何添加到Maven Central上的gradle库? - How do I go about adding a library to gradle that is on Maven Central? 如何在Android中使用虚线/虚线分隔线创建ListView? - How do I create a ListView with dashed / dotted line dividers in Android? 如何将从doInBackground()获得的数据应用于我的RecyclerView? - How do I go about applying data I get from doInBackground() to apply to my RecyclerView? Android:添加项目后如何返回到RecyclerView的顶部? - Android: how do I return to to top of RecyclerView after adding item? 如何正确构建Android应用程序? - How do I properly go about building an Android App? 我该如何实施Android UI,尤其是此图库? - How do I go about implementing Android UI, specifically this gallery? 如何仅在 RecyclerView 中的某些元素之间添加分隔线? - How do you add dividers between only certain elements in a RecyclerView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM