【问题标题】:Android RecyclerView item width doesn't match parent and preview doesn't match emulatorAndroid RecyclerView 项目宽度不匹配父和预览不匹配模拟器
【发布时间】:2021-09-18 01:13:43
【问题描述】:

这是我的回收站查看项目 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/recyclerItem"
    app:cardElevation="10dp"
    app:cardUseCompatPadding="false"
    app:cardCornerRadius="10dp">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

       <ImageView
           android:id="@+id/recyclerItemImageView"
           android:padding="8dp"
           android:src="@mipmap/ic_launcher"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
       </ImageView>

       <TextView
           android:id="@+id/recyclerItemTextView"
           android:layout_marginStart="4dp"
           android:textSize="18sp"
           android:textColor="@color/black"
           android:text="item"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
       </TextView>

   </LinearLayout>

</androidx.cardview.widget.CardView>
    

然后像这样预览给我看:

Preview

但是模拟器的结果是这样的:

Emulator result

所以回收站视图中的项目与模拟器上的父级宽度不匹配。其他xml有:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:layout_margin="16dp"
        android:name="com.example.databinding_practice.ListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/activityTextView"/>

    <TextView
        android:id="@+id/activityTextView"
        android:layout_margin="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textColor="@color/black"
        android:textSize="20sp" />

</RelativeLayout>

和片段:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    tools:context=".ListFragment">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

适配器类和 ListFragment 如下:

RecyclerAdapter.kt:

class RecyclerAdapter(var recyclerItems: ArrayList<RecyclerItemModel>) : RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>() {

    class RecyclerViewHolder(val binding: RecyclerItemBinding) : RecyclerView.ViewHolder(binding.root) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        val binding = RecyclerItemBinding.inflate(LayoutInflater.from(parent.context))
        return RecyclerViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        holder.binding.recyclerItemTextView.text = recyclerItems[position].text
        recyclerItems[position].imageMipmapSource?.let { holder.binding.recyclerItemImageView.setImageResource(it) }
    }

    override fun getItemCount(): Int {
        return recyclerItems.size
    }
}

ListFragment.kt:

class ListFragment : Fragment() {
    private var itemList : ArrayList<RecyclerItemModel> = arrayListOf()
    private var recyclerAdapter: RecyclerAdapter = RecyclerAdapter(arrayListOf())
    private lateinit var fragmentListBinding: FragmentListBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        fragmentListBinding = FragmentListBinding.inflate(layoutInflater)
        return fragmentListBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        for (i in 0..3){
            itemList.add(RecyclerItemModel(R.mipmap.ic_launcher,"Item $i"))
        }



        fragmentListBinding.recyclerView.layoutManager = LinearLayoutManager(context)
        fragmentListBinding.recyclerView.adapter = recyclerAdapter
        recyclerAdapter.recyclerItems = itemList
        recyclerAdapter.notifyDataSetChanged()
    }

我哪里做错了?如果我将匹配父级替换为固定大小,例如 200 dp - 300dp ,那可以正常工作,但匹配父级不能。为什么预览和模拟器不匹配?谢谢。

【问题讨论】:

  • 你能用布局检查器调试吗? developer.android.com/studio/debug/layout-inspector 您还需要检查您的inflate 电话(替换为inflate(LayoutInflater.from(context), this, true)
  • 当我替换 RecyclerItemBinding.inflate(LayoutInflater.from(parent.context),parent,false) 这对我有用。非常感谢。

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


【解决方案1】:

在片段中,您应该在 RecyclerView 中将 layout_widthlayout_height 更改为 "0dp"

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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="match_parent"
    tools:context=".ListFragment">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 没有效果 :( ,问题是布局膨胀。这应该是RecyclerItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)。谢谢。
  • 有 Java 版本吗?我找不到RecyclerItemBinding
  • @reactor 不,RecyclerItemBinding 是在项目中使用视图绑定功能时通过视图绑定生成的绑定类。这里link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多