监听横向Recyclerview滑动到底部
recyclerview.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
//得到当前显示的最后一个item的view
val lastChildView = recyclerView.layoutManager!!.getChildAt(recyclerView.layoutManager!!.childCount - 1)
//得到lastChildView的bottom坐标值
val lastChildBottom = lastChildView!!.right
//得到Recyclerview的底部坐标减去底部padding值,也就是显示内容最底部的坐标
val recyclerBottom = recyclerView.right - recyclerView.rightPadding
//通过这个lastChildView得到这个view当前的position值
val lastPosition = recyclerView.layoutManager!!.getPosition(lastChildView)
//判断lastChildView的bottom值跟recyclerBottom相等
//判断lastPosition是不是最后一个position
if (lastChildBottom == recyclerBottom && lastPosition == recyclerView.layoutManager!!.itemCount - 1) {
Toast.makeText(context, "到底了", Toast.LENGTH_SHORT).show();
}
}
})
监听纵向Recyclerview滑动到底部
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//得到当前显示的最后一个item的view
View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount()-1);
//得到lastChildView的bottom坐标值
int lastChildBottom = lastChildView.getBottom();
//得到Recyclerview的底部坐标减去底部padding值,也就是显示内容最底部的坐标
int recyclerBottom = recyclerView.getBottom()-recyclerView.getPaddingBottom();
//通过这个lastChildView得到这个view当前的position值
int lastPosition = recyclerView.getLayoutManager().getPosition(lastChildView);
//判断lastChildView的bottom值跟recyclerBottom是否相等
//判断lastPosition是不是最后一个position
if(lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount()-1 ){
Toast.makeText(context, "到底了", Toast.LENGTH_SHORT).show();
}
}
});