91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android ViewPager怎么去實現無限循環滾動回繞效果

發布時間:2020-06-23 15:16:29 來源:億速云 閱讀:551 作者:清晨 欄目:移動開發

不懂Android ViewPager怎么去實現無限循環滾動回繞效果?其實想解決這個問題也不難,下面讓小編帶著大家一起學習怎么去解決,希望大家閱讀完這篇文章后大所收獲。

android系統提供的ViewPager標準方式是左右可以自由滑動,但是滑動到最左邊的極限位置是第一個page,滑動到最右邊的位置是最后一個page,當滑動到最左或者最右時候,就不能再滑動/滾動了,這是Android系統默認的ViewPager實現方式。 

但是有些情況下開發者可能希望ViewPager能夠智能的無限循環滾動回繞,比如現在總共有編號1, 2, 3, 4, 5的5個Page。 

(1)當用戶手指從右往左滾動到最右邊/最后面的頁面5時候,如果此時用戶繼續拖住ViewPager往左邊滑動,那么ViewPager將回繞、循環到第一個Page -> 1,接著就是2,3,4,5;

(2)反過來,如果當用戶手指從左往右,滑到最左邊的第一個Page:1時候,如果此時繼續拖住ViewPager繼續從左往右滑動,那么將回繞到5,接著就是4,3,2,1.

我們把這種ViewPager稱之謂“無限循環滾動回繞”的ViewPager。 

這種類型的ViewPager網上有較多實現方式,現在給出一個流程較廣的代碼實現。 

寫一個測試的MainActivity.Java:

package zhangphil.demo;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 LoopViewPager viewpager = (LoopViewPager) findViewById(R.id.viewpager);
 viewpager.setAdapter(new SamplePagerAdapter());
 }

 public class SamplePagerAdapter extends PagerAdapter {
 private final Random random = new Random();
 private int mSize;

 public SamplePagerAdapter() {
 mSize = 5;
 }

 public SamplePagerAdapter(int count) {
 mSize = count;
 }

 @Override
 public int getCount() {
 return mSize;
 }

 @Override
 public boolean isViewFromObject(View view, Object object) {
 return view == object;
 }

 @Override
 public void destroyItem(ViewGroup view, int position, Object object) {
 view.removeView((View) object);
 }

 @Override
 public Object instantiateItem(ViewGroup view, int position) {
 TextView textView = new TextView(view.getContext());

 textView.setText(position + 1 + "");
 textView.setBackgroundColor(0xff000000 | random.nextInt(0x00ffffff));
 textView.setGravity(Gravity.CENTER);
 textView.setTextColor(Color.WHITE);
 textView.setTextSize(50);
 view.addView(textView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

 return textView;
 }

 // 增加item
 public void addItem() {
 mSize++;
 notifyDataSetChanged();
 }

 // 刪除item
 public void removeItem() {
 mSize--;
 mSize = mSize < 0 &#63; 0 : mSize;

 notifyDataSetChanged();
 }
 }
}

MainActivity.java需要的布局文件activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="zhangphil.demo.MainActivity" >

 <zhangphil.demo.LoopViewPager
  android:id="@+id/viewpager"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>
</RelativeLayout> 

核心關鍵代碼LoopViewPager.java類和LoopViewPager.java依賴的LoopPagerAdapterWrapper.java! 

LoopViewPager.java:

package zhangphil.demo;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.List;

public class LoopViewPager extends ViewPager {
 private static final boolean DEFAULT_BOUNDARY_CASHING = false;
 private static final boolean DEFAULT_BOUNDARY_LOOPING = true;

 private LoopPagerAdapterWrapper mAdapter;
 private boolean mBoundaryCaching = DEFAULT_BOUNDARY_CASHING;
 private boolean mBoundaryLooping = DEFAULT_BOUNDARY_LOOPING;
 private List<OnPageChangeListener> mOnPageChangeListeners;

 /**
 * helper function which may be used when implementing FragmentPagerAdapter
 *
 * @return (position-1)%count
 */
 public static int toRealPosition(int position, int count) {
 position = position - 1;
 if (position < 0) {
 position += count;
 } else {
 position = position % count;
 }
 return position;
 }

 /**
 * If set to true, the boundary views (i.e. first and last) will never be
 * destroyed This may help to prevent "blinking" of some views
 */
 public void setBoundaryCaching(boolean flag) {
 mBoundaryCaching = flag;
 if (mAdapter != null) {
 mAdapter.setBoundaryCaching(flag);
 }
 }

 public void setBoundaryLooping(boolean flag) {
 mBoundaryLooping = flag;
 if (mAdapter != null) {
 mAdapter.setBoundaryLooping(flag);
 }
 }

