본문 바로가기

개발 관련 이야기/안드로이드앱 개발

안드로이드 RecyclerView 에서 scrollToPosition()사용 예제 소스

https://github.com/android/views-widgets-samples/blob/master/RecyclerView/Application/src/main/java/com/example/android/recyclerview/RecyclerViewFragment.java

 

android/views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android. - android/views-widgets-samples

github.com

 

https://developer.android.com/guide/topics/ui/layout/recyclerview?hl=ko#java

 

RecyclerView로 목록 만들기  |  Android 개발자  |  Android Developers

RecyclerView를 사용하여 동적 콘텐츠의 목록과 그리드를 표시합니다.

developer.android.com

 

smoothScroller: stackoverflow.com/questions/52964701/how-to-make-auto-smooth-scroll-with-offset-recycler-view

 

How to make auto smooth scroll with offset Recycler View?

There is such issue, I have horizontal RecyclerView where each cell less than width of screen. So I found a solution here RecyclerVIew auto scroll to display all the elements as in News Feed et...

stackoverflow.com

SmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

  @Override protected int getVerticalSnapPreference() {

       return LinearSmoothScroller.SNAP_TO_ANY;

   }

 

  @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {

      return 120f / displayMetrics.densityDpi;

  }

}; .

..

smoothScroller.setTargetPosition(position);

recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);

 

수평, 수직 형태의 RecyclerView에 따라 getHorizontalSnapPreference(), getVerticalSnapPreference() 를 오버라이딩 하여 scroll시 Item의 margin 위치에 따라 시작, 끝 기준을 정하여 구현해주면 된다.

 


출처: https://kimch3617.tistory.com/entry/RecyclerView의-scrollToPosition나-smoothScrollToPosition-사용-시-Item사이의-margin이-있는-경우

 

RecyclerView의 scrollToPosition나 smoothScrollToPosition 사용 시 Item사이의 margin이 있는 경우

RecyclerView의 scrollToPosition나 smoothScrollToPosition 을 사용할 경우 각각의 Item 사이의 margin이 존재하여 정확한 위치에 스크롤 되지 않고 Margin만큼 벌어져서 보여지게 되는 경우가 있다. val smoothS..

kimch3617.tistory.com