Recyclerview仅显示第一项

时间:2019-11-18 18:40:35

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

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

这是MyAdapter

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)
        }

    }


}

这是片段:

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
    }

这是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>

还有一个MainActivity托管fragment_list1.xml和nav_graph.xml

3 个答案:

答案 0 :(得分:2)

list_item.xml中更改LinearLayout的属性:

android:layout_height="match_parent"

收件人:

android:layout_height="wrap_content"

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

答案 1 :(得分:1)

您将Mono<List<String>> response = Flux.fromIterable(queue) .flatMap(this::callHttp) .collectList(); private Mono<String> callHttp(String req) { return webClient .post() .syncBody(req) .retrieve() .bodyToMono(String.class) .retry(3); // retries failed requests at most 3 times } 设置为覆盖整个屏幕的RecyclerView项match_parent。尝试将其设置为height。检查以下内容:

wrap_content

如果您尝试<?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> ,那么您也可以看到其他项目。

答案 2 :(得分:0)

您的list_item的高度是match_parent,它会覆盖所有剩余空间

如果您只有一个TextView,请不要写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>