栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

RecyclerView实现抖音纵向滚动ViewPager效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RecyclerView实现抖音纵向滚动ViewPager效果

使用RecyclerView实现抖音纵向滚动ViewPager效果,供大家参考,具体内容如下

重写LinearLayoutManager,在onAttachedToWindow方法中使用 PagerSnapHelper设置RecyclerView条目加载方式为每次滚动加载一页

class MyLinearLayoutManager : LinearLayoutManager {

  private lateinit var mPagerSnapHelper: PagerSnapHelper
  private var mOnViewPagerListener: OnViewPagerListener? = null
  private lateinit var mRecyclerView: RecyclerView
  private var mDrift: Int = 0//位移,用来判断移动方向

  constructor(context: Context) : this(context, OrientationHelper.VERTICAL)
  constructor(context: Context, orientation: Int) : this(context, orientation, false)
  constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout) {
    mPagerSnapHelper = PagerSnapHelper()
  }

  override fun onAttachedToWindow(view: RecyclerView) {
    super.onAttachedToWindow(view)
    mPagerSnapHelper.attachToRecyclerView(view)//设置RecyclerView每次滚动一页
    mRecyclerView = view
    mRecyclerView.addonChildAttachStateChangeListener(mChildAttachStateChangeListener)
  }


  
  override fun onScrollStateChanged(state: Int) {
    if (state == RecyclerView.SCROLL_STATE_IDLE){
      val viewIdle = mPagerSnapHelper.findSnapView(this)
      val positionIdle = getPosition(viewIdle!!)
      if (monViewPagerListener != null && childCount == 1) {
 mOnViewPagerListener!!.onPageSelected(positionIdle, positionIdle == itemCount - 1)
      }
    }
  }

  
  override fun onLayoutCompleted(state: RecyclerView.State?) {
    super.onLayoutCompleted(state)
    if (monViewPagerListener != null) mOnViewPagerListener!!.onLayoutComplete()
  }

  
  override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
    this.mDrift = dy
    return super.scrollVerticallyBy(dy, recycler, state)
  }


  
  override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
    this.mDrift = dx
    return super.scrollHorizontallyBy(dx, recycler, state)
  }

  
  fun setonViewPagerListener(listener: OnViewPagerListener) {
    this.monViewPagerListener = listener
  }

  private val mChildAttachStateChangeListener = object : RecyclerView.onChildAttachStateChangeListener {
    override fun onChildViewAttachedToWindow(view: View) {
    }

    override fun onChildViewDetachedFromWindow(view: View) {
      if (mDrift >= 0) {
 if (monViewPagerListener != null) mOnViewPagerListener!!.onPageRelease(true, getPosition(view))
      } else {
 if (monViewPagerListener != null) mOnViewPagerListener!!.onPageRelease(false, getPosition(view))
      }

    }
  }

  interface OnViewPagerListener{
    
    fun onPageRelease(isNext: Boolean, position: Int)

    
    fun onPageSelected(position: Int, isBottom: Boolean)

    
    fun onLayoutComplete()
  }
}

重写RecyclerView条目内容主布局满屏填充

class MyImageView : ImageView {
  constructor(context: Context) : this(context, null!!)
  constructor(context: Context, attr: AttributeSet) : this(context, attr, 0)
  constructor(context: Context, attr: AttributeSet, defStyleAttr: Int) : super(context, attr, defStyleAttr)

  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    val width = View.getDefaultSize(0, widthMeasureSpec)
    val height = View.getDefaultSize(0, heightMeasureSpec)
    setMeasuredDimension(width, height)
  }

}

代码参考:LayoutManagerGroup

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/155851.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号