【问题标题】:Android: Change view visibilty in onPageSelected of ViewPager2 doesn't work first timeAndroid:在 ViewPager2 的 onPageSelected 中更改视图可见性第一次不起作用
【发布时间】:2020-04-13 15:08:45
【问题描述】:

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

片段布局

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

我的文本视图

<?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 中的可见性,它会正常工作。

喜欢

                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
                    }

但我不想用这个方法,我也试过在主线程上调用可见性的变化,但似乎并没有解决问题。

【问题讨论】:

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


    【解决方案1】:

    我用 livedata 解决了这个问题。

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

    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 方法。

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 2011-06-21
      • 2021-05-10
      • 2015-02-25
      • 2014-07-04
      相关资源
      最近更新 更多