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



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

碎片_布局

<?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 {
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解决了这个问题。

我已经声明了一个可变的LiveData并发布了索引更改,然后注册了一个具有片段生命周期的观察者。

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

然后在视图中寻呼机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方法中,可见性的更改都不起作用。

最新更新