RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果
使用RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果,供大家參考,具體內(nèi)容如下
重寫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//位移,用來判斷移動(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)聽豎直方向的相對(duì)偏移量
*/
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
this.mDrift = dy
return super.scrollVerticallyBy(dy, recycler, state)
}
/**
* 監(jiān)聽水平方向的相對(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)聽
* @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)聽以及判斷是否滑動(dòng)到底部*/
fun onPageSelected(position: Int, isBottom: Boolean)
/*布局完成的監(jiān)聽*/
fun onLayoutComplete()
}
}
重寫RecyclerView條目?jī)?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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- RecyclerView實(shí)現(xiàn)縱向和橫向滾動(dòng)
- Android RecyclerView 滾動(dòng)到中間位置的方法示例
- Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼
- Android使用Recyclerview實(shí)現(xiàn)圖片水平自動(dòng)循環(huán)滾動(dòng)效果
- XRecyclerView實(shí)現(xiàn)下拉刷新、滾動(dòng)到底部加載更多等功能
- Android_RecyclerView實(shí)現(xiàn)上下滾動(dòng)廣告條實(shí)例(帶圖片)
- Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的方法詳解
- Android使用RecyclerView實(shí)現(xiàn)水平滾動(dòng)控件
- Android代碼實(shí)現(xiàn)AdapterViews和RecyclerView無限滾動(dòng)
- RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果
相關(guān)文章
android實(shí)現(xiàn)簡(jiǎn)單儀表盤效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單儀表盤效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
解析:ClickOnce通過URL傳遞參數(shù) XXX.application?a=1
本篇文章是對(duì)ClickOnce通過URL傳遞參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android字體相關(guān)知識(shí)總結(jié)
最近接到一個(gè)需求,大致內(nèi)容是:全局替換當(dāng)前項(xiàng)目中的默認(rèn)字體,并引入 UI 設(shè)計(jì)師提供的一些新字體。于是對(duì)字體做了些研究,把自己的一些心得分享給大家。注意:本文所展示的系統(tǒng)源碼都是基于Android-30 ,并提取核心部分進(jìn)行分析2021-06-06
Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例
本篇文章主要介紹了Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
Android啟動(dòng)內(nèi)置APK和動(dòng)態(tài)發(fā)送接收自定義廣播實(shí)例詳解
這篇文章主要介紹了Android啟動(dòng)內(nèi)置APK和動(dòng)態(tài)發(fā)送接收自定義廣播實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06

