最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果

 更新時(shí)間:2018年07月17日 09:56:07   作者:TomCat0916  
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果,供大家參考,具體內(nèi)容如下

重寫(xiě)LinearLayoutManager,在onAttachedToWindow方法中使用 PagerSnapHelper設(shè)置RecyclerView條目加載方式為每次滾動(dòng)加載一頁(yè)

class MyLinearLayoutManager : LinearLayoutManager {

  private lateinit var mPagerSnapHelper: PagerSnapHelper
  private var mOnViewPagerListener: OnViewPagerListener? = null
  private lateinit var mRecyclerView: RecyclerView
  private var mDrift: Int = 0//位移,用來(lái)判斷移動(dòng)方向

  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)//設(shè)置RecyclerView每次滾動(dòng)一頁(yè)
    mRecyclerView = view
    mRecyclerView.addOnChildAttachStateChangeListener(mChildAttachStateChangeListener)
  }


  /**
   * 滑動(dòng)狀態(tài)的改變
   * 緩慢拖拽-> SCROLL_STATE_DRAGGING
   * 快速滾動(dòng)-> SCROLL_STATE_SETTLING
   * 空閑狀態(tài)-> SCROLL_STATE_IDLE
   * @param state
   */
  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)
      }
    }
  }

  /**
   * 布局完成后調(diào)用
   * @param state
   */
  override fun onLayoutCompleted(state: RecyclerView.State?) {
    super.onLayoutCompleted(state)
    if (mOnViewPagerListener != null) mOnViewPagerListener!!.onLayoutComplete()
  }

  /**
   * 監(jiān)聽(tīng)豎直方向的相對(duì)偏移量
   */
  override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
    this.mDrift = dy
    return super.scrollVerticallyBy(dy, recycler, state)
  }


  /**
   * 監(jiān)聽(tīng)水平方向的相對(duì)偏移量
   */
  override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
    this.mDrift = dx
    return super.scrollHorizontallyBy(dx, recycler, state)
  }

  /**
   * 設(shè)置監(jiān)聽(tīng)
   * @param listener
   */
  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{
    /*釋放的監(jiān)聽(tīng)*/
    fun onPageRelease(isNext: Boolean, position: Int)

    /*選中的監(jiān)聽(tīng)以及判斷是否滑動(dòng)到底部*/
    fun onPageSelected(position: Int, isBottom: Boolean)

    /*布局完成的監(jiān)聽(tīng)*/
    fun onLayoutComplete()
  }
}

重寫(xiě)RecyclerView條目?jī)?nèi)容主布局滿(mǎn)屏填充

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

绥宁县| 库伦旗| 南乐县| 阿鲁科尔沁旗| 通山县| 钟山县| 阿勒泰市| 黑山县| 漳平市| 齐齐哈尔市| 拉孜县| 府谷县| 大理市| 临高县| 永吉县| 镇坪县| 五大连池市| 容城县| 中山市| 阜宁县| 长治县| 盐亭县| 招远市| 贵南县| 铁岭市| 理塘县| 博野县| 牡丹江市| 丁青县| 集安市| 枣庄市| 太谷县| 鞍山市| 聊城市| 香格里拉县| 休宁县| 青岛市| 台江县| 探索| 长子县| 望都县|