这篇文章主要介绍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 List<ImageView> mImageList; /** * 当前广告的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 List<ImageView> mImageList; private int[] mImageIds; public LoopViewPagerAdapter2(List<ImageView> 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
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/titleBar" layout="@layout/my_app_bar" /> <android.support.v4.view.ViewPager android:id="@+id/activity_loop_viewpager" android:layout_width="match_parent" android:layout_height="200dp" android:layout_below="@id/titleBar" /> <LinearLayout android:id="@+id/activity_ll_container" android:layout_width="match_parent" android:orientation="horizontal" android:gravity="center" android:background="@color/toast" android:layout_alignBottom="@id/activity_loop_viewpager" android:visibility="gone" android:layout_height="40dp"/> </RelativeLayout>
以上是“Android如何实现ViewPager无限循环效果”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。