返回顶部
首页 > 资讯 > 移动开发 >Android仿抖音上下滑动布局
  • 210
分享到

Android仿抖音上下滑动布局

布局抖音Android 2022-06-06 11:06:50 210人浏览 独家记忆
摘要

抖音上下滑动,监听播放,自动吸顶,吸底效果,供大家参考,具体内容如下 使用RecyclerView+PagerSnapHelper实现 public class DouYin

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

使用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();
  }
  
  @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运用onTouchEvent自定义滑动布局Android开发中RecyclerView模仿探探左右滑动布局功能android实现上下左右滑动界面布局


--结束END--

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

本文链接: https://lsjlt.com/news/28450.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作