Loading

ViewPager结合view无限滑动

 

使用viewPager进无限滑动,这里的实现是在适配器里面进行,当然在外头使用滑动监听也行。 


import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class ReadViewPagerAdapter extends PagerAdapter 
{
private ArrayList<View> viewArrayList; public ReadViewPagerAdapter(ArrayList<View> viewArrayList)
{
this.viewArrayList = viewArrayList; } @Override public int getCount()
{
return Integer.MAX_VALUE;//实现无限循环播放,这里使用一个极端的数字 } @Override public boolean isViewFromObject(View view, Object object)
{
return view == object; } @Override public Object instantiateItem(ViewGroup container, int position)
{
position %= viewArrayList.size(); //防止数组越界 if (position < 0)
{ position
= viewArrayList.size() + position; } View view = viewArrayList.get(position); //如果这个view已经被加入到viewPager中,那么就将它移除,不然可能会出现一个view被重复添加而出现错误
if (view.getParent() != null) { ViewGroup viewGroup = (ViewGroup) view.getParent(); viewGroup.removeView(view); } container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object)
{
// container.removeView(viewArrayList.get(position));//删除页卡,这个地方不能对其进行重写 } }

 以上便实现了对viewPager里面的view进行无限循环切换了

posted @ 2017-03-07 19:51  妖久  阅读(560)  评论(0编辑  收藏  举报