Android:GestureDetector.SimpleOnGestureListener,onFling,onScroll,velocityX,Y&distanceX,Y,kotlin

Android:GestureDetector.SimpleOnGestureListener,onFling,onScroll,record velocityX,Y & distanceX,Y in the ScrollView,kotlin

 

import android.os.Bundle
import android.view.GestureDetector
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.time.LocalTime


class MainActivity : AppCompatActivity() {
    companion object {
        var TAG = "test"
    }

    private var display: TextView? = null
    private var scrollView: ScrollView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        display = findViewById(R.id.display)
        scrollView?.findViewById(R.id.scrollView)

        var rv = findViewById(R.id.recycler_view)
        var layoutManager: LinearLayoutManager = LinearLayoutManager(this).apply {
            this.orientation = LinearLayoutManager.HORIZONTAL
        }
        rv.layoutManager = layoutManager

        var adapter = MyAdapter()
        rv.adapter = adapter

        val gestureDetector =
            GestureDetector(this, object : GestureDetector.SimpleOnGestureListener() {
                override fun onFling(
                    e1: MotionEvent,
                    e2: MotionEvent,
                    velocityX: Float,
                    velocityY: Float
                ): Boolean {
                    appendToDisplay("onFling-velocity:X,Y=${velocityX},${velocityY}  e1(${e1.x},${e1.y}) e2(${e2.x},${e2.y})")

                    return super.onFling(e1, e2, velocityX, velocityY)
                }

                override fun onScroll(
                    e1: MotionEvent,
                    e2: MotionEvent,
                    distanceX: Float,
                    distanceY: Float
                ): Boolean {
                    appendToDisplay("onScroll-distance:X,Y=${distanceX},${distanceY}")

                    return super.onScroll(e1, e2, distanceX, distanceY)
                }
            })

        rv.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                return gestureDetector.onTouchEvent(event!!)
            }
        })
    }

    fun appendToDisplay(s: String) {
        var str: String = display?.text.toString()
        str = "${LocalTime.now()}-> $s\n$str"
        display?.text = str
    }

    class MyAdapter : RecyclerView.Adapter() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
            var view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item, parent, false)
            return MyVH(view)
        }

        override fun getItemCount(): Int {
            return Int.MAX_VALUE
        }

        override fun onBindViewHolder(holder: MyVH, position: Int) {
            holder.textView.text = "@$position"
        }
    }

    class MyVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var textView = itemView.findViewById(R.id.text)

        init {

        }
    }
}

 




    

    

        

    

 

 




    

        

        

    

    

 

 

 

 

 

Android RecyclerView GestureDetector SimpleOnGestureListener,kotlin_zhangphil的博客-CSDN博客Android RecyclerView单击、长按事件:基于OnItemTouchListener + GestureDetector标准实现Android RecyclerView虽然拥有ListView绝大多数的功能,但Android RecyclerView却没有实现像ListView那样的点击事件、长按事件的标准实现方式,我在附录文章1中介绍的方式,实现了典型的RecyclerView。https://blog.csdn.net/zhangphil/article/details/130506750

 

Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类_zhangphil的博客-CSDN博客Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类我写的附录文章2,介绍了Android如何基于OnItemTouchListener +GestureDetector实现单击、长按事件的监听,由于如今RecyclerView在Android开发是如此的普遍,以及Recyclehttps://blog.csdn.net/zhangphil/article/details/53231344

Android不用OnScrollListener采用GestureDetector结合OnTouchListener实现ListView下拉/上拉刷新_gesturedetector 提交刷新页面_zhangphil的博客-CSDN博客Android不用OnScrollListener采用GestureDetector结合OnTouchListener实现ListView下拉/上拉刷新通常Android的ListView的下拉/上拉刷新实现,使用OnScrollListener比较简单,比如如果要实现下拉见顶刷新,思路是在OnScrollListener判断当前ListView的滚动状态,如果滚动停止,则将此时Lishttps://blog.csdn.net/zhangphil/article/details/50375244

 

你可能感兴趣的:(Android,kotlin,android,kotlin)