Recyclerview的宽度比父宽度大

时间:2019-06-14 05:26:19

标签: android kotlin android-recyclerview recycler-adapter

即使我将xml中的宽度设置为match_parent,我也不知道为什么RecyclerView的宽度大于父宽度。我认为xml没什么问题,但是我不确定适配器。这是适配器的代码:

class ListAdapter(val data : ArrayList<devices>): RecyclerView.Adapter<ListAdapter.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.devicelist, parent, false)
            )
        }

        override fun getItemCount() = data.size

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val list = data[position]
            holder.view.listimgtxt.text = list.name
            holder.view.listsw1.text = list.sw1
            holder.view.listsw2.text = list.sw2
            holder.view.listsw3.text = list.sw3
            holder.view.listsw4.text = list.sw4
        }
        class ViewHolder(val view: View) : RecyclerView.ViewHolder(view)
    }

这是我的xml代码(由于代码太长而删除了imageviews和textviews):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content">

</android.support.constraint.ConstraintLayout>

包含recyclerview的xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/app_bar_main"
        tools:context=".MainActivity"
        android:background="#FFFFFF"
>

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

输出:enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用约束布局,并将“ Recycler”视图的宽度设置为0dp并将其约束在父对象的左侧和右侧...匹配父对象不遵守约束。

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/content_frame"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        />
</android.support.constraint.ConstraintLayout>

还要将列表项中所有图像的宽度设置为0dp,以便根据约束设置将其调整为屏幕尺寸。