Java Code Examples for androidx.recyclerview.widget.RecyclerView#smoothScrollToPosition()

The following examples show how to use androidx.recyclerview.widget.RecyclerView#smoothScrollToPosition() . 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: BackToTopUtils.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void scrollToTop(RecyclerView recyclerView) {
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    int firstVisibleItemPosition = 0;
    if (manager instanceof LinearLayoutManager) {
        firstVisibleItemPosition = getFirstVisibleItemPosition((LinearLayoutManager) manager);
    } else if (manager instanceof StaggeredGridLayoutManager) {
        firstVisibleItemPosition = getFirstVisibleItemPosition((StaggeredGridLayoutManager) manager);
    }
    if (firstVisibleItemPosition > 5) {
        recyclerView.scrollToPosition(5);
    }
    recyclerView.smoothScrollToPosition(0);

    MysplashActivity activity = MysplashApplication.getInstance().getTopActivity();
    if (activity != null) {
        ComponentFactory.getSettingsService().notifySetBackToTop(activity);
    }
}
 
Example 2
Source File: SearchResultFragment.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
@Override public void scrollToTop() {
  RecyclerView list;
  if (followedStoresResultList.getVisibility() == View.VISIBLE) {
    list = followedStoresResultList;
  } else {
    list = allStoresResultList;
  }
  LinearLayoutManager layoutManager = ((LinearLayoutManager) list.getLayoutManager());
  int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
  if (lastVisibleItemPosition > 10) {
    list.scrollToPosition(10);
  }
  list.smoothScrollToPosition(0);
}
 
Example 3
Source File: MyStoresFragment.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
@Override @UiThread public void scrollToTop() {
  RecyclerView view = getRecyclerView();
  LinearLayoutManager layoutManager = ((LinearLayoutManager) view.getLayoutManager());
  int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
  if (lastVisibleItemPosition > 10) {
    view.scrollToPosition(10);
  }
  view.smoothScrollToPosition(0);
}
 
Example 4
Source File: UserActivityTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Config(shadows = ShadowRecyclerView.class)
@Test
public void testScrollToTop() {
    verify(userManager).getUser(eq("username"), userCaptor.capture());
    userCaptor.getValue().onResponse(user);
    RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.recycler_view);
    recyclerView.smoothScrollToPosition(1);
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(1);
    TabLayout.Tab tab = ((TabLayout) activity.findViewById(R.id.tab_layout)).getTabAt(0);
    tab.select();
    tab.select();
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(0);
}
 
Example 5
Source File: MainActivityTest.java    From meat-grinder with MIT License 4 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
    RecyclerView recyclerView = (RecyclerView) view;
    recyclerView.smoothScrollToPosition(position);
}
 
Example 6
Source File: ItemActivityTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Config(shadows = ShadowRecyclerView.class)
@Test
public void testScrollToTop() {
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() {
        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }

        @Override
        public String getId() {
            return "1";
        }

        @Override
        public boolean isStoryType() {
            return true;
        }

        @Override
        public int getKidCount() {
            return 10;
        }

        @Override
        public String getUrl() {
            return "http://example.com";
        }
    });
    controller = Robolectric.buildActivity(ItemActivity.class, intent);
    controller.create().start().resume();
    activity = controller.get();
    // see https://github.com/robolectric/robolectric/issues/1326
    ShadowLooper.pauseMainLooper();
    controller.visible();
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    RecyclerView recyclerView = activity.findViewById(R.id.recycler_view);
    recyclerView.smoothScrollToPosition(1);
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(1);
    TabLayout tabLayout = activity.findViewById(R.id.tab_layout);
    assertThat(tabLayout.getTabCount()).isEqualTo(2);
    tabLayout.getTabAt(1).select();
    tabLayout.getTabAt(0).select();
    tabLayout.getTabAt(0).select();
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(0);
}