Search in sources :

Example 1 with InfiniteScrollAdapter

use of com.yarolegovich.discretescrollview.InfiniteScrollAdapter in project DiscreteScrollView by yarolegovich.

the class DiscreteScrollViewOptions method smoothScrollToUserSelectedPosition.

public static void smoothScrollToUserSelectedPosition(final DiscreteScrollView scrollView, View anchor) {
    PopupMenu popupMenu = new PopupMenu(scrollView.getContext(), anchor);
    Menu menu = popupMenu.getMenu();
    final RecyclerView.Adapter<?> adapter = scrollView.getAdapter();
    int itemCount = (adapter instanceof InfiniteScrollAdapter) ? ((InfiniteScrollAdapter<?>) adapter).getRealItemCount() : (adapter != null ? adapter.getItemCount() : 0);
    for (int i = 0; i < itemCount; i++) {
        menu.add(String.valueOf(i + 1));
    }
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int destination = Integer.parseInt(String.valueOf(item.getTitle())) - 1;
            if (adapter instanceof InfiniteScrollAdapter) {
                destination = ((InfiniteScrollAdapter<?>) adapter).getClosestPosition(destination);
            }
            scrollView.smoothScrollToPosition(destination);
            return true;
        }
    });
    popupMenu.show();
}
Also used : InfiniteScrollAdapter(com.yarolegovich.discretescrollview.InfiniteScrollAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) MenuItem(android.view.MenuItem) PopupMenu(androidx.appcompat.widget.PopupMenu) Menu(android.view.Menu) PopupMenu(androidx.appcompat.widget.PopupMenu)

Aggregations

Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 PopupMenu (androidx.appcompat.widget.PopupMenu)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 InfiniteScrollAdapter (com.yarolegovich.discretescrollview.InfiniteScrollAdapter)1