 @Override
 public void setAdapter(PagerAdapter adapter) {
 mAdapter = new LoopPagerAdapterWrapper(adapter);
 mAdapter.setBoundaryCaching(mBoundaryCaching);
 mAdapter.setBoundaryLooping(mBoundaryLooping);
 super.setAdapter(mAdapter);
 setCurrentItem(0, false);
 }

 @Override
 public PagerAdapter getAdapter() {
 return mAdapter != null &#63; mAdapter.getRealAdapter() : mAdapter;
 }

 @Override
 public int getCurrentItem() {
 return mAdapter != null &#63; mAdapter.toRealPosition(super.getCurrentItem()) : 0;
 }

 public void setCurrentItem(int item, boolean smoothScroll) {
 int realItem = mAdapter.toInnerPosition(item);
 super.setCurrentItem(realItem, smoothScroll);
 }

 @Override
 public void setCurrentItem(int item) {
 if (getCurrentItem() != item) {
 setCurrentItem(item, true);
 }
 }

 @Override
 public void setOnPageChangeListener(OnPageChangeListener listener) {
 addOnPageChangeListener(listener);
 }

 @Override
 public void addOnPageChangeListener(OnPageChangeListener listener) {
 if (mOnPageChangeListeners == null) {
 mOnPageChangeListeners = new ArrayList<>();
 }
 mOnPageChangeListeners.add(listener);
 }

 @Override
 public void removeOnPageChangeListener(OnPageChangeListener listener) {
 if (mOnPageChangeListeners != null) {
 mOnPageChangeListeners.remove(listener);
 }
 }

 @Override
 public void clearOnPageChangeListeners() {
 if (mOnPageChangeListeners != null) {
 mOnPageChangeListeners.clear();
 }
 }

 public LoopViewPager(Context context) {
 super(context);
 init(context);
 }

 public LoopViewPager(Context context, AttributeSet attrs) {
 super(context, attrs);
 init(context);
 }

 private void init(Context context) {
 if (onPageChangeListener != null) {
 super.removeOnPageChangeListener(onPageChangeListener);
 }
 super.addOnPageChangeListener(onPageChangeListener);
 }

 private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
 private float mPreviousOffset = -1;
 private float mPreviousPosition = -1;

 @Override
 public void onPageSelected(int position) {

 int realPosition = mAdapter.toRealPosition(position);
 if (mPreviousPosition != realPosition) {
 mPreviousPosition = realPosition;

 if (mOnPageChangeListeners != null) {
  for (int i = 0; i < mOnPageChangeListeners.size(); i++) {
  OnPageChangeListener listener = mOnPageChangeListeners.get(i);
  if (listener != null) {
  listener.onPageSelected(realPosition);
  }
  }
 }
 }
 }

 @Override
 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
 int realPosition = position;
 if (mAdapter != null) {
 realPosition = mAdapter.toRealPosition(position);

 if (positionOffset == 0 && mPreviousOffset == 0
  && (position == 0 || position == mAdapter.getCount() - 1)) {
  setCurrentItem(realPosition, false);
 }
 }

 mPreviousOffset = positionOffset;

 if (mOnPageChangeListeners != null) {
 for (int i = 0; i < mOnPageChangeListeners.size(); i++) {
  OnPageChangeListener listener = mOnPageChangeListeners.get(i);
  if (listener != null) {
  if (realPosition != mAdapter.getRealCount() - 1) {
  listener.onPageScrolled(realPosition, positionOffset, positionOffsetPixels);
  } else {
  if (positionOffset > .5) {
  listener.onPageScrolled(0, 0, 0);
  } else {
  listener.onPageScrolled(realPosition, 0, 0);
  }
  }
  }
 }
 }
 }

 @Override
 public void onPageScrollStateChanged(int state) {
 if (mAdapter != null) {
 int position = LoopViewPager.super.getCurrentItem();
 int realPosition = mAdapter.toRealPosition(position);
 if (state == ViewPager.SCROLL_STATE_IDLE && (position == 0 || position == mAdapter.getCount() - 1)) {
  setCurrentItem(realPosition, false);
 }
 }

 if (mOnPageChangeListeners != null) {
 for (int i = 0; i < mOnPageChangeListeners.size(); i++) {
  OnPageChangeListener listener = mOnPageChangeListeners.get(i);
  if (listener != null) {
  listener.onPageScrollStateChanged(state);
  }
 }
 }
 }
 };
}

LoopPagerAdapterWrapper.java:

package zhangphil.demo;

import android.os.Parcelable;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

public class LoopPagerAdapterWrapper extends PagerAdapter {

 private PagerAdapter mAdapter;

 private SparseArray<ToDestroy> mToDestroy = new SparseArray<>();

 private static final boolean DEFAULT_BOUNDARY_CASHING = true;
 private static final boolean DEFAULT_BOUNDARY_LOOPING = true;

