当ViewPager的OnPageSelected()称为Kotlin时,请在片段中获取null



使用kotlin android扩展名进行视图初始化

PageSelece的活动代码:

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

默认情况下仅查看Pager仅3个片段。当前,上一个和下一个。

当您移动到某个页面时,所有不在此范围之外的页面呼叫onDestroyView(),并且视图被破坏,即它变为null。当此页面再次进入范围时,它必须再次创建视图,从而调用onCreateView()。当它创建

时,该视图为null

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

希望这能解决您的问题。

最新更新