android短視訊開發,不設定滑動限制,可一直往下滑

zhibo系統開發發表於2021-12-21

android短視訊開發,不設定滑動限制,可一直往下滑實現的相關程式碼

 companion object {
        val TAG = "Main"
    }
 
 
    private val adapter by lazy {
        object : BaseQuickAdapter<Int, BaseViewHolder>(R.layout.item_layout) {
            override fun convert(helper: BaseViewHolder?, item: Int?) {
                helper?.itemView?.apply {
                    textView.text = "hello world$item"
                }
            }
 
            override fun getItemCount(): Int {
                return Int.MAX_VALUE
            }
 
            override fun getItem(position: Int): Int? {
                return data[position % data.size]
            }
 
            override fun getItemViewType(position: Int): Int {
                var count = headerLayoutCount + data.size
                if (count <= 0) {
                    count = 1
                }
                return super.getItemViewType(position % count)
            }
        }
    }
 
    var handler = Handler()
    var currentInt = 0
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camrea)
 
        recyclerView.layoutManager = linearLayoutManager
        recyclerView.setHasFixedSize(true)
 
        recyclerView.adapter = adapter
        adapter.setNewData(
            listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        )
 
        //PagerSnapHelper().attachToRecyclerView(recyclerView)
 
        play()
 
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                play()
            }
 
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                
                /*if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    play()
                }*/
                
            }
        })
        
        /*var animator=ValueAnimator.ofInt(0,recyclerView.height)
        animator.duration=2000
        animator.addUpdateListener { 
            var a=it.animatedValue as Int
            recyclerView.translationY=-a.toFloat()
            recyclerView.requestLayout()
        }
        animator.repeatMode=ValueAnimator.REVERSE
        animator.repeatCount=ValueAnimator.INFINITE
        animator.start()*/
 
    }
 
    private fun play() {
        currentInt++
        recyclerView.smoothScrollToPosition(currentInt)
        /*handler.postDelayed({
            currentInt++
            //recyclerView.scrollToPosition(currentInt)
            recyclerView.smoothScrollToPosition(currentInt)
//            val layoutManager = recyclerView.layoutManager as LinearLayoutManager
//            layoutManager.scrollToPositionWithOffset(currentInt, 0)
        }, 2000)*/
    }
    var linearLayoutManager: LinearLayoutManager = object : LinearLayoutManager(this) {
        override fun smoothScrollToPosition(
            recyclerView: RecyclerView,
            state: RecyclerView.State,
            position: Int
        ) {
            val smoothScroller: LinearSmoothScroller =
                object : LinearSmoothScroller(recyclerView.context) {
                    override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
                        // 返回:滑過1px時經歷的時間(ms)。                                                                              
                        return 20000f / displayMetrics.densityDpi
                    }
 
                    override fun calculateDtToFit(
                        viewStart: Int,
                        viewEnd: Int,
                        boxStart: Int,
                        boxEnd: Int,
                        snapPreference: Int
                    ): Int {
                        return boxStart - viewStart
                    }
                }
            smoothScroller.targetPosition = position
            startSmoothScroll(smoothScroller)
            
        }
    }

  以上就是android短視訊開發,不設定滑動限制,可一直往下滑實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2848672/,如需轉載,請註明出處,否則將追究法律責任。

相關文章