【问题标题】:Space between recyclerView items not showing even after adding margin即使在添加边距后,recyclerView 项目之间的空间也不显示
【发布时间】:2021-12-01 07:48:33
【问题描述】:

我尝试在 item_layout 中的 recyclerView 项目之间添加空间。它在 XML 预览中运行良好,但在设备上运行不佳。

city_item_layout:

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

<data>

    <variable
        name="viewClickListener"
        type="android.view.View.OnClickListener" />

    <variable
        name="weatherResponseBody"
        type="com.example.weatherapp.model.WeatherResponseBody" />

    <variable
        name="city"
        type="com.example.weatherapp.model.CityWeatherReport" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="@{viewClickListener}"
    android:padding="20dp"
    android:background="@drawable/curved_background"
    android:foreground="?attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:elevation="1dp">

    <ImageView
        android:id="@+id/weather_state_icon_iv"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_baseline_wb_sunny_24"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:imageUrl="@{city.weatherIcon}"/>

    <TextView
        android:id="@+id/city_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@{city.city}"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginStart="20dp"
        app:layout_constraintStart_toEndOf="@id/weather_state_icon_iv"
        app:layout_constraintTop_toTopOf="@id/weather_state_icon_iv"
        tools:text="New York" />

    <TextView
        android:id="@+id/country_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{city.country}"
        android:textStyle="italic"
        app:layout_constraintEnd_toEndOf="@id/city_tv"
        app:layout_constraintTop_toBottomOf="@id/city_tv"
        tools:text="Nigeria" />

    <TextView
        android:id="@+id/weather_state_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{city.mainWeather}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@id/weather_state_icon_iv"
        app:layout_constraintStart_toStartOf="@id/weather_state_icon_iv"
        app:layout_constraintTop_toBottomOf="@id/weather_state_icon_iv"
        app:layout_constraintVertical_bias="0.0"
        tools:text="Sunny" />

    <TextView
        android:id="@+id/temperature_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="170dp"
        android:text="@{city.tempToString()}"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/country_tv"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="@string/temperature" />

</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView_layout:

<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainFragment">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:minHeight="?attr/actionBarSize">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Weather Forecast"
        android:textSize="18sp"
        android:textStyle="bold" />
</androidx.appcompat.widget.Toolbar>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingHorizontal="20dp">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/city_list_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/city_item_layout" />
</LinearLayout>

XML 预览: enter image description here

在设备上安装后:

enter image description here

我不知道我做错了什么。我需要帮助

【问题讨论】:

  • 为什么你的 RecyclerView 在一个 LinearLayout 里面?只是为了填充?最好使用Android的Space view
  • @rtsketo,我在尝试解决问题时用 LinearLayout 封装了 recyclerView

标签: android xml kotlin android-layout android-recyclerview


【解决方案1】:

尝试用item_layout.xml 中的LinearLayout 包装你的ConstraintLayout,它应该是:

<LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_margin="10dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:onClick="@{viewClickListener}"
        android:padding="20dp"
        android:background="@drawable/curved_background"
        android:foreground="?attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:elevation="1dp">

<!-- your code here-->

</LinearLayout>

你可以试一试,祝你好运;)

【讨论】:

  • 哇...谢谢。它就像魔术一样工作。
【解决方案2】:

我不熟悉 RecyclerView 中的填充,但它似乎忽略了它。一个肯定会起作用的解决方案是将您的 ConstraintLayout 包装在另一个 ConstraintLayout 中,所有边缘的边距为 20dp。

做类似的事情,但使用 FrameLayout 而不是 ConstraintLayout 可能会更高效。

无论如何,我不能保证这是最好的解决方案,但它会起作用。

【讨论】:

  • 我在获取问题解决方案的过程中添加了填充。我会像你说的那样尝试 FrameLayout。谢谢
猜你喜欢
  • 2017-01-04
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 2013-04-01
  • 2021-06-28
  • 1970-01-01
相关资源
最近更新 更多