下面列出了android.view.View#getTop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
int slop = yvel > minimumFlingVelocity ? getHeight() / 6 : getHeight() / 3;
if (releasedChild.getTop() > slop) {
if (callback != null) {
callback.onPullComplete();
}
} else {
if (callback != null) {
callback.onPullCancel();
}
dragger.settleCapturedViewAt(0, 0);
invalidate();
}
}
protected void onMiddleMsgLoaderSuccessCallback(int position, CommentListBean newValue, boolean towardsBottom) {
if (newValue != null) {
int size = newValue.getSize();
if (getActivity() != null && newValue.getSize() > 0) {
getDataList().addMiddleData(position, newValue, towardsBottom);
if (towardsBottom) {
getAdapter().notifyDataSetChanged();
} else {
View v = Utility.getListViewItemViewFromPosition(getListView(), position + 1 + 1);
int top = (v == null) ? 0 : v.getTop();
getAdapter().notifyDataSetChanged();
int ss = position + 1 + size - 1;
getListView().setSelectionFromTop(ss, top);
}
}
}
}
private boolean isFirstItemVisible() {
final Adapter adapter = mRootView.getAdapter();
if (null == adapter || adapter.isEmpty()) {
return true;
} else {
/**
* This check should really just be:
* mRootView.getFirstVisiblePosition() == 0, but PtRListView
* internally use a HeaderView which messes the positions up. For
* now we'll just add one to account for it and rely on the inner
* condition which checks getTop().
*/
if (mRootView.getFirstVisiblePosition() <= 1) {
final View firstVisibleChild = mRootView.getChildAt(0);
if (firstVisibleChild != null) {
return firstVisibleChild.getTop() >= mRootView.getTop();
}
}
}
return false;
}
/**
* 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 && ViewCompat.canScrollHorizontally(v, -dx);
}
private Rect getRect(View view) {
Rect rect = new Rect();
rect.top = view.getTop();
rect.left = view.getLeft();
rect.right = view.getRight();
rect.bottom = view.getBottom();
ViewParent viewParent = view.getParent();
if (viewParent != null) {
if (viewParent instanceof ViewGroup) {
Rect prect = getRect((ViewGroup) viewParent);
rect.top += prect.top;
rect.left += prect.left;
rect.right += prect.left;
rect.bottom += prect.top;
}
}
return rect;
}
/**
* Determines if an item is obscured by a header
*
*
* @param parent
* @param item to determine if obscured by header
* @param header that might be obscuring the item
* @param orientation of the {@link RecyclerView}
* @return true if the item view is obscured by the header view
*/
private boolean itemIsObscuredByHeader(RecyclerView parent, View item, View header, int orientation) {
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) item.getLayoutParams();
mDimensionCalculator.initMargins(mTempRect1, header);
int adapterPosition = parent.getChildAdapterPosition(item);
if (adapterPosition == RecyclerView.NO_POSITION || mHeaderProvider.getHeader(parent, adapterPosition) != header) {
// Resolves https://github.com/timehop/sticky-headers-recyclerview/issues/36
// Handles an edge case where a trailing header is smaller than the current sticky header.
return false;
}
if (orientation == LinearLayoutManager.VERTICAL) {
int itemTop = item.getTop() - layoutParams.topMargin;
int headerBottom = header.getBottom() + mTempRect1.bottom + mTempRect1.top;
if (itemTop > headerBottom) {
return false;
}
} else {
int itemLeft = item.getLeft() - layoutParams.leftMargin;
int headerRight = header.getRight() + mTempRect1.right + mTempRect1.left;
if (itemLeft > headerRight) {
return false;
}
}
return true;
}
@Override
public int getScrollY() {
int firstVisiblePosition = absListView.getFirstVisiblePosition();
View c = absListView.getChildAt(0);
if (c == null) {
return 0;
}
int top = c.getTop() - absListView.getPaddingTop();
return -top + firstVisiblePosition * c.getHeight();
}
private View getChildForTouchEvent(MotionEvent event) {
final float x = event.getX(), y = event.getY();
View child;
for (int z = getChildCount() - 1; z >= 0 ; z--) {
child = getChildAt(z);
if (child.isShown() && x >= child.getLeft() && x <= child.getRight()
&& y >= child.getTop() && y <= child.getBottom()) {
if (DEBUG) Log.d(LOG_TAG, "Got Child for Touch Event: " + child);
return child;
}
}
return null;
}
public static void drawLeftAlignItem(Canvas canvas, Drawable drawable, View child, ViewGroup.MarginLayoutParams params) {
final int top = child.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin;
final int left = child.getLeft() - params.leftMargin - drawable.getIntrinsicWidth();
final int right = left + drawable.getIntrinsicWidth();
drawable.setBounds(left, top, right, bottom);
drawable.draw(canvas);
}
private boolean inChild(int x, int y) {
if (getChildCount() > 0) {
final int scrollY = getScrollY();
final View child = getChildAt(0);
return !(y < child.getTop() - scrollY
|| y >= child.getBottom() - scrollY
|| x < child.getLeft()
|| x >= child.getRight());
}
return false;
}
/**
* @param c
* @param parent
* @param state
*/
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (dividerDrawable == null) {
return;
}
int childCount = parent.getChildCount();
int rightV = parent.getWidth();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int leftV = parent.getPaddingLeft() + child.getPaddingLeft();
int bottomV = child.getTop() - params.topMargin;
int topV = bottomV - dividerDrawable.getIntrinsicHeight();
int topH = child.getTop() + params.topMargin;
int bottomH = child.getBottom() + params.bottomMargin;
int rightH = child.getLeft() - params.leftMargin;
int leftH = rightH - dividerDrawable.getIntrinsicWidth();
dividerDrawable.setBounds(leftH, topH, rightH, bottomH);
dividerDrawable.draw(c);
dividerDrawable.setBounds(leftV, topV, rightV, bottomV);
dividerDrawable.draw(c);
}
}
float[] mapPointFromViewToParent(View v, float x, float y) {
mTmpPoint[0] = x;
mTmpPoint[1] = y;
v.getMatrix().mapPoints(mTmpPoint);
mTmpPoint[0] += v.getLeft();
mTmpPoint[1] += v.getTop();
return mTmpPoint;
}
@Override
public boolean isReadyForPull(View view, final float x, final float y) {
boolean ready = false;
// First we check whether we're scrolled to the top
AbsListView absListView = (AbsListView) view;
if (absListView.getCount() == 0) {
ready = true;
} else if (absListView.getFirstVisiblePosition() == 0) {
final View firstVisibleChild = absListView.getChildAt(0);
ready = firstVisibleChild != null && firstVisibleChild.getTop() >= 0;
}
// Then we have to check whether the fas scroller is enabled, and check we're not starting
// the gesture from the scroller
if (ready && absListView.isFastScrollEnabled() && isFastScrollAlwaysVisible(absListView)) {
switch (getVerticalScrollbarPosition(absListView)) {
case View.SCROLLBAR_POSITION_RIGHT:
ready = x < absListView.getRight() - absListView.getVerticalScrollbarWidth();
break;
case View.SCROLLBAR_POSITION_LEFT:
ready = x > absListView.getVerticalScrollbarWidth();
break;
}
}
return ready;
}
public int getFirstPositionOffset() {
final View firstChild = getChildAt(0);
if (firstChild == null) {
return 0;
}
return firstChild.getTop();
}
/**
* 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
&& (ViewCompat.canScrollHorizontally(v, -dx) || ViewCompat.canScrollVertically(v,
-dy));
}
/**
* close surface
*
* @param smooth smoothly or not.
* @param notify if notify all the listeners.
*/
public void close(boolean smooth, boolean notify) {
View surface = getSurfaceView();
View bottomView = getCurrentBottomView();
if (surface == null) {
return;
}
Rect rect = computeSurfaceLayoutArea(false);
int dx, dy;
if (smooth) {
mDragHelper.smoothSlideViewTo(getSurfaceView(), rect.left, rect.top);
mDragHelper.smoothSlideViewTo(bottomView, rect.right, rect.top);
} else {
int measuredWidth = bottomView.getMeasuredWidth();
dx = rect.left - surface.getLeft();
dy = rect.top - surface.getTop();
surface.layout(rect.left, rect.top, rect.right, rect.bottom);
bottomView.layout(rect.right, rect.top, rect.right + measuredWidth, rect.bottom);
if (notify) {
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
dispatchSwipeEvent(rect.left, rect.top, dx, dy);
} else {
safeBottomView();
}
}
invalidate();
}
private View getChildOnOffset(AppBarLayout abl, final int offset) {
for (int i = 0, count = abl.getChildCount(); i < count; i++) {
View child = abl.getChildAt(i);
if (child.getTop() <= -offset && child.getBottom() >= -offset) {
return child;
}
}
return null;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean enable = true;
float x = centerPoint.x;
float y = centerPoint.y;
float y1 = event.getY();
float x1 = event.getX();
float x2 = v.getLeft();
float y2 = v.getTop();
float x3 = x1 - x2;
float y3 = y1 - y2;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
m_x = x3;
m_y = y3;
tounched = true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
m_x = 0;
m_y = 0;
tounched = false;
}
float d = (float) Math.sqrt((Math.pow(Math.abs((x - x3)), 2) + Math
.pow(Math.abs((y - y3)), 2)));
if (d > radius) {
m_x = 0;
m_y = 0;
enable = false;
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if ((m_x == 0) && (m_y == 0)) {
enable = false;
}
if (enable) {
float c = (float) Math
.sqrt((Math.pow(Math.abs((m_x - x3)), 2) + Math.pow(
Math.abs((m_y - y3)), 2)));
float a = (float) Math
.sqrt((Math.pow(Math.abs(m_x - x), 2) + Math.pow(
Math.abs((m_y - y)), 2)));
float b = (float) Math
.sqrt((Math.pow(Math.abs(x3 - x), 2) + Math.pow(
Math.abs((y3 - y)), 2)));
float cosc = (float) (Math.pow(a, 2) + Math.pow(b, 2) - Math
.pow(c, 2)) / (2 * a * b);
float m_angle = getAngle((m_x - x), (m_y - y));
float angle3 = getAngle((x3 - x), (y3 - y));
if ((90 > m_angle) && (angle3 > 270)) {
m_angle = m_angle + (float) 360;
}
if ((90 > angle3) && (m_angle > 270)) {
angle3 = angle3 + (float) 360;
}
if (angle3 - m_angle > 0) {
startAngle = (float) (startAngle + Math.acos(cosc) * 180
/ Math.PI);
} else {
startAngle = (float) (startAngle - Math.acos(cosc) * 180
/ Math.PI);
}
m_x = x3;
m_y = y3;
}
}
return true;
}
private int getRelativeTop(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getTop();
else
return myView.getTop() + getRelativeTop((View) myView.getParent());
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mEnableDivider) {
drawDivider(c, parent);
}
// 只支持LinearLayoutManager或者GridLayoutManager且一行只有一列的情况,这个比较符合使用场景
if (parent.getLayoutManager() instanceof GridLayoutManager && ((GridLayoutManager) parent.getLayoutManager()).getSpanCount() > 1) {
return;
} else if (parent.getLayoutManager() instanceof StaggeredGridLayoutManager) {
return;
}
// 检测到标签存在的时候,将标签强制固定在RecyclerView顶部
createPinnedHeader(parent);
if (!mDisableDrawHeader && mPinnedHeaderView != null && mFirstVisiblePosition >= mHeaderPosition) {
// 标签相对parent高度加上自身的高度
final int headerEndAt = mPinnedHeaderParentView.getTop() + mPinnedHeaderParentView.getMeasuredHeight() + mRecyclerViewPaddingTop;
// 根据xy坐标查找view
View v = parent.findChildViewUnder(c.getWidth() / 2, headerEndAt + 1);
if (isPinnedHeader(parent, v) && v.getTop() <= mPinnedHeaderView.getHeight() + mRecyclerViewPaddingTop + mParentPaddingTop) {
// 如果view是标签的话,那么缓存的标签就要跟随这个真正的标签标签移动了,效果类似于下面的标签把它顶上去一样
// 得到mPinnedHeaderView为标签跟随移动的位移
mPinnedHeaderOffset = v.getTop() - (mRecyclerViewPaddingTop + mParentPaddingTop + mPinnedHeaderView.getHeight());
} else {
mPinnedHeaderOffset = 0;
}
// Log.e("TAG", "SmallPinnedHeaderItemDecoration-152行-onDraw(): " + v.getTop() + ";" + mPinnedHeaderOffset);
// 拿到锁定的矩形
mClipBounds = c.getClipBounds();
mClipBounds.left = 0;
mClipBounds.right = parent.getWidth();
mClipBounds.top = mRecyclerViewPaddingTop + mParentPaddingTop;
mClipBounds.bottom = parent.getHeight();
// 重新锁定
c.clipRect(mClipBounds);
}
}