获取视图当ViewPager的onPageSelected()调用Kotlin时,片段为空

时间:2018-04-25 12:50:18

标签: android kotlin

使用Kotlin Android Extensions进行视图初始化

PageSelected的活动代码:

introPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                when(position) {
                    0 -> (pagerAdapter.getItem(position) as SearchFragment).startViewAnimations()
                }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

片段方法:

fun startViewAnimations() {
        rippleBack?.startRippleAnimation()
        centerImage?.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely))

        val handler = Handler()
        handler.postDelayed({
            relAllViews?.visibility = View.VISIBLE
            relAllViews?.animate()?.alpha(1.0f)
            rippleBack?.animate()?.alpha(0.0f)
            rippleBack?.visibility = View.GONE
            rippleBack?.stopRippleAnimation()
            centerImage?.clearAnimation()
        }, 3500)
    }

如果我在片段onViewCreated()中使用相同的功能,它可以正常使用代码示例:

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

完整碎片代码:

package com.beeland.consumer.fragment.appintro

import android.os.Bundle
import android.os.Handler
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import com.beeland.consumer.R
import com.beeland.consumer.utils.Utils
import kotlinx.android.synthetic.main.fragment_search.*

/**
 * @author Yash
 * @since 23-04-2018.
 */
class SearchFragment : Fragment() {

    /**
     * Method to replace layouts in App Intro Section
     *
     * @return IntroFragment's new instance
     */
    fun newInstance(): SearchFragment {
        val intro = SearchFragment()
        val args = Bundle()
        args.putString("", "")
        intro.arguments = args
        return intro
    }

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

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

    fun startViewAnimations() {
        rippleBack?.startRippleAnimation()
        centerImage?.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely))

        val handler = Handler()
        handler.postDelayed({
            relAllViews?.visibility = View.VISIBLE
            relAllViews?.animate()?.alpha(1.0f)
            rippleBack?.animate()?.alpha(0.0f)
            rippleBack?.visibility = View.GONE
            rippleBack?.stopRippleAnimation()
            centerImage?.clearAnimation()

            handler.postDelayed({
                Utils.bounceAnimationVisible(txt5Km)
            }, 300)

            handler.postDelayed({
                Utils.bounceAnimationVisible(txtPostalDelivery)
            }, 600)

            handler.postDelayed({
                Utils.bounceAnimationVisible(img1)
            }, 900)

            handler.postDelayed({
                Utils.bounceAnimationVisible(img2)
            }, 1200)

            handler.postDelayed({
                Utils.bounceAnimationVisible(img3)
            }, 1500)

            handler.postDelayed({
                Utils.bounceAnimationVisible(imgRes1)
                Utils.bounceAnimationVisible(imgRes2)
                Utils.bounceAnimationVisible(imgRes3)
                Utils.bounceAnimationVisible(imgRes4)
            }, 1800)

        }, 3500)
    }
}

1 个答案:

答案 0 :(得分:0)

默认情况下,查看寻呼机仅缓存3个片段。当前,上一个和下一个。

当您移动到某个页面时,超出此范围的所有页面都会调用onDestroyView()并且视图被销毁,即它变为空。当此页面再次进入范围时,它必须再次创建视图,从而调用onCreateView()。在创建

时视图为null

可以使用setOffscreenPageLimit(int limit)自定义缓存片段(实际视图)的范围。

希望这能解决你的问题。