RecyclerView项目之间存在较大间隙

3

分享你的 XML 文件。 - Uttam Panchasara
您可以在此处查看有关编程的相关内容:https://github.com/userRidae/RecyclerView/tree/master/app/src/main/res/layout - Ridae HAMDANI
2个回答

11

像这样更改您的布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.breuhteam.recyclerview.MainActivity">

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

view_item.xml

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

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/itemView"
    android:src="@drawable/diy"
    android:layout_margin="1dp"
    android:adjustViewBounds="true"/>
</RelativeLayout>

谢谢,它对我有用,但当我向下滚动并想返回到上一个项目时,某些项目的大小会改变。 - Ridae HAMDANI
这是因为RecyclerView每次创建新视图时,您必须为项目设置标记。 - Uttam Panchasara
我该怎么做呢? - Ridae HAMDANI
示例:https://dev59.com/sF8d5IYBdhLWcg3wYhWX - Uttam Panchasara
这个答案对我有用,谢谢@UttamPanchasara。 - Nelson Katale
layout_margin和adjustViewBounds属性非常重要,但大多数答案都没有提到。 - Michele Dorigatti

0
所以我遇到了同样的问题,但是是关于文本的,但我觉得这并不重要。
我们需要将约束布局的高度更改为自适应内容而不是匹配父级输入图像描述

翻译成中文:目前写的方式,你的回答不清楚。请编辑以添加更多细节,帮助其他人理解如何解答提出的问题。您可以在帮助中心找到有关撰写良好答案的更多信息。 - hossein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接