这篇文章主要介绍Android如何实现ViewPager无限循环效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
在鞍山等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、网站建设 网站设计制作按需策划设计,公司网站建设,企业网站建设,高端网站设计,全网整合营销推广,外贸营销网站建设,鞍山网站建设费用合理。
具体内容如下
原理:在Adapter中将getCount设置为无限大
package com.xiaomai.myproject.demo; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import com.xiaomai.myproject.R; import com.xiaomai.myproject.adapter.LoopViewPagerAdapter2; import com.xiaomai.myproject.base.BaseActivity; import java.util.ArrayList; import java.util.List; /** * Created by XiaoMai on 2016/10/9 17:44. * */ public class LoopViewPagerDemo2Activity extends BaseActivity { /** * 更新图片的时间 */ private static final int UPDATE_TIME = 3 * 1000; private ViewPager mViewPager; private LoopViewPagerAdapter2 mAdapter; /** * 指示Point */ private LinearLayout mLinearLayoutPonits; /** * 图片资源集合 */ private int[] mImageIds; /** * 图片集合 */ private ListmImageList; /** * 当前广告的index */ private int mCurrentPageIndex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected void initVariables() { super.initVariables(); /** * 初始化图片的资源id */ mImageIds = new int[]{ R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.drawable.my_toast, R.drawable.my_toast, R.drawable.my_toast, R.drawable.my_toast}; /** * 添加ImageView */ mImageList = new ArrayList<>(); //额外添加两张图片 ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); for (int i = 0; i < mImageIds.length; i++) { ImageView imageView = new ImageView(mContext); imageView.setLayoutParams(layoutParams); mImageList.add(imageView); } } @Override protected void initViews() { mLinearLayoutPonits = (LinearLayout) findViewById(R.id.activity_ll_container); mViewPager = (ViewPager) findViewById(R.id.activity_loop_viewpager); mAdapter = new LoopViewPagerAdapter2(mImageList, mImageIds); mViewPager.setAdapter(mAdapter); int middle = mAdapter.getCount() / 2; mViewPager.setCurrentItem(middle - middle % mImageList.size()); } @Override protected int getContentLayout() { return R.layout.act_loopviewpager; } }
代码:
package com.xiaomai.myproject.adapter; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import java.util.List; /** * Created by XiaoMai on 2016/10/9 17:53. */ public class LoopViewPagerAdapter2 extends PagerAdapter { private ListmImageList; private int[] mImageIds; public LoopViewPagerAdapter2(List mImageList, int[] mImageIds) { this.mImageList = mImageList; this.mImageIds = mImageIds; } @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) { ImageView imageView = mImageList.get(position % mImageList.size()); imageView.setImageResource(mImageIds[position % mImageIds.length]); container.addView(imageView); return mImageList.get(position % mImageList.size()); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mImageList.get(position % mImageList.size())); } }
act_loopviewpager.xml
以上是“Android如何实现ViewPager无限循环效果”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!