您好,登錄后才能下訂單哦!
本文實例為大家分享了Android實現ViewPager無限循環的具體代碼,供大家參考,具體內容如下
方式一:
實現原理:
假設有3張圖片,分別是1,2,3,那么就創建5張圖片,這5張圖片的順序為:3,1,2,3,1,其中1,2,3為我們要實現滑動的圖片,最左面的3和最右面的1是我們另外添加的圖片,開始時,顯示圖片1,當圖片向左滑動依次為1,2,3,當從第3張圖片繼續向左滑動,會出現我們多添加的圖片1,這時,將當前的index設置為真正的圖片1所在的位置。
package com.xiaomai.myproject.demo; import android.os.Bundle; import android.os.Handler; import android.support.v4.view.ViewPager; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import com.xiaomai.myproject.R; import com.xiaomai.myproject.adapter.LoopViewPagerAdapter; import com.xiaomai.myproject.base.BaseActivity; import com.xiaomai.myproject.utils.Utils; import java.util.ArrayList; import java.util.List; /** * Created by XiaoMai on 2016/10/7 17:19. * */ public class LoopViewPagerDemoActivity extends BaseActivity { /** * 更新圖片的時間 */ private static final int UPDATE_TIME = 3 * 1000; private ViewPager mViewPager; private LoopViewPagerAdapter mLoopViewPager; /** * 圖片資源集合 */ private int[] mImageIds; /** * 圖片集合 */ private List<ImageView> mImageList; /** * 當前廣告的index */ private int mCurrentPageIndex; /** * 指示Point */ private LinearLayout mLinearLayoutPoints; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected void onResume() { super.onResume(); mHandler.postDelayed(myRunnable, UPDATE_TIME); } @Override protected void onPause() { super.onPause(); mHandler.removeCallbacks(myRunnable); } Runnable myRunnable = new Runnable() { @Override public void run() { mViewPager.setCurrentItem(mCurrentPageIndex + 1); } }; @Override protected void initVariables() { super.initVariables(); } @Override protected void initViews() { mLinearLayoutPoints = (LinearLayout) findViewById(R.id.activity_ll_container); mViewPager = (ViewPager) findViewById(R.id.activity_loop_viewpager); } @Override protected void loadData() { super.loadData(); mHandler.postDelayed(new Runnable() { @Override public void run() { /** * 初始化圖片的資源id */ mImageIds = new int[]{ R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.drawable.my_toast, R.drawable.my_toast}; /** * 添加ImageView */ mImageList = new ArrayList<>(); //額外添加兩張圖片 int pagerCount = mImageIds.length + 2; ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); for (int i = 0; i < pagerCount; i++) { ImageView imageView = new ImageView(mContext); imageView.setLayoutParams(layoutParams); mImageList.add(imageView); } //圓點的直徑 int diameter = Utils.dip2px(mContext,10f); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(diameter, diameter); int margin = Utils.dip2px(mContext,5f); params.setMargins(margin,margin,margin,margin); mLinearLayoutPoints.removeAllViews(); for (int i = 0; i < mImageList.size(); i++) { View view = new View(mContext); view.setLayoutParams(params); if (i != 0 && i != mImageList.size() - 1) { view.setBackgroundResource(R.drawable.circle_normal); } mLinearLayoutPoints.addView(view); } mLinearLayoutPoints.setVisibility(View.VISIBLE); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mHandler.removeCallbacks(myRunnable); mHandler.postDelayed(myRunnable, UPDATE_TIME); mCurrentPageIndex = position; if (position == 0) { // 當視圖在第一個時,將頁面號設置為圖片的最后一張。 mCurrentPageIndex = mImageIds.length; } else if (position == mImageIds.length + 1) { // 當視圖在最后一個時,將頁面號設置為圖片的第一張。 mCurrentPageIndex = 1; } else { } /** * 當視圖在第一個或者最后一個時,pageIndex和position的值不相等, * 所以要改變viewPager的當前item。 */ if (position != mCurrentPageIndex) { /** * 第二參數必須設置為false,意思是立即過渡,不給用戶視覺上的效果。 */ mViewPager.setCurrentItem(mCurrentPageIndex, false); return; } for (int i = 1; i < mLinearLayoutPoints.getChildCount() - 1; i++) { if (i != mCurrentPageIndex){ mLinearLayoutPoints.getChildAt(i).setBackgroundResource(R.drawable.circle_normal); }else { mLinearLayoutPoints.getChildAt(mCurrentPageIndex).setBackgroundResource( R.drawable.circle_select); } } } @Override public void onPageScrollStateChanged(int state) { } }); /** * 當ViewPager被按下的時候,取消自動滾動 * 反之開啟自動滑動。 */ mViewPager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ mHandler.removeCallbacks(myRunnable); }else if (event.getAction() == MotionEvent.ACTION_UP){ mHandler.postDelayed(myRunnable, UPDATE_TIME); } return false; } }); mLoopViewPager = new LoopViewPagerAdapter(mImageList, mImageIds); mViewPager.setAdapter(mLoopViewPager); /** * 因為第0個item是另外添加的那張圖片,所以初始化時要把當前item設置為1。 */ mViewPager.setCurrentItem(1); dissMissProgressDialog(); } }, 3 * 1000); } @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/7 17:23. */ public class LoopViewPagerAdapter extends PagerAdapter { private List<ImageView> mImageList; private int[] mImageIds; public LoopViewPagerAdapter(List<ImageView> mImageList, int[] mImageIds) { this.mImageList = mImageList; this.mImageIds = mImageIds; } @Override public int getCount() { return mImageList == null ? 0 : mImageList.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { if (position == 0) { // 如果是第0個item就設置它的圖片內容為最后一個圖片的內容 mImageList.get(position).setImageResource(mImageIds[mImageIds.length-1]); } else if (position == (mImageList.size() - 1)) { // 如果是最后一個item就設置它的圖片內容為第一個圖片的內容 mImageList.get(position).setImageResource(mImageIds[0]); } else { // 這是正常的圖片 mImageList.get(position).setImageResource(mImageIds[position - 1]); } container.addView(mImageList.get(position)); return mImageList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mImageList.get(position)); } }
布局文件
<?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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。