下面列出了android.widget.AbsListView#getLastVisiblePosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
//Log.v(TAG, "已经停止:SCROLL_STATE_IDLE" + "-->" + view.getCount());
Context context = view.getContext();
SheJiaoMaoApplication sheJiaoMao = (SheJiaoMaoApplication) context.getApplicationContext();
if (view.getLastVisiblePosition() == view.getCount() - 1
&& sheJiaoMao.isAutoLoadMore()) {
view.getChildAt(view.getChildCount() - 1).performClick();
}
displayImage(view);
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
//Log.v(TAG, "SCROLL_STATE_TOUCH_SCROLL:当屏幕滚动且用户使用的触碰或手指还在屏幕上时为1");
break;
case OnScrollListener.SCROLL_STATE_FLING:
//Log.v(TAG, "SCROLL_STATE_FLING:由于用户的操作,屏幕产生惯性滑动时为2");
break;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
//控制上拉加载更多
if (isSuspend && view.getLastVisiblePosition() == (view.getCount() - 1)) {
mListView.setType(UIListView.TYPE_ONLY_UP_LOADMORD);
}else {
mListView.setType(UIListView.TYPE_NONE);
}
break;
default:
break;
}
}
public boolean canChildScrollDown() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
try {
if (absListView.getCount() > 0) {
if (absListView.getLastVisiblePosition() + 1 == absListView.getCount()) {
int lastIndex = absListView.getLastVisiblePosition() - absListView.getFirstVisiblePosition();
return absListView.getChildAt(lastIndex).getBottom() == absListView.getPaddingBottom();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
} else {
return true;
}
} 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);
}
}
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;
}
/**
* This method is by no means accurate, and Will only work to any degree of accuracy if your list items
* are the same height.
* Otherwise it becomes vastly more difficult to calculate the correct height.
*
* @param listView listView to get height of, if no adapter is attached then nothing will happen.
* @return 0 for failure.
*/
public static int calculateApproximateHeight(AbsListView listView) {
final ListAdapter adapter = listView.getAdapter();
int onScreenHeight = 0, totalHeight = 0;
final int totalCount = adapter.getCount();
final int visibleCount = listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
if (totalCount > 0) {
View view;
for (int i = 0; i < visibleCount; i++) {
// final View view = adapter.getView(0, null, listView);
view = listView.getChildAt(i);
// view.measure(
// View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
// View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
onScreenHeight += view.getMeasuredHeight();
}
// Get the average on screen height, then multiply it up.
totalHeight = (onScreenHeight / visibleCount) * totalCount;
// Add the divider height.
if (listView instanceof ListView) {
totalHeight += ((ListView) listView).getDividerHeight() * (totalCount - 1);
}
}
return totalHeight;
}
/**
* This method is by no means accurate, and Will only work to any degree of accuracy if your list items
* are the same height.
* Otherwise it becomes vastly more difficult to calculate the correct height.
*
* @param listView listView to get height of, if no adapter is attached then nothing will happen.
* @return 0 for failure.
*/
public static int calculateApproximateHeight(AbsListView listView) {
final ListAdapter adapter = listView.getAdapter();
int onScreenHeight = 0, totalHeight = 0;
final int totalCount = adapter.getCount();
final int visibleCount = listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
if (totalCount > 0) {
View view;
for (int i = 0; i < visibleCount; i++) {
// final View view = adapter.getView(0, null, listView);
view = listView.getChildAt(i);
// view.measure(
// View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
// View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
onScreenHeight += view.getMeasuredHeight();
}
// Get the average on screen height, then multiply it up.
totalHeight = (onScreenHeight / visibleCount) * totalCount;
// Add the divider height.
if (listView instanceof ListView) {
totalHeight += ((ListView) listView).getDividerHeight() * (totalCount - 1);
}
}
return totalHeight;
}
public boolean canChildScrollDown() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
try {
if (absListView.getCount() > 0) {
if (absListView.getLastVisiblePosition() + 1 == absListView.getCount()) {
int lastIndex = absListView.getLastVisiblePosition() - absListView.getFirstVisiblePosition();
return absListView.getChildAt(lastIndex).getBottom() == absListView.getPaddingBottom();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
} else {
return true;
}
} else {
return ViewCompat.canScrollVertically(mTarget, 1);
}
}
public boolean canChildScrollDown() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
try {
if (absListView.getCount() > 0) {
if (absListView.getLastVisiblePosition() + 1 == absListView.getCount()) {
int lastIndex = absListView.getLastVisiblePosition() - absListView.getFirstVisiblePosition();
return absListView.getChildAt(lastIndex).getBottom() == absListView.getPaddingBottom();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
} else {
return true;
}
} else {
return ViewCompat.canScrollVertically(mTarget, 1);
}
}
public static int getItemIndexAtLocation(AbsListView listView, int y) {
int index = 0;
if (listView.getCount() <= 0)
return index;
int k = listView.getFirstVisiblePosition();
for(int i = k ; i <= listView.getLastVisiblePosition() ; i++) {
View view = listView.getChildAt(i - k);
if (y > view.getTop() && y < view.getBottom() ) {
return index = i;
}
}
return 0;
}
public boolean canChildScrollDown() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
View lastChild = absListView.getChildAt(absListView.getChildCount() - 1);
if (lastChild != null) {
return (absListView.getLastVisiblePosition() == (absListView.getCount() - 1))
&& lastChild.getBottom() > absListView.getPaddingBottom();
}
else
{
return false;
}
} else {
return mTarget.getHeight() - 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);
}
}
public static boolean canScrollDown(View targetView) {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (targetView instanceof AbsListView) {
final AbsListView absListView = (AbsListView) targetView;
return absListView.getChildCount() > 0
&& (absListView.getLastVisiblePosition() < absListView.getChildCount() - 1
|| absListView.getChildAt(absListView.getChildCount() - 1).getBottom() > absListView.getPaddingBottom());
} else {
return targetView.getScrollY() < 0;
}
} else {
return targetView.canScrollVertically(1);
}
}
/**
* 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;
}
private void ifNeedLoad(AbsListView view, int scrollState) {
if (!loadEnable) {
return;
}
try {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& !isLoading
&& view.getLastVisiblePosition() == view
.getPositionForView(footer) && !isLoadFull) {
onLoad();
isLoading = true;
}
} catch (Exception e) {
}
}
public boolean canChildScrollDown() {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
return absListView.getChildCount() > 0
&& absListView.getLastVisiblePosition() == absListView.getAdapter().getCount() - 1;
} else {
return mTarget.getScrollY() < 0;
}
} else {
return ViewCompat.canScrollVertically(mTarget, 1);
}
}
/**
* 判断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);
}
}
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);
}
}
@Override
public
void onScrollStateChanged(AbsListView view, int scrollState)
{
FeedsActivity activity = (FeedsActivity) view.getContext();
if(SCROLL_STATE_TOUCH_SCROLL == scrollState || SCROLL_STATE_IDLE == scrollState)
{
Adapter adapter = view.getAdapter();
int first = view.getFirstVisiblePosition();
int last = view.getLastVisiblePosition();
for(int i = 0; last - first >= i; i++)
{
View viewItem = view.getChildAt(i);
if(null != viewItem && viewItem.isShown() && 0 <= viewItem.getTop())
{
FeedItem item = (FeedItem) adapter.getItem(first + i);
activity.readItem(item.m_time);
}
}
}
if(SCROLL_STATE_IDLE == scrollState)
{
AsyncNavigationAdapter.run(activity);
}
}
public static boolean isAbsListViewToBottom(AbsListView absListView) {
if (absListView != null && absListView.getAdapter() != null && absListView.getChildCount() > 0 && absListView.getLastVisiblePosition() == absListView.getAdapter().getCount() - 1) {
View lastChild = absListView.getChildAt(absListView.getChildCount() - 1);
return lastChild.getBottom() <= absListView.getMeasuredHeight();
}
return false;
}