下面列出了android.view.View#canScrollHorizontally ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean canScrollLeft(View view, float x, float y) {
if (view instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) view;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
int childLeft = child.getLeft() - view.getScrollX();
int childTop = child.getTop() - view.getScrollY();
int childRight = child.getRight() - view.getScrollX();
int childBottom = child.getBottom() - view.getScrollY();
boolean intersects = x > childLeft && x < childRight && y > childTop && y < childBottom;
if (intersects && canScrollLeft(child, x - childLeft, y - childTop)) {
return true;
}
}
}
return view.canScrollHorizontally(-1);
}
private boolean canScrollRight(View view, float x, float y) {
if (view instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) view;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
int childLeft = child.getLeft() - view.getScrollX();
int childTop = child.getTop() - view.getScrollY();
int childRight = child.getRight() - view.getScrollX();
int childBottom = child.getBottom() - view.getScrollY();
boolean intersects = x > childLeft && x < childRight && y > childTop && y < childBottom;
if (intersects && canScrollRight(child, x - childLeft, y - childTop)) {
return true;
}
}
}
return view.canScrollHorizontally(1);
}
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
* or just its children (false).
* @param dx Delta scrolled in pixels
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance first.
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
// This will not work for transformed views in Honeycomb+
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight()
&& y + scrollY >= child.getTop() && y + scrollY < child.getBottom()
&& canScroll(child, true, dx, x + scrollX - child.getLeft(),
y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && v.canScrollHorizontally(-dx);
}
@Override
public boolean isNotYetInEdgeCannotMoveHeader(
SmoothRefreshLayout parent, @Nullable View child, @Nullable IRefreshView header) {
if (child == null) {
if (parent.isVerticalOrientation()) {
return !mFullyExpanded;
} else {
return true;
}
} else {
if (parent.isVerticalOrientation()) {
return !mFullyExpanded || child.canScrollVertically(-1);
} else {
return child.canScrollHorizontally(-1);
}
}
}
@Override
public boolean isNotYetInEdgeCannotMoveFooter(
SmoothRefreshLayout parent, @Nullable View child, @Nullable IRefreshView footer) {
if (child == null) {
if (parent.isVerticalOrientation()) {
return !mFullyCollapsed;
} else {
return true;
}
} else {
if (parent.isVerticalOrientation()) {
return !mFullyCollapsed || child.canScrollVertically(1);
} else {
return child.canScrollHorizontally(1);
}
}
}
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
* or just its children (false).
* @param dx Delta scrolled in pixels
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance first.
for (int i = count - 1; i >= 0; i--) {
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
canScroll(child, true, dx, x + scrollX - child.getLeft(),
y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && v.canScrollHorizontally(-dx);
}
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
* or just its children (false).
* @param dx Delta scrolled in pixels along the X axis
* @param dy Delta scrolled in pixels along the Y axis
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance first.
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
// This will not work for transformed views in Honeycomb+
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
canScroll(child, true, dx, dy, x + scrollX - child.getLeft(),
y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && (v.canScrollHorizontally(-dx) ||
v.canScrollVertically(-dy));
}
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
* or just its children (false).
* @param dx Delta scrolled in pixels along the X axis
* @param dy Delta scrolled in pixels along the Y axis
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance first.
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
// This will not work for transformed views in Honeycomb+
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight()
&& y + scrollY >= child.getTop() && y + scrollY < child.getBottom()
&& canScroll(child, true, dx, dy, x + scrollX - child.getLeft(),
y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && (v.canScrollHorizontally(-dx) || v.canScrollVertically(-dy));
}
public static boolean canScrollLeft(@NonNull View targetView) {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (targetView instanceof AbsListView) {
final ViewGroup viewGroup = (ViewGroup) targetView;
final AbsListView absListView = (AbsListView) targetView;
return viewGroup.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0
|| viewGroup.getChildAt(0).getTop() < targetView.getPaddingTop());
} else {
return targetView.getScrollY() > 0;
}
} else {
return targetView.canScrollHorizontally(-1);
}
}
public static boolean canScrollRight(@NonNull View targetView) {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (targetView instanceof AbsListView) {
final ViewGroup viewGroup = (ViewGroup) targetView;
final AbsListView absListView = (AbsListView) targetView;
final int childCount = viewGroup.getChildCount();
return childCount > 0 && (absListView.getLastVisiblePosition() < childCount - 1
|| viewGroup.getChildAt(childCount - 1).getBottom() > targetView.getPaddingBottom());
} else {
return targetView.getScrollY() < 0;
}
} else {
return targetView.canScrollHorizontally(1);
}
}
@Override
public boolean isNotYetInEdgeCannotMoveHeader() {
View targetView = getScrollTargetView();
if (mInEdgeCanMoveHeaderCallBack != null) {
return mInEdgeCanMoveHeaderCallBack.isNotYetInEdgeCannotMoveHeader(
this, targetView, mHeaderView);
}
return targetView != null && targetView.canScrollHorizontally(-1);
}
@Override
public boolean isNotYetInEdgeCannotMoveFooter() {
final View targetView = getScrollTargetView();
if (mInEdgeCanMoveFooterCallBack != null) {
return mInEdgeCanMoveFooterCallBack.isNotYetInEdgeCannotMoveFooter(
this, targetView, mHeaderView);
}
return targetView != null && targetView.canScrollHorizontally(1);
}
@Override
public void onNestedScrollAccepted(@NonNull View child, @NonNull View target, int axes, int type) {
isBeingNestedScrolling = true;
if ((!target.canScrollHorizontally(-1) && !target.canScrollHorizontally(1))
|| swipeDistance != 0) {
nestedScrollingDistance = nestedScrollingTrigger;
} else {
nestedScrollingDistance = 0;
}
}
@Override
protected boolean canScroll(View webView, boolean checkV, int dx, int x, int y) {
if (webView instanceof HorizontalSlideWebView) {
// Log.d(TAG, "dx: " + dx + " x:" + x + " y:" + y);
return webView.canScrollHorizontally(y); // 不再兼容 API < 14
} else {
return super.canScroll(webView, checkV, dx, x, y);
}
}
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for
* scrollability (true), or just its children (false).
* @param dx Delta scrolled in pixels along the X axis
* @param dy Delta scrolled in pixels along the Y axis
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance
// first.
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
// This will not work for transformed views in Honeycomb+
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft()
&& x + scrollX < child.getRight()
&& y + scrollY >= child.getTop()
&& y + scrollY < child.getBottom()
&& canScroll(child, true, dx, dy, x + scrollX - child.getLeft(), y
+ scrollY - child.getTop())) {
return true;
}
}
}
return checkV
&& (v.canScrollHorizontally(-dx) || v.canScrollVertically(-dy));
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
final int layoutDirection = dx > 0 ? 1 : -1;
View currView = getCurrentPageView();
//check if we need to work with the child view
if (recyclerView != null && currView != null && currView.canScrollHorizontally(layoutDirection)
&& ((layoutDirection == 1 && currView.getLeft() <= 0) ||
(layoutDirection == -1 && currView.getRight() >= currView.getWidth()))
) {
dOffset = 0;
if (lastTouchEvent != null) {
currView.dispatchTouchEvent(lastTouchEvent);
lastTouchEvent = null;
}
ViewCompat.setOverScrollMode(recyclerView, ViewCompat.OVER_SCROLL_NEVER);
if ((layoutDirection == 1 && currView.getLeft() < 0) || (layoutDirection == -1 && currView.getRight() > currView.getWidth())) {
adjust();
}
return 0;
} else if (Math.abs(dOffset + dx) < triggPx &&
recyclerView != null && currView != null &&
currView.getLeft() == 0 && currView.getRight() == currView.getWidth()
) {
dOffset += dx;
if (lastTouchEvent != null) {
currView.dispatchTouchEvent(lastTouchEvent);
lastTouchEvent = null;
}
ViewCompat.setOverScrollMode(recyclerView, ViewCompat.OVER_SCROLL_NEVER);
return 0;
} else {
dOffset = 0;
ViewCompat.setOverScrollMode(recyclerView, ViewCompat.OVER_SCROLL_ALWAYS);
return super.scrollHorizontallyBy(dx, recycler, state);
}
}
public static boolean a(View view, int i)
{
return view.canScrollHorizontally(i);
}
public static boolean canScrollHorizontally(View v, int direction) {
return v.canScrollHorizontally(direction);
}
/**
* view是否已经滚动到最左边
*
* @param view
* @return
*/
public static boolean isScrollToLeft(View view)
{
return !view.canScrollHorizontally(-1);
}
/**
* view是否已经滚动到最右边
*
* @param view
* @return
*/
public static boolean isScrollToRight(View view)
{
return !view.canScrollHorizontally(1);
}