获取view的宽高为0
1. 如果你的宽高在xml里写死了
View.measureWidth
View.measureHeight
2.如果是在onCreate声明周期中获取
View.post {
View.width
View.height
}
获取屏幕宽高
Context.resources.displayMetrics.widthPixels
Context.resources.displayMetrics.heightPixels
获取你要设置TextView文字前的长度
在你设置TextView文字前你想要知道文字的长度进行特定的实现可以:
TextView.paint.measureText("文字长度")
判断RecyclerView滑动到底部和顶部 通过LinearLayoutManager
RecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
val isFirst = (RecyclerView.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() == 0 //用于判断是否在顶部
val isLast = (RecyclerView.layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition() == (RecyclerView.layoutManager as LinearLayoutManager).itemCount - 1 //用于判断是否在底部
}
})