回收者视图不尊重列表项的边距

Vai*_*gal 0 xml android kotlin android-recyclerview

它应该是什么样子 -

工作室样本

看起来怎么样——

安卓截图

列表项.xml -

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
    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:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/card_background"
    android:layout_marginVertical="5dp"
    android:layout_marginHorizontal="10dp"
    app:cardCornerRadius="8dp">

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

        <ImageView
            android:id="@+id/is_app_or_web_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/card_spacing"
            android:padding="@dimen/image_padding"
            app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="@id/delete_image_view"
            tools:src="@drawable/is_website_icon_24" />

        <TextView
            android:id="@+id/service_name_text_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="@dimen/card_spacing"
            android:text="@string/service_name"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
            app:layout_constraintLeft_toRightOf="@id/is_app_or_web_image_view"
            app:layout_constraintRight_toLeftOf="@id/edit_image_view"
            app:layout_constraintTop_toTopOf="@id/delete_image_view" />


        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/delete_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/card_spacing"
            android:layout_marginEnd="@dimen/card_spacing"
            android:background="@color/card_foreground"
            android:padding="@dimen/image_padding"
            android:src="@drawable/ic_baseline_delete_20"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/show_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/card_spacing"
            android:background="@color/card_foreground"
            android:padding="@dimen/image_padding"
            android:src="@drawable/ic_baseline_show_20"
            app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
            app:layout_constraintRight_toLeftOf="@id/delete_image_view"
            app:layout_constraintTop_toTopOf="@id/delete_image_view" />


        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/edit_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/card_spacing"
            android:background="@color/card_foreground"
            android:padding="@dimen/image_padding"
            android:src="@drawable/ic_baseline_edit_20"
            app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
            app:layout_constraintRight_toLeftOf="@id/show_image_view"
            app:layout_constraintTop_toTopOf="@id/delete_image_view" />


        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginVertical="@dimen/card_spacing"
            app:cardBackgroundColor="@color/card_foreground"
            app:cardCornerRadius="@dimen/corner_radius"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="@id/is_app_or_web_image_view"
            app:layout_constraintRight_toRightOf="@id/delete_image_view"
            app:layout_constraintTop_toBottomOf="@id/is_app_or_web_image_view">


            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/service_password_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:background="#00000000"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="@string/service_password" />


        </androidx.cardview.widget.CardView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

我已经尝试过的 -

将列表项的高度设置为固定值。类似于 200 dp(没有任何效果。就好像该值根本没有输入一样)

尝试更改布局管理器(无效)

现在,我什至不知道该尝试什么。任何帮助都是有用的。

适配器代码 -

package com.kenetic.savepass.adapters

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.kenetic.savepass.R
import com.kenetic.savepass.databinding.PassListBinding
import com.kenetic.savepass.password.PasswordData


class PassAdapter() :
    ListAdapter<PasswordData, PassAdapter.PassViewHolder>(diffCallBack) {

    class PassViewHolder(val binding: PassListBinding) : RecyclerView.ViewHolder(binding.root) {
        var verifiedWithPassword = false
        private val TAG = "PassAdapter"

        fun bind(passwordData: PasswordData) {
            binding.isAppOrWebImageView.setImageResource(
                if (passwordData.isAnApplication) {
                    R.drawable.is_application_icon_24
                } else {
                    R.drawable.is_website_icon_24
                }
            )
            binding.serviceNameTextView.text = passwordData.serviceName

            binding.servicePasswordTextView.text = passwordData.servicePassword

            binding.deleteImageView.setOnClickListener {
                Log.d(TAG, "delete image onClick working")
            }
            binding.showImageView.setOnClickListener {
                Log.d(TAG, "show image onClick working")
            }
            binding.editImageView.setOnClickListener {
                Log.d(TAG, "edit image onClick working")
            }
        }
    }

    companion object {
        private val diffCallBack = object : DiffUtil.ItemCallback<PasswordData>() {
            override fun areItemsTheSame(oldItem: PasswordData, newItem: PasswordData) =
                (oldItem.id == newItem.id)

            override fun areContentsTheSame(oldItem: PasswordData, newItem: PasswordData) =
                (oldItem == newItem)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PassViewHolder {
        return PassViewHolder(PassListBinding.inflate(LayoutInflater.from(parent.context)))
    }

    override fun onBindViewHolder(holder: PassViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
}
Run Code Online (Sandbox Code Playgroud)

Flo*_*tin 5

我遇到了同样的问题,但事实证明我没有以好的方式膨胀 recyclerview 项目。

我之前对这些参数进行了夸大:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
        return TodoViewHolder(
            TodoListItemBinding.inflate(LayoutInflater.from(parent.context))
        )
    }
Run Code Online (Sandbox Code Playgroud)

这是使项目之间的边距正确显示的膨胀方式:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
        return TodoViewHolder(
            TodoListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }
Run Code Online (Sandbox Code Playgroud)