安卓轮播图(ViewPager 无限循环滚动)

屏幕划动是在两个完整界面间的转换,它在一些UI中很常见,比如设置向导和幻灯放映。Android官方培训课程中文版

有关 ViewPager 用法介绍可以参考官方文档 Android

安卓官方文档 https://developer.android.google.cn/develop/index.html

此为一个博客评论中看到的方案,实践后确实可行,做个记录。

修改 ViewPager 的 Change 事件达到循环滚动(Java):

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (position == 0 && positionOffsetPixels == 0)
            mViewPager.setCurrentItem(data.size() - 2, false);
        else if (position == mFragments.size() - 1 && positionOffsetPixels == 0)
            mViewPager.setCurrentItem(1, false);
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});

自动切换 View (Kotlin)
参考 RxJava轮询器:interval

Observable.interval(3, 3, TimeUnit.SECONDS).bindToLifecycle(this)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe { next ->
            var currentIndex = mViewPager.currentItem
            if (++currentIndex == mAdapter.count) {
                mViewPager.currentItem = 1
            } else {
                mViewPager.setCurrentItem(currentIndex, true)
            }
        }

以下为 Kotlin 实现的代码 ->
ViewPager 布局和设置

class Home : Fragment() {

    private lateinit var mAdapter: HomeLoopPagerAdapter

    private lateinit var mViewPager: ViewPager

    /**
    * 界面布局
    */
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View = UI {
        linearLayout {
            orientation = LinearLayout.VERTICAL

            viewPager {
                id = HomeTabFragment.ID_VIEW_PAGER
                backgroundColor = Color.WHITE
            }.lparams {
                width = matchParent
                height = dip(130)
                topMargin = dip(8)
                bottomMargin = dip(8)
            }
        }
    }.view

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

        mAdapter = HomeLoopPagerAdapter()

        mViewPager = view.findViewById(ID_VIEW_PAGER) as ViewPager
        mViewPager.adapter = mAdapter
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        /**
        * 适配器数据
        */
        val data = arrayListOf()

        for (i in 1..5) {
            val slideshowView = SimpleDraweeView(context)
            val hierarchy = GenericDraweeHierarchyBuilder(resources)
                    .setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                    .setPlaceholderImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                    .setPlaceholderImage(R.mipmap.img_home_slideshow)
                    .build()
            slideshowView.hierarchy = hierarchy
            slideshowView.setImageURI(Uri.parse(""), context)

            val bean = HomeLooperAdapterBean(slideshowView)
            data.add(bean)
        }
        mAdapter.updateData(data)

        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            /**
            * 关键代码
            */
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if (position == 0 && positionOffsetPixels == 0)
                    mViewPager.setCurrentItem(data.size - 2, false)
                else if (position == data.size - 1 && positionOffsetPixels == 0)
                    mViewPager.setCurrentItem(1, false)
            }

            override fun onPageSelected(position: Int) {
            }
        })
    }

    companion object {
        val ID_VIEW_PAGER = 1001
    }

}

适配器无变化

class HomeLoopPagerAdapter : PagerAdapter() {

    val mData = arrayListOf()

    override fun isViewFromObject(view: View?, `object`: Any?): Boolean = view == `object`

    override fun getCount(): Int = mData.size

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any?) = container.removeView(`object` as View)

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = mData[position].simpleDraweeView
        container.addView(view)
        return view
    }

    fun updateData(list: List) {
        mData.clear()
        mData.addAll(list)
        notifyDataSetChanged()
    }

}

Over 。。。

你可能感兴趣的:(安卓轮播图(ViewPager 无限循环滚动))