Android实现ViewPager无限循环效果(二)
时间:2020-12-17 02:24:08|栏目:Android代码|点击: 次
本文实例为大家分享了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>
上一篇:Popupwindow 的简单实用案例(显示在控件下方)
栏 目:Android代码
本文标题:Android实现ViewPager无限循环效果(二)
本文地址:http://www.codeinn.net/misctech/33719.html