下面列出了android.widget.AbsListView#getAdapter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* get AbsListView height according to every children
*
* @param view
* @return
*/
public static int getAbsListViewHeightBasedOnChildren(AbsListView view) {
ListAdapter adapter;
if (view == null || (adapter = view.getAdapter()) == null) {
return 0;
}
int height = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View item = adapter.getView(i, null, view);
if (item instanceof ViewGroup) {
item.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
item.measure(0, 0);
height += item.getMeasuredHeight();
}
height += view.getPaddingTop() + view.getPaddingBottom();
return height;
}
/**
* get AbsListView height according to every children
*
* @param view
* @return
*/
public static int getAbsListViewHeightBasedOnChildren(AbsListView view) {
ListAdapter adapter;
if (view == null || (adapter = view.getAdapter()) == null) {
return 0;
}
int height = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View item = adapter.getView(i, null, view);
if (item instanceof ViewGroup) {
item.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
item.measure(0, 0);
height += item.getMeasuredHeight();
}
height += view.getPaddingTop() + view.getPaddingBottom();
return height;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
{
if (firstVisibleItem < this.topVisibleItem - DISTANCE_TO_HIDE_ACTIONBAR)
{
setActionbarVisibility(true);
this.topVisibleItem = firstVisibleItem;
}
else if (firstVisibleItem > this.topVisibleItem + DISTANCE_TO_HIDE_ACTIONBAR)
{
setActionbarVisibility(false);
this.topVisibleItem = firstVisibleItem;
}
ListAdapter adapter = view.getAdapter();
if (adapter instanceof HeaderViewListAdapter)
{
HeaderViewListAdapter headerViewListAdapter = (HeaderViewListAdapter) adapter;
if (headerViewListAdapter.getWrappedAdapter() instanceof FileCardAdapter)
{
int startPrefetch = firstVisibleItem + visibleItemCount-headerViewListAdapter.getHeadersCount();
((FileCardAdapter) headerViewListAdapter.getWrappedAdapter()).prefetchImages(startPrefetch, visibleItemCount);
}
}
}
/**
* 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;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (AppConfig.INSTANCE.getBoolean(view.getContext(), AppConfig.Key.SCROLLING_PAUSE_LOAD) && view.getAdapter() != null) {
ListAdapter listAdapter = view.getAdapter();
if (listAdapter instanceof WrapperListAdapter) {
listAdapter = ((WrapperListAdapter) listAdapter).getWrappedAdapter();
}
if (listAdapter instanceof BaseAdapter) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
if (!sketch.getConfiguration().isPauseLoadEnabled()) {
sketch.getConfiguration().setPauseLoadEnabled(true);
}
} else if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
if (sketch.getConfiguration().isPauseLoadEnabled()) {
sketch.getConfiguration().setPauseLoadEnabled(false);
((BaseAdapter) listAdapter).notifyDataSetChanged();
}
}
}
}
if (absListScrollListener != null) {
absListScrollListener.onScrollStateChanged(view, scrollState);
}
}
public static int getAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace) {
int totalHeight = 0;
int w = MeasureSpec.makeMeasureSpec(0, 0);
int h = MeasureSpec.makeMeasureSpec(0, 0);
absListView.measure(w, h);
ListAdapter mListAdapter = (ListAdapter) absListView.getAdapter();
if (mListAdapter == null) {
return 0;
}
int count = mListAdapter.getCount();
View listItem;
if (absListView instanceof ListView) {
for (int i = 0; i < count; i++) {
listItem = mListAdapter.getView(i, null, absListView);
listItem.measure(w, h);
totalHeight += listItem.getMeasuredHeight();
}
if (count == 0) {
totalHeight = verticalSpace;
} else {
totalHeight += ((ListView) absListView).getDividerHeight() * (count - 1);
}
} else if (absListView instanceof GridView) {
int remain = count % lineNumber;
if (remain > 0) {
remain = 1;
}
if (mListAdapter.getCount() == 0) {
totalHeight = verticalSpace;
} else {
listItem = mListAdapter.getView(0, null, absListView);
listItem.measure(w, h);
int line = (count / lineNumber) + remain;
totalHeight = (listItem.getMeasuredHeight() * line) + ((line - 1) * verticalSpace);
}
}
return totalHeight;
}
/**
* 判断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 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;
}
public static void scrollToBottom(final AbsListView absListView) {
if (absListView != null) {
if (absListView.getAdapter() != null && absListView.getAdapter().getCount() > 0) {
absListView.post(new Runnable() {
@Override
public void run() {
absListView.setSelection(absListView.getAdapter().getCount() - 1);
}
});
}
}
}
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;
}
public static void scrollToBottom(final AbsListView absListView) {
if (absListView != null) {
if (absListView.getAdapter() != null && absListView.getAdapter().getCount() > 0) {
absListView.post(new Runnable() {
@Override
public void run() {
absListView.setSelection(absListView.getAdapter().getCount() - 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);
}
}
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);
}
}
/**
* ListView滚动底部是否
* @param absListView
* @return
*/
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();
}
public static void scrollToBottom(final AbsListView absListView) {
if (absListView != null) {
if (absListView.getAdapter() != null && absListView.getAdapter().getCount() > 0) {
absListView.post(new Runnable() {
@Override
public void run() {
absListView.setSelection(absListView.getAdapter().getCount() - 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);
}
}