RecyclerView實現(xiàn)抖音縱向滾動ViewPager效果
更新時間:2018年07月17日 09:56:07 作者:TomCat0916
這篇文章主要為大家詳細介紹了RecyclerView實現(xiàn)抖音縱向滾動ViewPager效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
使用RecyclerView實現(xiàn)抖音縱向滾動ViewPager效果,供大家參考,具體內(nèi)容如下
重寫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)
}
/**
* 滑動狀態(tài)的改變
* 緩慢拖拽-> SCROLL_STATE_DRAGGING
* 快速滾動-> 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)
}
}
}
/**
* 布局完成后調用
* @param state
*/
override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state)
if (mOnViewPagerListener != null) mOnViewPagerListener!!.onLayoutComplete()
}
/**
* 監(jiān)聽豎直方向的相對偏移量
*/
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
this.mDrift = dy
return super.scrollVerticallyBy(dy, recycler, state)
}
/**
* 監(jiān)聽水平方向的相對偏移量
*/
override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
this.mDrift = dx
return super.scrollHorizontallyBy(dx, recycler, state)
}
/**
* 設置監(jiān)聽
* @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)聽*/
fun onPageRelease(isNext: Boolean, position: Int)
/*選中的監(jiān)聽以及判斷是否滑動到底部*/
fun onPageSelected(position: Int, isBottom: Boolean)
/*布局完成的監(jiān)聽*/
fun onLayoutComplete()
}
}
重寫RecyclerView條目內(nèi)容主布局滿屏填充
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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- RecyclerView實現(xiàn)縱向和橫向滾動
- Android RecyclerView 滾動到中間位置的方法示例
- Android RecyclerView 實現(xiàn)快速滾動的示例代碼
- Android使用Recyclerview實現(xiàn)圖片水平自動循環(huán)滾動效果
- XRecyclerView實現(xiàn)下拉刷新、滾動到底部加載更多等功能
- Android_RecyclerView實現(xiàn)上下滾動廣告條實例(帶圖片)
- Android中RecyclerView實現(xiàn)分頁滾動的方法詳解
- Android使用RecyclerView實現(xiàn)水平滾動控件
- Android代碼實現(xiàn)AdapterViews和RecyclerView無限滾動
- RecyclerView實現(xiàn)橫向滾動效果
相關文章
解析:ClickOnce通過URL傳遞參數(shù) XXX.application?a=1
本篇文章是對ClickOnce通過URL傳遞參數(shù)進行了詳細的分析介紹,需要的朋友參考下2013-06-06
Android啟動內(nèi)置APK和動態(tài)發(fā)送接收自定義廣播實例詳解
這篇文章主要介紹了Android啟動內(nèi)置APK和動態(tài)發(fā)送接收自定義廣播實例詳解的相關資料,需要的朋友可以參考下2017-06-06

