RecyclerView- 특정 위치에서 항목 위로 부드럽게 스크롤하는 방법은 무엇입니까?
RecyclerView에서 다음을 사용하여 선택한 항목의 맨 위로 스크롤 할 수 있습니다.
((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
그러나 갑자기 항목을 맨 위 위치로 이동합니다. 매끄럽게 항목의 맨 위로 이동하고 싶습니다 .
나는 또한 시도했다 :
recyclerView.smoothScrollToPosition(position);
그러나 항목을 맨 위로 선택한 위치로 이동하지 않기 때문에 잘 작동하지 않습니다. 위치의 항목이 표시 될 때까지 목록을 스크롤 할뿐입니다.
RecyclerView
확장 가능하게 설계 되었더라도 스크롤을 수행하기 위해 LayoutManager
( droidev가 제안한대로 ) 하위 클래스를 만들 필요가 없습니다 .
대신 SmoothScroller
기본 설정을 만드 십시오 SNAP_TO_START
.
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
이제 스크롤하려는 위치를 설정합니다.
smoothScroller.setTargetPosition(position);
SmoothScroller를 LayoutManager에 전달합니다.
layoutManager.startSmoothScroll(smoothScroller);
LayoutManager를 이용하여 사용자 정의합니다.
public class LinearLayoutManagerWithSmoothScroller extends LinearLayoutManager {
public LinearLayoutManagerWithSmoothScroller(Context context) {
super(context, VERTICAL, false);
}
public LinearLayoutManagerWithSmoothScroller(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class TopSnappedSmoothScroller extends LinearSmoothScroller {
public TopSnappedSmoothScroller(Context context) {
super(context);
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return LinearLayoutManagerWithSmoothScroller.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}
RecyclerView를 사용하고 smoothScrollToPosition을 호출하십시오.
예 :
recyclerView.setLayoutManager(new LinearLayoutManagerWithSmoothScroller(context));
recyclerView.smoothScrollToPosition(position);
지정된 위치의 RecyclerView 항목의 맨 위로 스크롤됩니다.
도움이 되셨기를 바랍니다.
이렇게해볼 수 있어요
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,new RecyclerView.State(), recyclerView.getAdapter().getItemCount());
이것은 RecyclerView에서 직접 호출하기 위해 Kotlin에서 기능 확장 기능입니다 (@Paul Woitaschek 답변 기반).
fun RecyclerView.smoothSnapToPosition(position: Int, snapMode: Int = LinearSmoothScroller.SNAP_TO_START) {
val smoothScroller = object: LinearSmoothScroller(this.context) {
override fun getVerticalSnapPreference(): Int {
return snapMode
}
override fun getHorizontalSnapPreference(): Int {
return snapMode
}
}
smoothScroller.targetPosition = position
layoutManager?.startSmoothScroll(smoothScroller)
}
다음과 같이 사용하십시오.
myRecyclerView.smoothSnapToPosition(itemPosition)
스크롤하는 가장 쉬운 방법 RecyclerView
은 다음과 가능합니다.
// Define the Index we wish to scroll to.
final int lIndex = 0;
// Assign the RecyclerView's LayoutManager.
this.getRecyclerView().setLayoutManager(this.getLinearLayoutManager());
// Scroll the RecyclerView to the Index.
this.getLinearLayoutManager().smoothScrollToPosition(this.getRecyclerView(), new RecyclerView.State(), lIndex);
LinearSmoothScroller에서 calculateDyToMakeVisible / calculateDxToMakeVisible 함수를 재정적으로 배치 할 Y / X 위치를 구현합니다.
override fun calculateDyToMakeVisible(view: View, snapPreference: Int): Int {
return super.calculateDyToMakeVisible(view, snapPreference) - ConvertUtils.dp2px(10f)
}
나는 이렇게 사용했다 :
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, new RecyclerView.State(), 5);
아마도 @droidev 접근 방식이 올바른 방법이지만 기본적으로 동일한 작업을 수행하고 LayoutManager의 확장이 필요하지 않은 약간의 것을 게시하고 싶습니다.
여기에 있는 참고 사항-항목 (목록 상단에서 스크롤하려는 항목)이 화면에 표시되고 자동으로 상단으로 스크롤하려는 경우 잘 작동합니다. 목록의 마지막 항목에 동일한 목록에 새 항목을 추가하는 작업이 있고 새로 추가 된 항목에 사용자를 집중하려는 경우에 유용합니다.
int recyclerViewTop = recyclerView.getTop();
int positionTop = recyclerView.findViewHolderForAdapterPosition(positionToScroll) != null ? recyclerView.findViewHolderForAdapterPosition(positionToScroll).itemView.getTop() : 200;
final int calcOffset = positionTop - recyclerViewTop;
//then the actual scroll is gonna happen with (x offset = 0) and (y offset = calcOffset)
recyclerView.scrollBy(0, offset);
아이디어는 간단합니다. 1. recyclerview 요소의 최상위 좌표를 가져와야합니다. 2. 맨 위로 스크롤하려는 뷰 항목의 맨 위 좌표를 가져와야합니다. 3. 계산 된 오프셋으로 마지막에해야 할 일
recyclerView.scrollBy(0, offset);
200은 뷰 홀더 항목이 존재하지 않는 경우에도 사용할 수있는 하드 코딩 된 정수 값의 예일뿐입니다.
'IT' 카테고리의 다른 글
Android 휴대 전화 애플리케이션을 세로 모드로 잠금 (0) | 2020.08.24 |
---|---|
Intellij에서 생성 된 버전 UID를 생성하는 방법 (0) | 2020.08.24 |
FragmentPagerAdapter를 사용할 때 기존의 조각을 가져 오는 방법 (0) | 2020.08.24 |
Alpine Linux에 Pillow를 사용할 때 디렉토리 디렉토리 "limits.h"없음 (0) | 2020.08.24 |
Yii Framework에 CSS, javascript 파일 포함 (0) | 2020.08.24 |