Android RecyclerView canScrollVertically方向与返回值,Kotlin
Kotlin
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
companion object {
const val TAG = "fly"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rv = findViewById<RecyclerView>(R.id.rv)
val linearLayoutManager = LinearLayoutManager(this)
linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
rv.layoutManager = linearLayoutManager
val adapter = MyAdapter()
rv.adapter = adapter
rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val b1 = recyclerView.canScrollVertically(1)
Log.d(TAG, "scrolling up $b1")
val b2 = recyclerView.canScrollVertically(-1)
Log.d(TAG, "scrolling down $b2")
}
})
}
class MyAdapter : RecyclerView.Adapter<MyVH> {
constructor() {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
val v = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)
return MyVH(v)
}
override fun getItemCount(): Int {
return 30
}
override fun onBindViewHolder(holder: MyVH, position: Int) {
holder.text?.text = "$position"
}
}
class MyVH : RecyclerView.ViewHolder {
var text: TextView? = null
constructor(itemView: View) : super(itemView) {
text = itemView.findViewById(android.R.id.text1)
text?.setTextColor(ContextCompat.getColor(itemView.context, android.R.color.holo_red_light))
}
}
}
1、当手指在屏幕下滑,直到把RecyclerView第一个item(0)显示在最顶部,即RecyclerView不能往下再滑的时候:
scrolling up true
scrolling down false
2、当手指在屏幕上滑,直到把RecyclerView最后一个item(29)显示在最底部,即RecyclerView不能往上再滑的时候:
scrolling up false
scrolling down true
除此以外,当RecyclerView处于既可以上滑也可以下滑时候,canScrollVertically返回值均为true:
scrolling up true
scrolling down true
Android RecyclerView滑动到底部/上拉/上拉见底自动加载更多实现以及点击/长按事件处理_android 模拟上拉事件-CSDN博客文章浏览阅读3.8k次。Android RecyclerView滑动到底部/上拉/上拉见底自动加载更多实现以及点击/长按事件处理Android RecyclerView下拉加载更多有Android谷歌官方的实现,实现的方式是通过SwipeRefreshLayout把RecyclerView包裹起来,然后就可以通过SwipeRefreshLayout的回调接口实现下拉刷新功能。Android上拉加载上拉刷新的实现没有官方统..._android 模拟上拉事件https://blog.csdn.net/zhangphil/article/details/79662823如何使用Android最新的RecyclerView取代ListView?_android recyclerview替代listview-CSDN博客文章浏览阅读2.3k次。效果图如下:使用RecyclerView之前需要先导入android.support.v7.widget.RecyclerView所在的jar包。就在support.v7下面,目录结构如下:...\android-sdk-windows\extras\android\support\v7\recyclerview\libs\android-support-v7-recycler_android recyclerview替代listviewhttps://blog.csdn.net/zhangphil/article/details/45170845