ViewPager结合view无限滑动

时间:2022-07-09 03:52:42

使用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 < )
{
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进行无限循环切换了