Android Recyclerview仅显示第一项

Android Recyclerview仅显示第一项,android,android-layout,android-fragments,kotlin,android-recyclerview,Android,Android Layout,Android Fragments,Kotlin,Android Recyclerview,我的recyclerview仅显示ArrayList中的一个项,第一个,当我调试适配器时,它显示的getItemCount()值为3,看起来正常,并且据我所知没有运行错误,请帮助: 这是我的适配器 class MyAdapter(private val myDataset: ArrayList<String>) : RecyclerView.Adapter<MyAdapter.Holder>() { override fun onCreateViewH

我的recyclerview仅显示ArrayList中的一个项,第一个,当我调试适配器时,它显示的getItemCount()值为3,看起来正常,并且据我所知没有运行错误,请帮助:

这是我的适配器

class MyAdapter(private val myDataset: ArrayList<String>) :
    RecyclerView.Adapter<MyAdapter.Holder>() {


    override fun onCreateViewHolder(parent: ViewGroup,
                                    viewType: Int): Holder {

        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item, parent, false)

        return Holder(view)
    }


    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.textView?.text = myDataset[position]
    }


    override fun getItemCount() = myDataset.size


    class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var textView:TextView? = null

        init {
            textView = itemView.findViewById(R.id.name)
        }

    }


}
类MyAdapter(私有val myDataset:ArrayList):
RecyclerView.Adapter(){
在CreateViewHolder(父级:ViewGroup,
视图类型:Int):保持架{
val view=LayoutInflater.from(parent.context)
.充气(R.layout.list_项,父项,假)
报税表持有人(视图)
}
覆盖BindViewHolder(holder:holder,position:Int){
holder.textView?.text=myDataset[位置]
}
重写getItemCount()=myDataset.size
类持有者(itemView:View):RecyclerView.ViewHolder(itemView){
变量textView:textView?=null
初始化{
textView=itemView.findViewById(R.id.name)
}
}
}
这是片段:

class FragmentList1 : Fragment() {

    var myArrayList: ArrayList <String> = arrayListOf("Fausto", "Félix", "Nacho")


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment

        var view = inflater.inflate(R.layout.fragment_list1,
            container, false)

        var recycler: RecyclerView = view.findViewById(R.id.recyclerView)

        recycler.setHasFixedSize(true)
        recycler.layoutManager = LinearLayoutManager(context)
        recycler.adapter = MyAdapter(myArrayList)

        return view
    }
class FragmentList1:Fragment(){
var myArrayList:ArrayList=arrayListOf(“Fausto”、“Félix”、“Nacho”)
覆盖创建视图(
充气器:布局充气器,容器:视图组?,
savedInstanceState:捆绑?
):查看{
//为该碎片膨胀布局
var视图=充气机。充气(R.layout.fragment_列表1,
容器(假)
var recycler:RecyclerView=view.findviewbyd(R.id.RecyclerView)
recycler.setHasFixedSize(真)
recycler.layoutManager=LinearLayoutManager(上下文)
recycler.adapter=MyAdapter(myArrayList)
返回视图
}
这是fragment_list1.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".FragmentList1">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />

</LinearLayout>

这是list_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="28dp"
        android:text="Aqui va el nombre"
         />



</LinearLayout>


还有一个主活动承载着一个fragment_list1.xml,以及
list_item.xml中的nav_graph.xml

更改
LinearLayout
的属性:

android:layout_height="match_parent"
致:


因为第一个项目占用了所有可用空间,而您看不到其余的项目。

您将
匹配父项
到您的回收视图项目
高度
,该项目覆盖了所有屏幕。尝试将其设置为
包装内容
。检查以下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="28dp"
        android:text="Aqui va el nombre"/>
</LinearLayout>


如果您尝试
滚动
,那么您也可以看到其他项目。

您的列表项目的高度是匹配的,覆盖了所有剩余空间

如果您只有一个文本视图,则不要编写
android:orientation=“horizontal”

默认情况下,它是水平的

<LinearLayout 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_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="28dp"
        android:text="Aqui va el nombre"/>
</LinearLayout>

您的list\u item.xml根布局设置为与父级匹配,它应该是wrap\u内容
<LinearLayout 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_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="28dp"
        android:text="Aqui va el nombre"/>
</LinearLayout>