android的ViewPager动画无法在全屏模式下覆盖整个屏幕

时间:2019-03-15 15:23:45

标签: android android-viewpager fullscreen

在我的应用程序中,我正在全屏活动中使用ViewPager,一切正常,除了正确显示最后一张幻灯片的onSwipe动画外,它的行为就像是一个NavigationBar(照片中的示例)。

我该怎么办?

https://i.stack.imgur.com/f9Ldq.png

我的活动的.kt代码

import android.animation.ArgbEvaluator
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import kotlinx.android.synthetic.main.activity_walkthrough.*

//activity showing introduction to app which contains all app's not obvoius functions, and which welcomes new user - it should be shown only after first start of app (not after actualisations!)

class WalkthroughActivity : AppCompatActivity()
{
    //adapter's class for ViewPager
    inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm)
    {
        //contains all set fragments
        private val page = mutableListOf<Fragment>()

        //adds fragment to list ('page' variable specified above)
        fun addFragment(fragment: Fragment, backgroundColor: Int): SectionsPagerAdapter
        {
            page.add(fragment)
            fragmentsColors.add(backgroundColor)

            return this
        }

        //returns fragmnet by it's index
        override fun getItem(position: Int): Fragment
        {
            return page[position]
        }

        //return number of all fragments in list ('page' variable specified above)
        override fun getCount(): Int
        {
            return page.size
        }
    }

    //adapter for ViewPager
    private lateinit var adapter: SectionsPagerAdapter

    //list of all fragments background's colors - used to moothly change background color while user swipes it
    private var fragmentsColors = mutableListOf<Int>()

    //determines if user want to slide last page - which means this activity should be finished
    private var isSwipeOnLastPage=false

    override fun onCreate(savedInstanceState: Bundle?)
    {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)

        overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_walkthrough)

        //adding listeners to slider movements events
        container.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {}

            //animating background color change on sliding fragments
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int)
            {
                if(position == adapter.count-1)
                {
                    if(isSwipeOnLastPage)
                        stopThisActivity()
                    else
                        isSwipeOnLastPage=true
                }
                else
                    isSwipeOnLastPage=false

                //color update
                val colorUpdate = ArgbEvaluator().evaluate( positionOffset, fragmentsColors[position], fragmentsColors[if (position < fragmentsColors.size - 1) position + 1 else fragmentsColors.size - 1]) as Int

                container.setBackgroundColor(colorUpdate)

                //chanigng statusBar's color
                window.statusBarColor=colorUpdate
                window.navigationBarColor=colorUpdate
            }

            override fun onPageSelected(position: Int)
            {
                //if selected postiion is the last one - 'skip button' becomes invisible and 'next button' becomes 'finish button'
                if(position == adapter.count-1)
                {
                    skipBTN.visibility= View.INVISIBLE

                    nextBTN.text = "Zakończ"
                    nextBTN.setOnClickListener {
                        stopThisActivity()
                    }
                }
                else
                    skipBTN.visibility= View.VISIBLE
            }
        })

        //if 'skip button' is clicked whole activity finishes
        skipBTN.setOnClickListener {
            stopThisActivity()
        }

        //if 'next button' is clicked currently shown fragment changes to the next one (with animation)
        nextBTN.setOnClickListener {
            container.setCurrentItem(container.currentItem+1, true)
        }

        //creating ViewPager's adapter and specyfiyng it's fragments
        adapter = SectionsPagerAdapter(supportFragmentManager)
            .addFragment(WalkthroughFragmentOne(), ContextCompat.getColor(this, android.R.color.holo_orange_light))
            .addFragment(WalkthroughFragmentOne(), ContextCompat.getColor(this, android.R.color.holo_green_light))
            .addFragment(WalkthroughFragmentOne(), ContextCompat.getColor(this, android.R.color.holo_red_light))

        //chanigng statusBarColor to match first fragment background's color
        window.statusBarColor=ContextCompat.getColor(this, android.R.color.holo_orange_light)

        //setting number of dots to number of set fragments in ViewPager's adapter

        container.adapter = this.adapter
    }

    //stops this activity - used to perform additional actions before ending
    private fun stopThisActivity()
    {
        this.finish()
    }
}

我的活动的.xml文件

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:fitsSystemWindows="false"
             tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                         xmlns:tools="http://schemas.android.com/tools"
                                                         xmlns:app="http://schemas.android.com/apk/res-auto"
                                                         android:id="@+id/main_content"
                                                         android:layout_width="match_parent"
                                                         android:layout_height="match_parent"
                                                         tools:context=".MainActivity"
                                                         android:padding="0dp">


        <androidx.viewpager.widget.ViewPager
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <ImageView android:layout_width="match_parent"
                   android:layout_height="1dp"
                   android:alpha="0.2"
                   android:src="@drawable/black_gradient_divider" app:layout_anchorGravity="left|top"
                   app:layout_anchor="@+id/nextBTN"/>

        <Button
                android:text="Pomiń"
                android:textColor="#64000000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/skipBTN" app:layout_anchorGravity="left|bottom"
                app:layout_anchor="@+id/container"
                android:background="@android:color/transparent"/>
        <Button
                android:text="Dalej"
                android:textColor="#64000000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/nextBTN" app:layout_anchorGravity="right|bottom"
                app:layout_anchor="@+id/container"
                android:background="@android:color/transparent"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</FrameLayout>

0 个答案:

没有答案