android RecyclerView最后一项未显示

vql8enpb  于 2023-05-15  发布在  Android
关注(0)|答案(1)|浏览(220)

我不知道什么是确切的问题,但我的recycleview是不显示最后两个项目“项目49”和“项目50”。我尝试了很多方法,但都无法解决这个问题。
在这里,如果您看到最后两个项目,则项目49和项目50不可见

我想显示我所有的50个项目时,向下滚动,但只有48个项目显示

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        >
        <LinearLayout
            android:id="@+id/sheetBehaviour"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            app:behavior_hideable="false"
            app:behavior_peekHeight="462dp"

            >
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycleview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:itemCount="50"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                tools:listitem="@layout/layout_items"
                android:nestedScrollingEnabled="false"
                />
        </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
My adapter class 

package com.example.recycleviewissue

import android.R
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

class MyListAdapter(listdata: MutableList<String>) :
    RecyclerView.Adapter<MyListAdapter.ViewHolder>() {
    private val listdata: MutableList<String>

    // RecyclerView recyclerView;
    init {
        this.listdata = listdata
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val listItem: View =
            layoutInflater.inflate(com.example.recycleviewissue.R.layout.layout_items, parent, false)
        return ViewHolder(listItem)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.textView.setText(listdata[position])

    }

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

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var textView: TextView

        init {
            textView = itemView.findViewById<TextView>(com.example.recycleviewissue.R.id.items) as TextView
        }
    }
}
class MainActivity : AppCompatActivity() {

    lateinit var recycleView : RecyclerView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recycleView = findViewById(R.id.recycleview);
        val listOfItem = mutableListOf<String>()
        for(i in 1..50){
            listOfItem.add("Item "+i)
        }
        val adapter : MyListAdapter = MyListAdapter(listOfItem)
        recycleView.adapter = adapter

        val sheetBehavior = BottomSheetBehavior.from(findViewById(R.id.sheetBehaviour));
        sheetBehavior.isDraggable = false
    }
}

enter image description here

bmp9r5qi

bmp9r5qi1#

这看起来更像是CordinatorLayout的行为问题。您定义了recyclerview的行为,但它是LinearLayout的子项。在这种情况下,您应该在LinearLayout中添加另一个CoordinatorLayout,以使recyclerview的滚动行为有意义。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" >
        
        <LinearLayout
            android:id="@+id/sheetBehaviour"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            app:behavior_hideable="false"
            app:behavior_peekHeight="462dp" >
            
<!--             Add this coordinatorlayout -->
            <androidx.coordinatorlayout.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent" >
                
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycleview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:itemCount="50"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                    tools:listitem="@layout/layout_items"
                    android:nestedScrollingEnabled="false" />
                
            </androidx.coordinatorlayout.widget.CoordinatorLayout>
        </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

相关问题