這篇文章主要介紹Android如何實(shí)現(xiàn)ViewPager無限循環(huán)效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
在鞍山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需策劃設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)整合營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),鞍山網(wǎng)站建設(shè)費(fèi)用合理。
具體內(nèi)容如下
原理:在Adapter中將getCount設(shè)置為無限大
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 { /** * 更新圖片的時(shí)間 */ private static final int UPDATE_TIME = 3 * 1000; private ViewPager mViewPager; private LoopViewPagerAdapter2 mAdapter; /** * 指示Point */ private LinearLayout mLinearLayoutPonits; /** * 圖片資源集合 */ private int[] mImageIds; /** * 圖片集合 */ private ListmImageList; /** * 當(dāng)前廣告的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如何實(shí)現(xiàn)ViewPager無限循環(huán)效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!