欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android仿抖音上下滑动布局

时间:2020-10-07 14:29:08|栏目: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

上一篇:Android中实现下载和解压zip文件功能代码分享

栏    目:Android代码

下一篇:Android添加glide库报错Error: Failed to resolve: com.android.support:support-annotations:26.0.2的解决

本文标题:Android仿抖音上下滑动布局

本文地址:http://www.codeinn.net/misctech/8261.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有