欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿抖音上下滑动布局

程序员文章站 2022-04-20 13:36:10
抖音上下滑动,监听播放,自动吸顶,吸底效果,供大家参考,具体内容如下 使用recyclerview+pagersnaphelper实现 public class douyi...

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

使用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

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