 private boolean mBoundaryCaching = DEFAULT_BOUNDARY_CASHING;
 private boolean mBoundaryLooping = DEFAULT_BOUNDARY_LOOPING;

 void setBoundaryCaching(boolean flag) {
 mBoundaryCaching = flag;
 }

 void setBoundaryLooping(boolean flag) {
 mBoundaryLooping = flag;
 }

 LoopPagerAdapterWrapper(PagerAdapter adapter) {
 this.mAdapter = adapter;
 }

 @Override
 public void notifyDataSetChanged() {
 mToDestroy = new SparseArray<>();
 super.notifyDataSetChanged();
 }

 int toRealPosition(int position) {
 int realPosition = position;
 int realCount = getRealCount();
 if (realCount == 0)
 return 0;
 if (mBoundaryLooping) {
 realPosition = (position - 1) % realCount;
 if (realPosition < 0)
 realPosition += realCount;
 }

 return realPosition;
 }

 public int toInnerPosition(int realPosition) {
 int position = (realPosition + 1);
 return mBoundaryLooping &#63; position : realPosition;
 }

 private int getRealFirstPosition() {
 return mBoundaryLooping &#63; 1 : 0;
 }

 private int getRealLastPosition() {
 return getRealFirstPosition() + getRealCount() - 1;
 }

 @Override
 public int getCount() {
 int count = getRealCount();
 return mBoundaryLooping &#63; count + 2 : count;
 }

 public int getRealCount() {
 return mAdapter.getCount();
 }

 public PagerAdapter getRealAdapter() {
 return mAdapter;
 }

 @Override
 public Object instantiateItem(ViewGroup container, int position) {
 int realPosition = (mAdapter instanceof FragmentPagerAdapter || mAdapter instanceof FragmentStatePagerAdapter)
 &#63; position : toRealPosition(position);

 if (mBoundaryCaching) {
 ToDestroy toDestroy = mToDestroy.get(position);
 if (toDestroy != null) {
 mToDestroy.remove(position);
 return toDestroy.object;
 }
 }
 return mAdapter.instantiateItem(container, realPosition);
 }

 @Override
 public void destroyItem(ViewGroup container, int position, Object object) {
 int realFirst = getRealFirstPosition();
 int realLast = getRealLastPosition();
 int realPosition = (mAdapter instanceof FragmentPagerAdapter || mAdapter instanceof FragmentStatePagerAdapter)
 &#63; position : toRealPosition(position);

 if (mBoundaryCaching && (position == realFirst || position == realLast)) {
 mToDestroy.put(position, new ToDestroy(container, realPosition, object));
 } else {
 mAdapter.destroyItem(container, realPosition, object);
 }
 }

 /*
 * Delegate rest of methods directly to the inner adapter.
 */

 @Override
 public void finishUpdate(ViewGroup container) {
 mAdapter.finishUpdate(container);
 }

 @Override
 public boolean isViewFromObject(View view, Object object) {
 return mAdapter.isViewFromObject(view, object);
 }

 @Override
 public void restoreState(Parcelable bundle, ClassLoader classLoader) {
 mAdapter.restoreState(bundle, classLoader);
 }

 @Override
 public Parcelable saveState() {
 return mAdapter.saveState();
 }

 @Override
 public void startUpdate(ViewGroup container) {
 mAdapter.startUpdate(container);
 }

 @Override
 public void setPrimaryItem(ViewGroup container, int position, Object object) {
 mAdapter.setPrimaryItem(container, position, object);
 }

 /*
 * End delegation
 */

 /**
 * Container class for caching the boundary views
 */
 static class ToDestroy {
 ViewGroup container;
 int position;
 Object object;

 public ToDestroy(ViewGroup container, int position, Object object) {
 this.container = container;
 this.position = position;
 this.object = object;
 }
 }
}

如果讀者有興趣使用,直接將上面兩個核心關鍵代碼放入到自己的項目代碼包中,當作自己寫的類直接使用即可,寫布局時候不要再寫ViewPager,而是直接像我上面寫的那個布局文件一樣用LoopViewPager。

感謝你能夠認真閱讀完這篇文章,希望小編分享Android ViewPager怎么去實現無限循環滾動回繞效果內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

博客| 正安县| 菏泽市| 杨浦区| 新邵县| 石柱| 西藏| 开江县| 萨迦县| 富民县| 恩施市| 镇江市| 肇东市| 石家庄市| 星座| 灵山县| 方山县| 濮阳县| 扎赉特旗| 榆林市| 昌邑市| 滁州市| 且末县| 凌源市| 荥阳市| 岗巴县| 荔波县| 赞皇县| 邯郸县| 凌海市| 黎城县| 绥棱县| 海城市| 平定县| 康保县| 枣强县| 健康| 恩平市| 仪征市| 永和县| 玉林市|