Java Code Examples for android.widget.listview#getLastVisiblePosition()

The following examples show how to use android.widget.listview#getLastVisiblePosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UiUtil.java    From android-file-chooser with Apache License 2.0 6 votes vote down vote up
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));
    }
}
 
Example 2
Source File: FsBrowserRecord.java    From edslite with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: GitHistoryActivity.java    From APDE with GNU General Public License v2.0 6 votes vote down vote up
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));
		}
	}
}
 
Example 4
Source File: MessageActivity.java    From sctalk with Apache License 2.0 6 votes vote down vote up
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();
        }
    }
}
 
Example 5
Source File: AppMenu.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: ListViewUtil.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
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;
    }
}
 
Example 7
Source File: ListViewUtil.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
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;
    }
}
 
Example 8
Source File: DataList.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@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;
        }
    }
}
 
Example 9
Source File: UARTLogFragment.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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());
}
 
Example 10
Source File: AppMenu.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: ConversationFragment.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
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());
        }
    }
}
 
Example 12
Source File: BounceTouchListener.java    From Bounce with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: WordListPreference.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 14
Source File: AppMenu.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: ViewHelper.java    From DMusic with Apache License 2.0 5 votes vote down vote up
/**
 * 判断 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;
}
 
Example 16
Source File: AutoScrollListView.java    From Android-Next with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 17
Source File: WordListPreference.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 18
Source File: LocationListBaseFragment.java    From edslite with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 19
Source File: ViewHelper.java    From Common with Apache License 2.0 5 votes vote down vote up
/**
 * 判断 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;
}
 
Example 20
Source File: FromListViewListener.java    From GestureViews with Apache License 2.0 4 votes vote down vote up
@Override
boolean isShownInList(ListView list, int pos) {
    return pos >= list.getFirstVisiblePosition() && pos <= list.getLastVisiblePosition();
}