学而不思则罔,思而不学则殆
【Android】canScrollVertically和canScrollHorizontally
简介
canScrollVertically和canScrollHorizontally是原生View中的方法提供,用来判断View能否在某个方向上滑动。
总结
总结表格
测试 | 返回值ture | 返回值false |
---|---|---|
canScrollVertically(1) | 能够向上滑动 | 不能向上滑动 |
canScrollVertically(-1) | 能够向下滑动 | 不能向下滑动 |
canScrollHorizontally(1) | 能够向左滑动 | 不能向左滑动 |
canScrollHorizontally(-1) | 能够向右滑动 | 不能向右滑动 |
官方文档疑惑
/**
* Check if this view can be scrolled vertically in a certain direction.
*
* @param direction Negative to check scrolling up, positive to check scrolling down.
* @return true if this view can be scrolled in the specified direction, false otherwise.
*/
public boolean canScrollVertically(int direction) {
final int offset = computeVerticalScrollOffset();
final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
if (range == 0) return false;
if (direction < 0) {
return offset > 0;
} else {
return offset < range - 1;
}
}
检查此视图是否可以在某个方向上垂直滚动,
@param direction负值检查向上滚动,正向检查向下滚动。
@return如果此视图可以在指定方向滚动,则为true,否则为false
这个跟实际测试结果相反。我把传入的direction设为负值,才是判断手指能否向下滑动,正值是判断手指能否向上滑动。不知道是不是作者对这个“滚动”的对象或者坐标系跟我理解的不同,总之,在我看来,这个注释和代码效果是完全相反的。
/**
* Check if this view can be scrolled horizontally in a certain direction.
*
* @param direction Negative to check scrolling left, positive to check scrolling right.
* @return true if this view can be scrolled in the specified direction, false otherwise.
*/
public boolean canScrollHorizontally(int direction) {
final int offset = computeHorizontalScrollOffset();
final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
if (range == 0) return false;
if (direction < 0) {
return offset > 0;
} else {
return offset < range - 1;
}
}
检查向左滚动为负数,检查向右滚动为正数。
同理,也跟实际相反。
测试
测试canScrollVertically
自定义RecyclerView:
public class DiyRecyclerView extends RecyclerView {
public DiyRecyclerView(@NonNull Context context) {
super(context);
}
public DiyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public DiyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
Log.d("zhangyu20201011", "canScrollVertically(-1):" + canScrollVertically(-1)+" canScrollVertically(1):"+canScrollVertically(1));
return super.onTouchEvent(e);
}
}
在onTouchEvent方法中打印canScrollVertically的值。其中canScrollVertically表示竖直方向能否滑动。
滑动到顶部,在往下滑动的时候,打印log如下:
2020-10-11 22:44:09.242 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):false canScrollVertically(1):true
2020-10-11 22:44:16.250 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):false canScrollVertically(1):true
2020-10-11 22:44:16.479 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):false canScrollVertically(1):true
中间部分滑动:
2020-10-11 22:44:41.851 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):true canScrollVertically(1):true
2020-10-11 22:44:41.867 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):true canScrollVertically(1):true
2020-10-11 22:44:42.199 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):true canScrollVertically(1):true
滑动到底部,再往上滑动:
2020-10-11 22:45:08.361 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):true canScrollVertically(1):false
2020-10-11 22:45:42.289 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):true canScrollVertically(1):false
2020-10-11 22:45:42.468 11039-11039/com.example.bitmapdemo D/zhangyu20201011: canScrollVertically(-1):true canScrollVertically(1):false
测试canScrollHorizontally
测试代码:
public class DiyRecyclerView extends RecyclerView {
public DiyRecyclerView(@NonNull Context context) {
super(context);
}
public DiyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public DiyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d("zhangyu20201011", "canScrollHorizontally(-1):" + canScrollHorizontally(-1)+" canScrollHorizontally(1):"+canScrollHorizontally(1));
return super.dispatchTouchEvent(ev);
}
}
滑动到最左边,向右滑动,这个时候已经滑动不动了,log如下:
2020-10-11 22:54:33.937 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):false canScrollHorizontally(1):true
2020-10-11 22:54:34.028 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):false canScrollHorizontally(1):true
2020-10-11 22:54:34.296 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):false canScrollHorizontally(1):true
中间位置:
2020-10-11 22:55:12.024 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):false canScrollHorizontally(1):true
2020-10-11 22:55:12.106 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):false canScrollHorizontally(1):true
2020-10-11 22:55:12.138 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):false canScrollHorizontally(1):true
滑动到最右边,向左滑动:
2020-10-11 22:55:51.039 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):true canScrollHorizontally(1):false
2020-10-11 22:55:51.180 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):true canScrollHorizontally(1):false
2020-10-11 22:55:52.095 11666-11666/com.example.bitmapdemo D/zhangyu20201011: canScrollHorizontally(-1):true canScrollHorizontally(1):false