Android:第一次在 ViewPager2 的 onPageSelected 中更改视图可见性不起作用

Android: Change view visibilty in onPageSelected of ViewPager2 doesn't work first time

我正在开发一个带有 viewpager2 的应用程序,当在 viewpager 中选择的元素发生变化时,某些视图的可见性也会发生变化。问题是,当适配器分配给 viewpager 时,它被称为 onPageSelected,索引为 0,但视图的可见性不会改变,如果我滚动,它们就会正确改变。

fragment_layout

<?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">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="btn1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="btn2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

BaseFragment代码

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import kotlinx.android.synthetic.main.fragment_layout.*

class BaseFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_layout,container,false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewpager.adapter = MyAdapter(arrayOf("Page1","Page2"))
        viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                    if (position == 0){
                        btn1.visibility = View.VISIBLE
                        btn2.visibility = View.GONE
                    }else {
                        btn1.visibility = View.GONE
                        btn2.visibility = View.VISIBLE
                    }
            }
        })
    }
}

MyAdapter 代码


import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class MyAdapter(private val myDataset: Array<String>) :
    RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder.
    // Each data item is just a string in this case that is shown in a TextView.
    class MyViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)


    // Create new views (invoked by the layout manager)
    override fun onCreateViewHolder(parent: ViewGroup,
                                    viewType: Int): MyAdapter.MyViewHolder {
        // create a new view
        val textView = LayoutInflater.from(parent.context)
            .inflate(R.layout.my_text_view, parent, false) as TextView
        // set the view's size, margins, paddings and layout parameters
        return MyViewHolder(textView)
    }

    // Replace the contents of a view (invoked by the layout manager)
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.textView.text = myDataset[position]
    }

    // Return the size of your dataset (invoked by the layout manager)
    override fun getItemCount() = myDataset.size
}

my_text_view

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/colorPrimary"
    android:textColor="@android:color/white"
    android:textSize="32sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

显然,如果我更改视图或 viewpager 中 post 的可见性,它会正常工作。

喜欢

                viewpager.post {
                    if (position == 0){
                        btn1.visibility = View.VISIBLE
                        btn2.visibility = View.GONE
                    }else {
                        btn1.visibility = View.GONE
                        btn2.visibility = View.VISIBLE
                    }
                }

                    if (position == 0){
                        btn1.post {
                            btn1.visibility = View.VISIBLE
                        }
                        btn2.post {
                            btn2.visibility = View.GONE
                        }
                    }else {
                        btn1.visibility = View.GONE
                        btn2.visibility = View.VISIBLE
                    }

但是我不想用这个方法,我也试过在主线程调用改变可见性,但是好像没有解决问题。

我用livedata解决了问题

我已经声明了一个 mutableLiveData 和 post 索引更改,然后我用片段生命周期注册了一个观察者。

val liveData:MutableLiveData<Int> = MutableLiveData()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    liveData.observe(this, Observer {
        if (position == 0){
            btn1.visibility = View.VISIBLE
            btn2.visibility = View.GONE
        }else {
            btn1.visibility = View.GONE
            btn2.visibility = View.VISIBLE
        }   
    })
}

然后在 viewpager onPageSelected

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    viewpager.adapter = MyAdapter(arrayOf("Page1","Page2"))
    viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            liveData.postValue(position)
        }
    })
}

我不明白问题的原因,但很明显它与片段的生命周期有关,但我不明白为什么在viewpager的回调中调用可见性的变化甚至记录都不起作用它在 onResume 或 onStart 方法中。