下面列出了android.widget.ListView#getLastVisiblePosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static RowViewInfo getCurrentRowViewInfo(FileListViewFragment host, Object item)
{
if(host == null || host.isRemoving() || !host.isResumed())
return null;
ListView list = host.getListView();
if(list == null)
return null;
int start = list.getFirstVisiblePosition();
for(int i=start, j=list.getLastVisiblePosition();i<=j;i++)
if(j<list.getCount() && item == list.getItemAtPosition(i))
{
RowViewInfo rvi = new RowViewInfo();
rvi.view = list.getChildAt(i-start);
rvi.position = i;
rvi.listView = list;
return rvi;
}
return null;
}
public void selectItem(int num) {
final ListView commitList = (ListView) getView().findViewById(R.id.git_history_commit_list);
selectedItem = num;
int selection = num - commitList.getFirstVisiblePosition();
//Keep the selected commit on screen... with a little bit of breathing room
if (num < commitList.getFirstVisiblePosition() + 2) {
commitList.setSelection(num == 0 ? num : num - 1);
} else if (num > commitList.getLastVisiblePosition() - 2) {
commitList.setSelection(num == commitList.getCount() - 1 ? num : num + 1);
}
for (int i = 0; i < commitList.getCount(); i ++) {
View child = commitList.getChildAt(i);
if (child != null) {
child.setBackgroundColor(selection == i
? getResources().getColor(R.color.holo_select)
: getResources().getColor(android.R.color.transparent));
}
}
}
private void onMsgRecv(MessageEntity entity) {
logger.d("message_activity#onMsgRecv");
imService.getUnReadMsgManager().ackReadMsg(entity);
logger.d("chat#start pushList");
pushList(entity);
ListView lv = lvPTR.getRefreshableView();
if (lv != null) {
if (lv.getLastVisiblePosition() < adapter.getCount()) {
textView_new_msg_tip.setVisibility(View.VISIBLE);
} else {
scrollToBottomListItem();
}
}
}
public static void ensureVisible(@Nullable ListView listView, int pos) {
if (listView == null || listView.getAdapter() == null) {
return;
}
if (pos < 0 || pos >= listView.getAdapter().getCount()) {
return;
}
int first = listView.getFirstVisiblePosition();
int last = listView.getLastVisiblePosition();
if (pos < first) {
listView.setSelection(pos);
return;
}
if (pos >= last) {
listView.setSelection(1 + pos - (last - first));
}
}
void onWordListClicked(final View v) {
// Note : v is the preference view
final ViewParent parent = v.getParent();
// Just in case something changed in the framework, test for the concrete class
if (!(parent instanceof ListView)) return;
final ListView listView = (ListView)parent;
final int indexToOpen;
// Close all first, we'll open back any item that needs to be open.
final boolean wasOpen = mInterfaceState.isOpen(mWordlistId);
mInterfaceState.closeAll();
if (wasOpen) {
// This button being shown. Take note that we don't want to open any button in the
// loop below.
indexToOpen = -1;
} else {
// This button was not being shown. Open it, and remember the index of this
// child as the one to open in the following loop.
mInterfaceState.setOpen(mWordlistId, mStatus);
indexToOpen = listView.indexOfChild(v);
}
final int lastDisplayedIndex =
listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
// The "lastDisplayedIndex" is actually displayed, hence the <=
for (int i = 0; i <= lastDisplayedIndex; ++i) {
final ButtonSwitcher buttonSwitcher = (ButtonSwitcher)listView.getChildAt(i)
.findViewById(R.id.wordlist_button_switcher);
if (i == indexToOpen) {
buttonSwitcher.setStatusAndUpdateVisuals(getButtonSwitcherStatus(mStatus));
} else {
buttonSwitcher.setStatusAndUpdateVisuals(ButtonSwitcher.STATUS_NO_BUTTON);
}
}
}
/**
* 判断 ListView 是否已经滚动到底部
*
* @param listView 需要被判断的 ListView
* @return
*/
public static boolean isListViewAlreadyAtBottom(ListView listView) {
if (listView.getAdapter() == null || listView.getHeight() == 0) {
return false;
}
if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1) {
View lastItemView = listView.getChildAt(listView.getChildCount() - 1);
if (lastItemView != null && lastItemView.getBottom() == listView.getHeight()) {
return true;
}
}
return false;
}
private void updateRowView(ListView lv, int pos)
{
int start = lv.getFirstVisiblePosition();
if(pos >= start && pos <= lv.getLastVisiblePosition())
{
View view = lv.getChildAt(pos - start);
lv.getAdapter().getView(pos, view, lv);
}
}
void onWordListClicked(final View v) {
// Note : v is the preference view
final ViewParent parent = v.getParent();
// Just in case something changed in the framework, test for the concrete class
if (!(parent instanceof ListView)) return;
final ListView listView = (ListView)parent;
final int indexToOpen;
// Close all first, we'll open back any item that needs to be open.
final boolean wasOpen = mInterfaceState.isOpen(mWordlistId);
mInterfaceState.closeAll();
if (wasOpen) {
// This button being shown. Take note that we don't want to open any button in the
// loop below.
indexToOpen = -1;
} else {
// This button was not being shown. Open it, and remember the index of this
// child as the one to open in the following loop.
mInterfaceState.setOpen(mWordlistId, mStatus);
indexToOpen = listView.indexOfChild(v);
}
final int lastDisplayedIndex =
listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
// The "lastDisplayedIndex" is actually displayed, hence the <=
for (int i = 0; i <= lastDisplayedIndex; ++i) {
final ButtonSwitcher buttonSwitcher = (ButtonSwitcher)listView.getChildAt(i)
.findViewById(R.id.wordlist_button_switcher);
if (i == indexToOpen) {
buttonSwitcher.setStatusAndUpdateVisuals(getButtonSwitcherStatus(mStatus));
} else {
buttonSwitcher.setStatusAndUpdateVisuals(ButtonSwitcher.STATUS_NO_BUTTON);
}
}
}
public static void smoothScrollToPositionCompat(ListView listView,
int position, int offset) {
if (android.os.Build.VERSION.SDK_INT >= 11) {
listView.smoothScrollToPositionFromTop(position, offset);
}
{
int firstVisible = listView.getFirstVisiblePosition();
int lastVisible = listView.getLastVisiblePosition();
if (position < firstVisible)
listView.smoothScrollToPosition(position);
else
listView.smoothScrollToPosition(position + lastVisible
- firstVisible - 2);
}
}
/**
* 判断 ListView 是否已经滚动到底部
*
* @param listView 需要被判断的 ListView
* @return
*/
public static boolean isListViewAlreadyAtBottom(ListView listView) {
if (listView.getAdapter() == null || listView.getHeight() == 0) {
return false;
}
if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1) {
View lastItemView = listView.getChildAt(listView.getChildCount() - 1);
if (lastItemView != null && lastItemView.getBottom() == listView.getHeight()) {
return true;
}
}
return false;
}
/**
* Notifies the menu that the contents of the menu item specified by {@code menuRowId} have
* changed. This should be called if icons, titles, etc. are changing for a particular menu
* item while the menu is open.
* @param menuRowId The id of the menu item to change. This must be a row id and not a child
* id.
*/
public void menuItemContentChanged(int menuRowId) {
// Make sure we have all the valid state objects we need.
if (mAdapter == null || mMenu == null || mPopup == null || mPopup.getListView() == null) {
return;
}
// Calculate the item index.
int index = -1;
int menuSize = mMenu.size();
for (int i = 0; i < menuSize; i++) {
if (mMenu.getItem(i).getItemId() == menuRowId) {
index = i;
break;
}
}
if (index == -1) return;
// Check if the item is visible.
ListView list = mPopup.getListView();
int startIndex = list.getFirstVisiblePosition();
int endIndex = list.getLastVisiblePosition();
if (index < startIndex || index > endIndex) return;
// Grab the correct View.
View view = list.getChildAt(index - startIndex);
if (view == null) return;
// Cause the Adapter to re-populate the View.
list.getAdapter().getView(index, view, list);
}
/**
* Notifies the menu that the contents of the menu item specified by {@code menuRowId} have
* changed. This should be called if icons, titles, etc. are changing for a particular menu
* item while the menu is open.
* @param menuRowId The id of the menu item to change. This must be a row id and not a child
* id.
*/
public void menuItemContentChanged(int menuRowId) {
// Make sure we have all the valid state objects we need.
if (mAdapter == null || mMenu == null || mPopup == null || mPopup.getListView() == null) {
return;
}
// Calculate the item index.
int index = -1;
int menuSize = mMenu.size();
for (int i = 0; i < menuSize; i++) {
if (mMenu.getItem(i).getItemId() == menuRowId) {
index = i;
break;
}
}
if (index == -1) return;
// Check if the item is visible.
ListView list = mPopup.getListView();
int startIndex = list.getFirstVisiblePosition();
int endIndex = list.getLastVisiblePosition();
if (index < startIndex || index > endIndex) return;
// Grab the correct View.
View view = list.getChildAt(index - startIndex);
if (view == null) return;
// Cause the Adapter to re-populate the View.
list.getAdapter().getView(index, view, list);
}
private boolean hasHitBottom() {
if (mMainView instanceof ScrollView) {
ScrollView scrollView = (ScrollView) mMainView;
View view = scrollView.getChildAt(scrollView.getChildCount() - 1);
int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));// Calculate the scrolldiff
return diff == 0;
} else if (mMainView instanceof ListView) {
ListView listView = (ListView) mMainView;
if (listView.getAdapter() != null) {
if (listView.getAdapter().getCount() > 0) {
return listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1 &&
listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight();
}
}
} else if (mMainView instanceof RecyclerView) {
RecyclerView recyclerView = (RecyclerView) mMainView;
if (recyclerView.getAdapter() != null && recyclerView.getLayoutManager() != null) {
RecyclerView.Adapter adapter = recyclerView.getAdapter();
if (adapter.getItemCount() > 0) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
return linearLayoutManager.findLastCompletelyVisibleItemPosition() == adapter.getItemCount() - 1;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
int[] checks = staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(null);
for (int check : checks) {
if (check == adapter.getItemCount() - 1)
return true;
}
}
}
}
}
return false;
}
private ScrollState getScrollPosition() {
final ListView listView = this.binding == null ? null : this.binding.messagesView;
if (listView == null || listView.getCount() == 0 || listView.getLastVisiblePosition() == listView.getCount() - 1) {
return null;
} else {
final int pos = listView.getFirstVisiblePosition();
final View view = listView.getChildAt(0);
if (view == null) {
return null;
} else {
return new ScrollState(pos, view.getTop());
}
}
}
/**
* Notifies the menu that the contents of the menu item specified by {@code menuRowId} have
* changed. This should be called if icons, titles, etc. are changing for a particular menu
* item while the menu is open.
* @param menuRowId The id of the menu item to change. This must be a row id and not a child
* id.
*/
public void menuItemContentChanged(int menuRowId) {
// Make sure we have all the valid state objects we need.
if (mAdapter == null || mMenu == null || mPopup == null || mPopup.getListView() == null) {
return;
}
// Calculate the item index.
int index = -1;
int menuSize = mMenu.size();
for (int i = 0; i < menuSize; i++) {
if (mMenu.getItem(i).getItemId() == menuRowId) {
index = i;
break;
}
}
if (index == -1) return;
// Check if the item is visible.
ListView list = mPopup.getListView();
int startIndex = list.getFirstVisiblePosition();
int endIndex = list.getLastVisiblePosition();
if (index < startIndex || index > endIndex) return;
// Grab the correct View.
View view = list.getChildAt(index - startIndex);
if (view == null) return;
// Cause the Adapter to re-populate the View.
list.getAdapter().getView(index, view, list);
}
@Override
public void onSaveInstanceState(@NonNull final Bundle outState) {
super.onSaveInstanceState(outState);
// Save the last log list view scroll position
final ListView list = getListView();
final boolean scrolledToBottom = list.getCount() > 0 && list.getLastVisiblePosition() == list.getCount() - 1;
outState.putInt(SIS_LOG_SCROLL_POSITION, scrolledToBottom ? LOG_SCROLLED_TO_BOTTOM : list.getFirstVisiblePosition());
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
ListView listView = getListView();
int count = listView.getCheckedItemCount();
mode.setTitle(getResources().getQuantityString(R.plurals.itemsSelected, count, count));
// Update (redraw) list item view
int start = listView.getFirstVisiblePosition();
for (int i = start, j = listView.getLastVisiblePosition(); i <= j; i++) {
if (position == i) {
View view = listView.getChildAt(i - start);
listView.getAdapter().getView(i, view, listView);
break;
}
}
}
public static boolean isLastMessageVisible(ListView messageListView) {
if (messageListView == null || messageListView.getAdapter() == null) {
return false;
}
if (messageListView.getLastVisiblePosition() >= messageListView.getAdapter().getCount() - 1 - messageListView.getFooterViewsCount()) {
return true;
} else {
return false;
}
}
public static Object getViewHolderByIndex(ListView listView, int index) {
int firstVisibleFeedPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount();
int lastVisibleFeedPosition = listView.getLastVisiblePosition() - listView.getHeaderViewsCount();
//只有获取可见区域的
if (index >= firstVisibleFeedPosition && index <= lastVisibleFeedPosition) {
View view = listView.getChildAt(index - firstVisibleFeedPosition);
Object tag = view.getTag();
return tag;
} else {
return null;
}
}
@Override
boolean isShownInList(ListView list, int pos) {
return pos >= list.getFirstVisiblePosition() && pos <= list.getLastVisiblePosition();
}