ViewPager 子视图无法match_parent

ViewPager childView cannot match_parent

提问人:GHH 提问时间:11/6/2023 更新时间:11/6/2023 访问量:33

问:

custom_view_game_menu_list.xml

<?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"
    android:background="#000000">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:spanCount="4" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerView" />

</androidx.constraintlayout.widget.ConstraintLayout>

ViewPager 代码在 GameMenuListCustomView.kt 中非常简单

    gameMenuListAdapter = GameMenuListAdapter(gameMenuList)
    viewBinding.viewPager.adapter = gameMenuListAdapter
    viewBinding.viewPager.offscreenPageLimit = gameMenuList.count()

GameMenuListAdapter.kt (英语)

class GameMenuListAdapter(private val menuList: List<Menu>) : PagerAdapter() {

    override fun getCount(): Int {
        return menuList.count()
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = GameMenuListCustomView(container.context)
        view.setData(menuList[position].gameList, menuList[position].menuName == "熱門")
        container.addView(view, ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
        return view
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as GameMenuListCustomView)
    }
}

item_game_menu_list.xml(ViewPager 的子视图布局)

<?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:background="#0000ff"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:background="#ffff00"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我设置为子视图以确保子视图未match_parent。android:background="#ffff00"

我还设置了PagerAdapter仍然无法正常工作。container.addView(view, ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)

为什么 childView 无法match_parent?

enter image description here

android-layout android-viewpager

评论


答:

0赞 Nandish.Gravity 11/6/2023 #1

我认为问题出在GameMenuListCustomView(container.context)中 检查该函数的代码

评论

0赞 Community 11/7/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。