下面列出了android.widget.AbsListView#getFirstVisiblePosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @return Whether it is possible for the child view of this layout to
* scroll up. Override this if the child view is a custom view.
*/
public boolean canChildScrollUp() {
if (mChildScrollUpCallback != null) {
return mChildScrollUpCallback.canChildScrollUp(this, mTarget);
}
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(mTarget, -1) || mTarget.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTarget, -1);
}
}
public int getScrollY(AbsListView view) {
View c = view.getChildAt(0);
if (c == null) {
return 0;
}
int firstVisiblePosition = view.getFirstVisiblePosition();
int top = c.getTop();
int headerHeight = 0;
if (firstVisiblePosition >= 1) {
headerHeight = mHeaderHeight;
}
return -top + firstVisiblePosition * c.getHeight() + headerHeight;
}
/**
* Whether child view can scroll up
* @return
*/
public boolean canChildScrollUp() {
if (mTargetView == null) {
return false;
}
if (Build.VERSION.SDK_INT < 14) {
if (mTargetView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTargetView;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(mTargetView, -1) || mTargetView.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTargetView, -1);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
mFirstVisibleItem = firstVisibleItem;
if (view.getFirstVisiblePosition() == 1) {
mIsTop = true;
// 滑动到顶部
} else if (onLoadMoreListener != null && view.getLastVisiblePosition() == view.getCount() - 1) {
mIsBottom = true;
onLoadMoreListener.onLoadMore();
// 滑动到底部
} else {
mIsTop = false;
mIsBottom = false;
}
}
/**
* @return Whether it is possible for the child view of this layout to
* scroll up. Override this if the child view is a custom view.
*/
public boolean canChildScrollUp() {
if (mChildScrollUpCallback != null) {
return mChildScrollUpCallback.canChildScrollUp(this, mTarget);
}
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(mTarget, -1) || mTarget.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTarget, -1);
}
}
/**
* @return Whether it is possible for the child view of this layout to
* scroll up. Override this if the child view is a custom view.
*/
public boolean canChildScrollUp() {
if (onChildScrollUpCallback != null) {
return onChildScrollUpCallback.canChildScrollUp(this, target);
}
if (android.os.Build.VERSION.SDK_INT < 14) {
if (target instanceof AbsListView) {
final AbsListView absListView = (AbsListView) target;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 ||
absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(target, -1) || target.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(target, -1);
}
}
private boolean canChildScrollDown() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return mTarget.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTarget, -1);
}
}
/**
* Returns, whether a specific list view is scrolled to the top, or not.
*
* @param listView The list view as an instance of the class {@link AbsListView}. The list view may not
* be null
* @return True, if the given list view is scrolled to the top, false otherwise
*/
private boolean isListViewScrolledToTop(@NonNull final AbsListView listView) {
if (listView.getFirstVisiblePosition() == 0) {
if (listView.getChildCount() == 0) {
return true;
} else {
View child = listView.getChildAt(0);
return child == null || child.getTop() == 0;
}
}
return false;
}
public static boolean canChildScrollUp(View view) {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (view instanceof AbsListView) {
final AbsListView absListView = (AbsListView) view;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return view.getScrollY() > 0;
}
} else {
return view.canScrollVertically(-1);
}
}
@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() >= absListView.getPaddingTop();
}
// 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 static boolean isAbsListViewToTop(AbsListView absListView) {
if (absListView != null) {
int firstChildTop = 0;
if (absListView.getChildCount() > 0) {
// 如果AdapterView的子控件数量不为0,获取第一个子控件的top
firstChildTop = absListView.getChildAt(0).getTop() - absListView.getPaddingTop();
}
if (absListView.getFirstVisiblePosition() == 0 && firstChildTop == 0) {
return true;
}
}
return false;
}
/**
* Updates the title and selected month if the view has moved to a new
* month.
*/
@Override
public void onScroll(
AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
MonthView child = (MonthView) view.getChildAt(0);
if (child == null) {
return;
}
// Figure out where we are
long currScroll = view.getFirstVisiblePosition() * child.getHeight() - child.getBottom();
mPreviousScrollPosition = currScroll;
mPreviousScrollState = mCurrentScrollState;
}
public static int getScrollY(AbsListView lv) {
View c = lv.getChildAt(0);
if (c == null) {
return 0;
}
int firstVisiblePosition = lv.getFirstVisiblePosition();
int scrollY = -(c.getTop());
// int scrollY = 0;
sListViewItemHeights.put(lv.getFirstVisiblePosition(), c.getHeight());
// if(scrollY>0)
// Log.d("QuickReturnUtils", "getScrollY() : -(c.getTop()) - "+ -(c.getTop()));
// else
// Log.i("QuickReturnUtils", "getScrollY() : -(c.getTop()) - "+ -(c.getTop()));
if (scrollY < 0)
scrollY = 0;
for (int i = 0; i < firstVisiblePosition; ++i) {
// Log.d("QuickReturnUtils", "getScrollY() : i - "+i);
// Log.d("QuickReturnUtils", "getScrollY() : sListViewItemHeights.get(i) - "+sListViewItemHeights.get(i));
if (sListViewItemHeights.get(i) != null) // (this is a sanity check)
scrollY += sListViewItemHeights.get(i); //add all heights of the views that are gone
}
// Log.d("QuickReturnUtils", "getScrollY() : scrollY - "+scrollY);
return scrollY;
}
/**
* Updates the title and selected month if the view has moved to a new
* month.
*/
@Override
public void onScroll(
AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
MonthView child = (MonthView) view.getChildAt(0);
if (child == null) {
return;
}
// Figure out where we are
long currScroll = view.getFirstVisiblePosition() * child.getHeight() - child.getBottom();
mPreviousScrollPosition = currScroll;
mPreviousScrollState = mCurrentScrollState;
}
private boolean canChildScrollUp() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (contentView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) contentView;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(contentView, -1) || contentView.getScrollY() > 0;
}
} else {
/*return true can swipe up*/
return ViewCompat.canScrollVertically(contentView, -1);
}
}
@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() >= absListView.getPaddingTop();
}
// 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;
}
/**
* @return Whether it is possible for the child view of this layout to
* scroll up. Override this if the child view is a custom view.
*/
public boolean canChildScrollUp() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return mTarget.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTarget, -1);
}
}
/**
* @return Whether it is possible for the child view of this layout to
* scroll up. Override this if the child view is a custom view.
*/
public boolean canChildScrollUp() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return mTarget.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTarget, -1);
}
}
public static boolean canChildScrollUp(View view) {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (view instanceof AbsListView) {
final AbsListView absListView = (AbsListView) view;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0).getTop() <
absListView.getPaddingTop());
} else {
// return view.getScrollY() > 0;
return ViewCompat.canScrollVertically(view, -1) || view.getScrollY() > 0;
}
} else {
return view.canScrollVertically(-1);
}
}
public static int getScrollY(AbsListView lv) {
View c = lv.getChildAt(0);
if (c == null) {
return 0;
}
int firstVisiblePosition = lv.getFirstVisiblePosition();
int scrollY = -(c.getTop());
// int scrollY = 0;
sListViewItemHeights.put(lv.getFirstVisiblePosition(), c.getHeight());
// if(scrollY>0)
// Log.d("QuickReturnUtils", "getScrollY() : -(c.getTop()) - "+ -(c.getTop()));
// else
// Log.i("QuickReturnUtils", "getScrollY() : -(c.getTop()) - "+ -(c.getTop()));
if(scrollY<0)
scrollY = 0;
for (int i = 0; i < firstVisiblePosition; ++i) {
// Log.d("QuickReturnUtils", "getScrollY() : i - "+i);
// Log.d("QuickReturnUtils", "getScrollY() : sListViewItemHeights.get(i) - "+sListViewItemHeights.get(i));
if (sListViewItemHeights.get(i) != null) // (this is a sanity check)
scrollY += sListViewItemHeights.get(i); //add all heights of the views that are gone
}
// Log.d("QuickReturnUtils", "getScrollY() : scrollY - "+scrollY);
return scrollY;
}