Android仿抖音上下滑动布局

这篇文章主要为大家详细介绍了Android仿抖音上下滑动布局,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

抖音上下滑动,监听播放,自动吸顶,吸底效果,供大家参考,具体内容如下

使用RecyclerView+PagerSnapHelper实现


public class DouYinLayoutManager extends LinearLayoutManager implements RecyclerView.OnChildAttachStateChangeListener{
  //判断是否上滑还是下滑
  private int mDrift;
  private OnViewPagerListener onViewPagerListener;
  //吸顶,吸底
  private PagerSnapHelper pagerSnapHelper;

  public OnViewPagerListener getOnViewPagerListener() {
    return onViewPagerListener;
  }

  public void setOnViewPagerListener(OnViewPagerListener onViewPagerListener) {
    this.onViewPagerListener = onViewPagerListener;
  }

  public DouYinLayoutManager(Context context) {
    super(context);
  }

  public DouYinLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
    pagerSnapHelper = new PagerSnapHelper();
  }

  /**
   * 当manager完全添加到recycleview中是会被调用
   * @param view
   */
  @Override
  public void onAttachedToWindow(RecyclerView view) {
    view.addOnChildAttachStateChangeListener(this);
    pagerSnapHelper.attachToRecyclerView(view);
    super.onAttachedToWindow(view);
  }

  @Override
  public boolean canScrollVertically() {
    return super.canScrollVertically();
  }

  @Override
  public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
    mDrift = dy;
    return super.scrollVerticallyBy(dy, recycler, state);

  }

  @Override
  public void onChildViewAttachedToWindow(@NonNull View view) {
    if (mDrift > 0){
      //向上滑
      if (onViewPagerListener != null && Math.abs(mDrift) == view.getHeight()){
        onViewPagerListener.onPageSelected(false,view);
      }
    }else {
      //向下滑
      if (onViewPagerListener != null && Math.abs(mDrift) == view.getHeight()){
        onViewPagerListener.onPageSelected(true,view);
      }
    }
  }

  @Override
  public void onChildViewDetachedFromWindow(@NonNull View view) {
    if (mDrift >= 0){
      //向上滑
      if (onViewPagerListener != null){
        onViewPagerListener.onPageRelease(true,view);
      }
    }else {
      //向下滑
      if (onViewPagerListener != null){
        onViewPagerListener.onPageRelease(false,view);
      }
    }
  }

  @Override
  public void onScrollStateChanged(int state) {
    switch (state){
      case RecyclerView.SCROLL_STATE_IDLE:
        //当前显示的item
        View snapView = pagerSnapHelper.findSnapView(this);
        if (onViewPagerListener != null){
          onViewPagerListener.onPageSelected(false,snapView);
        }
        break;
    }
    super.onScrollStateChanged(state);

  }
}

recyclerView.setLayoutManager()即可


recyclerView = findViewById(R.id.recyclerView_dy);
    douYinLayoutManager = new DouYinLayoutManager(this, OrientationHelper.VERTICAL,false);
    recyclerView.setLayoutManager(douYinLayoutManager);
    recyclerView.setAdapter(new MyAdapter());

    douYinLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {
      @Override
      public void onPageRelease(boolean isNest, View position) {
        releaseVideo(position);
      }

      @Override
      public void onPageSelected(boolean isButten, View position) {
        playVideo(position);
      }
    });
  }

代码:MyUiDemo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