下面列出了android.widget.AbsListView#getChildCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static boolean performAbsListView(AbsListView view, int direction) {
int childCount = view.getChildCount();
if (childCount > 0) {
switch (direction) {
case DIRECTION_DOWN:
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
int firstItemTop = view.getChildAt(0).getTop();
int listViewTop = view.getTop() + view.getPaddingTop() - lp.topMargin;
if (DEBUG_SCROLL_CHECK)
L.e(TAG, "firstItemTop=%s,listViewTop=%s", firstItemTop, listViewTop);
return (view.getFirstVisiblePosition() > 0
|| firstItemTop < listViewTop);
case DIRECTION_UP:
int lastItemBottom = view.getChildAt(childCount - 1).getBottom();
int listViewBottom = view.getBottom() - view.getPaddingBottom();
if (DEBUG_SCROLL_CHECK)
L.e(TAG, "lastItemBottom=%s,listViewBottom=%s", lastItemBottom, listViewBottom);
return (view.getLastVisiblePosition() < childCount - 1
|| lastItemBottom > listViewBottom);
}
}
if (DEBUG_SCROLL_CHECK)
L.e(TAG, "AbsListView cannot scroll vertically or childCount is 0!!");
return 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 (mOnChildScrollUpCallback != null) {
return mOnChildScrollUpCallback.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 static boolean canChildScrollDown(View view) {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (view instanceof AbsListView) {
final AbsListView absListView = (AbsListView) view;
return absListView.getChildCount() > 0
&& (absListView.getLastVisiblePosition() < absListView.getChildCount() - 1
|| absListView.getChildAt(absListView.getChildCount() - 1).getBottom() > absListView.getPaddingBottom());
} else if (view instanceof ScrollView) {
ScrollView scrollView = (ScrollView) view;
if (scrollView.getChildCount() == 0) {
return false;
} else {
return scrollView.getScrollY() < scrollView.getChildAt(0).getHeight() - scrollView.getHeight();
}
} else {
return false;
}
} else {
return view.canScrollVertically(1);
}
}
/**
* Whether child view can scroll down
* @return
*/
public boolean canChildScrollDown() {
if (mTargetView == null) {
return false;
}
if (Build.VERSION.SDK_INT < 14) {
if (mTargetView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTargetView;
if (absListView.getChildCount() > 0) {
int lastChildBottom = absListView.getChildAt(absListView.getChildCount() - 1)
.getBottom();
return absListView.getLastVisiblePosition() == absListView.getAdapter().getCount() - 1
&& lastChildBottom <= absListView.getMeasuredHeight();
} else {
return false;
}
} else {
return ViewCompat.canScrollVertically(mTargetView, 1) || mTargetView.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTargetView, 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 (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);
}
}
private boolean canScrollDown(View view)
{
if (android.os.Build.VERSION.SDK_INT < 14)
{
if (view instanceof AbsListView)
{
final AbsListView absListView = (AbsListView) view;
return absListView.getChildCount() > 0
&& (absListView.getLastVisiblePosition() < absListView.getChildCount() - 1
|| absListView.getChildAt(absListView.getChildCount() - 1).getBottom() > absListView.getPaddingBottom());
} else
{
return ViewCompat.canScrollVertically(view, 1) || view.getScrollY() < 0;
}
} else
{
return ViewCompat.canScrollVertically(view, 1);
}
}
/**
* Utility method to check whether a {@link View} can scroll up from it's current position.
* Handles platform version differences, providing backwards compatible functionality where
* needed.
*/
private static boolean canViewScrollUp(View view) {
if (android.os.Build.VERSION.SDK_INT >= 14) {
// For ICS and above we can call canScrollVertically() to determine this
return ViewCompat.canScrollVertically(view, -1);
} else {
if (view instanceof AbsListView) {
// Pre-ICS we need to manually check the first visible item and the child view's top
// value
final AbsListView listView = (AbsListView) view;
return listView.getChildCount() > 0 &&
(listView.getFirstVisiblePosition() > 0
|| listView.getChildAt(0).getTop() < listView.getPaddingTop());
} else {
// For all other view types we just check the getScrollY() value
return view.getScrollY() > 0;
}
}
}
public static boolean reachedListBottom(AbsListView listView) {
boolean flag = true;
if (listView.getChildCount() != 0) {
int i = listView.getLastVisiblePosition();
int j = listView.getCount();
int k = listView.getHeight();
int l = listView.getChildAt(-1 + listView.getChildCount()).getBottom();
if (i != j - 1 || l > k) {
flag = false;
}
}
return flag;
}
/**
* @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);
}
}
/**
* Whether it is possible for the child view of this layout to scroll down. Override this if the child view is a custom view.
* 判断是否可以上拉
*/
public static boolean canChildScrollDown(View mChildView) {
if (Build.VERSION.SDK_INT < 14) {
if (mChildView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mChildView;
return absListView.getChildCount() > 0
&& (absListView.getLastVisiblePosition() < absListView.getChildCount() - 1
|| absListView.getChildAt(absListView.getChildCount() - 1).getBottom() > absListView.getPaddingBottom());
} else {
return ViewCompat.canScrollVertically(mChildView, 1) || mChildView.getScrollY() < 0;
}
} else {
return ViewCompat.canScrollVertically(mChildView, 1);
}
}
/**
* 判断目标View是否滑动到顶部-还能否继续滑动
*
* @return
*/
public boolean isChildScrollToTop() {
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 void onScroll(AbsListView v, int first, int count, int total) {
int top = 0;
if (v.getChildCount() > 0) {
top = -v.getChildAt(0).getTop() + first * v.getChildAt(0).getHeight();
if (top >= 0) {
mTopViewHeight = Math.max((int) (Style.MAX_TOP_HEIGHT - top/(getResources().getDisplayMetrics().density)/1.5f), Style.MIN_TOP_HEIGHT);
}
}
}
/**
* AbsListView类型的View是否已滑动到底部
* @param listView
* @return
*/
public static boolean absListViewReachBottom(AbsListView listView) {
if (listView.getChildCount() > 0) {
int lastItemBottom = listView.getChildAt(listView.getChildCount() - 1).getBottom();
int listHeight = listView.getBottom() - listView.getTop();
return listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1
&& lastItemBottom <= listHeight;
}
return 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 (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);
}
}
/**
* 判断View是否可以上拉
* @return canChildScrollUp
*/
public boolean canChildScrollUp() {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
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);
}
}
/**
* 判断View是否可以下拉
* @return canChildScrollDown
*/
public boolean canChildScrollDown() {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (mTargetView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTargetView;
return absListView.getChildCount() > 0 && absListView.getAdapter() != null
&& (absListView.getLastVisiblePosition() < absListView.getAdapter().getCount() - 1 || absListView.getChildAt(absListView.getChildCount() - 1)
.getBottom() < absListView.getPaddingBottom());
} else {
return ViewCompat.canScrollVertically(mTargetView, 1) || mTargetView.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTargetView, 1);
}
}
public boolean shouldHandleAbsListViewLoadingMore(AbsListView absListView) {
if (mIsLoadingMore || mCurrentRefreshStatus == RefreshStatus.REFRESHING || mLoadMoreFooterView == null || mDelegate == null || absListView == null || absListView.getAdapter() == null || absListView.getAdapter().getCount() == 0) {
return false;
}
int lastChildBottom = 0;
if (absListView.getChildCount() > 0) {
// 如果AdapterView的子控件数量不为0,获取最后一个子控件的bottom
lastChildBottom = absListView.getChildAt(absListView.getChildCount() - 1).getBottom();
}
return absListView.getLastVisiblePosition() == absListView.getAdapter().getCount() - 1 && lastChildBottom <= absListView.getMeasuredHeight();
}
/**
* 判断View是否可以下拉
* @return canChildScrollDown
*/
public boolean canChildScrollDown() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (mTargetView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTargetView;
return absListView.getChildCount() > 0 && absListView.getAdapter() != null
&& (absListView.getLastVisiblePosition() < absListView.getAdapter().getCount() - 1 || absListView.getChildAt(absListView.getChildCount() - 1)
.getBottom() < absListView.getPaddingBottom());
} else {
return ViewCompat.canScrollVertically(mTargetView, 1) || mTargetView.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTargetView, 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);
}
}
@Override
public void onScroll(AbsListView listView, int firstVisible, int visibleItemCount, int totalItemCount) {
if (!pickerInitialized || !selectorAnimationsEnabled) return;
int selectorPosY = cellHeight * cellsBeforeSelector;
int applyingRangeY = cellHeight;
for (int i = 0; i < listView.getChildCount(); i++) {
// Exclude elements that does not need to edit
if (!(listView.getChildAt(i) instanceof FrameLayout))
continue;
ViewGroup itemRoot = (ViewGroup) listView.getChildAt(i);
float deviation = 2f;
if (itemRoot.getTop() > selectorPosY + applyingRangeY * deviation || itemRoot.getTop() < selectorPosY - applyingRangeY * deviation)
continue;
View cellContent = itemRoot.getChildAt(0);
// Edit elements regarding to their position from selector
float dy = Math.abs(itemRoot.getTop() - selectorPosY);
if (!selectorAnimationCenterPivot) {
cellContent.setPivotX(0);
cellContent.setPivotY(cellContent.getHeight() / 2);
}
// Scale and "3d effect" for big scale factors on API>=LOLLIPOP
if (dy <= applyingRangeY) {
float k1 = 1 - (dy / applyingRangeY);
float scale = 1 + scaleFactorDelta * k1;
cellContent.setScaleX(scale);
cellContent.setScaleY(scale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
itemRoot.setZ((dy <= applyingRangeY / 2) ? 2 : 1);
} else {
cellContent.setScaleX(1f);
cellContent.setScaleY(1f);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
itemRoot.setZ(0);
}
}
}