溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

RecyclerView+PagerSnapHelper如何實(shí)現(xiàn)抖音首頁(yè)翻頁(yè)Viewpager效果

發(fā)布時(shí)間:2021-06-24 14:38:01 來(lái)源:億速云 閱讀:228 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要為大家展示了“RecyclerView+PagerSnapHelper如何實(shí)現(xiàn)抖音首頁(yè)翻頁(yè)Viewpager效果”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“RecyclerView+PagerSnapHelper如何實(shí)現(xiàn)抖音首頁(yè)翻頁(yè)Viewpager效果”這篇文章吧。

RecyclerView + PagerSnapHelper 實(shí)現(xiàn)抖音首頁(yè)翻頁(yè)的Viewpager效果,供大家參考,具體內(nèi)容如下

先來(lái)個(gè)效果

RecyclerView+PagerSnapHelper如何實(shí)現(xiàn)抖音首頁(yè)翻頁(yè)Viewpager效果

實(shí)現(xiàn)方式

PagerSnapHelperActivity.java

public class PagerSnapHelperActivity extends Activity {

 /**
 * UI
 */
 // recycleView
 private RecyclerView mRecyclerView = null;
 // adapter
 private PagerSnapHelperAdapter mMyadapter = null;
 /**
 * 數(shù)據(jù)
 */
 //data
 private ArrayList<String> mDataList = new ArrayList<String>();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.recycle_pager_activity);

 // -----------創(chuàng)建數(shù)據(jù)集-------------
 for (int i = 1; i < 100; i++) {
  mDataList.add("item" + i);
 }
 // 縱向List
 initUI();

 }

 public void initUI() {
 // ---RecyclerView---
 mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview_vertical);
 mRecyclerView.setNestedScrollingEnabled(false);
 // PagerSnapHelper
 PagerSnapHelper snapHelper = new PagerSnapHelper() {
  // 在 Adapter的 onBindViewHolder 之后執(zhí)行
  @Override
  public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
  // TODO 找到對(duì)應(yīng)的Index
  Log.e("xiaxl: ", "---findTargetSnapPosition---");
  int targetPos = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
  Log.e("xiaxl: ", "targetPos: " + targetPos);

  Toast.makeText(PagerSnapHelperActivity.this, "滑到到 " + targetPos + "位置", Toast.LENGTH_SHORT).show();

  return targetPos;
  }

  // 在 Adapter的 onBindViewHolder 之后執(zhí)行
  @Nullable
  @Override
  public View findSnapView(RecyclerView.LayoutManager layoutManager) {
  // TODO 找到對(duì)應(yīng)的View
  Log.e("xiaxl: ", "---findSnapView---");
  View view = super.findSnapView(layoutManager);
  Log.e("xiaxl: ", "tag: " + view.getTag());

  return view;
  }
 };
 snapHelper.attachToRecyclerView(mRecyclerView);
 // ---布局管理器---
 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
 // 默認(rèn)是Vertical (HORIZONTAL則為橫向列表)
 linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
 //
 mRecyclerView.setLayoutManager(linearLayoutManager);

 // TODO 這么寫是為了獲取RecycleView的寬高
 mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
   mRecyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  } else {
   mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  }

  /**
   * 這么寫是為了獲取RecycleView的寬高
   */
  // 創(chuàng)建Adapter,并指定數(shù)據(jù)集
  mMyadapter = new PagerSnapHelperAdapter(mDataList, mRecyclerView.getWidth(), mRecyclerView.getHeight());
  // 設(shè)置Adapter
  mRecyclerView.setAdapter(mMyadapter);
  }
 });
 }
}

recycle_pager_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <android.support.v7.widget.RecyclerView
 android:id="@+id/recyclerview_vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:scrollbars="vertical" />

</RelativeLayout>

PagerSnapHelperAdapter.java

public class PagerSnapHelperAdapter extends RecyclerView.Adapter<PagerSnapHelperAdapter.ViewHolder> {

 // 數(shù)據(jù)集
 private ArrayList<String> mDataList;


 private int mWidth;
 private int mHeight;

 //
 public PagerSnapHelperAdapter(ArrayList<String> dataset, int width, int height) {
 super();
 this.mDataList = dataset;
 //
 mWidth = width;
 mHeight = height;
 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
 Log.e("xiaxl: ", "---onCreateViewHolder---");
 // 創(chuàng)建一個(gè)View,簡(jiǎn)單起見直接使用系統(tǒng)提供的布局,就是一個(gè)TextView
 View view = View.inflate(viewGroup.getContext(), R.layout.recycle_pager_item, null);


 View contentView = view.findViewById(R.id.add_btn);
 RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) contentView.getLayoutParams();
 rl.width = mWidth;
 rl.height = mHeight;
 contentView.setLayoutParams(rl);


 // 創(chuàng)建一個(gè)ViewHolder
 ViewHolder holder = new ViewHolder(view);
 return holder;
 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int position) {
 Log.e("xiaxl: ", "---onBindViewHolder---");

 // 綁定數(shù)據(jù)到ViewHolder上
 viewHolder.itemView.setTag(position);
 //
 viewHolder.mTextView.setText(position + " item");
 }

 @Override
 public int getItemCount() {
 return mDataList.size();
 }

 /**
 *
 */
 public static class ViewHolder extends RecyclerView.ViewHolder {

 public TextView mTextView;

 public ViewHolder(View itemView) {
  super(itemView);
  mTextView = (TextView) itemView.findViewById(R.id.add_btn);
 }
 }
}

recycle_pager_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="640dp"
 android:orientation="vertical">


 <TextView
 android:id="@+id/add_btn"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#ff0000"
 android:gravity="center"
 android:text="添加Item"
 android:textSize="36dp" />

</RelativeLayout>

以上是“RecyclerView+PagerSnapHelper如何實(shí)現(xiàn)抖音首頁(yè)翻頁(yè)Viewpager效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI