선택된 항목을 맨 위에 표시하려면 RecyclerView를 스크롤하십시오.
RecyclerView
선택한 항목을 맨 위에 표시 하기 위해 a를 스크롤하는 방법을 찾고 있습니다.
A의 ListView
내가 사용하여 해당 작업을 수행 할 수 있었다 scrollTo(x,y)
및 필요를 중심으로 될 수있는 요소의 상단을 받고.
다음과 같은 것 :
@Override
public void onItemClick(View v, int pos){
mylistView.scrollTo(0, v.getTop());
}
문제는 메서드를 RecyclerView
사용할 때 오류가 반환 된다는 scrollTo
것입니다.
RecyclerView는 절대 위치로 스크롤을 지원하지 않습니다
a RecyclerView
를 스크롤 하여 선택한 항목을보기 상단에 놓을 수 있습니까?
LinearLayoutManager
또는 Staggered를 사용하는 경우 GridLayoutManager
각각에는 scrollToPositionWithOffset 메소드가 있으며,이 위치는 시작 위치에서 항목 시작 위치와 위치 오프셋을 가져옵니다. RecyclerView
필요한 것을 달성하는 것처럼 보입니다 (오프셋을 0으로 설정) 상단에 맞춰야합니다).
예를 들어 :
//Scroll item 2 to 20 pixels from the top
linearLayoutManager.scrollToPositionWithOffset(2, 20);
수직 LinearLayout Manager를 찾고 있다면 사용자 정의를 사용하여 부드러운 스크롤을 얻을 수 있습니다 LinearSmoothScroller
.
import android.content.Context;
import android.graphics.PointF;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
public class SnappingLinearLayoutManager extends LinearLayoutManager {
public SnappingLinearLayoutManager(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 SnappingLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}
재활용보기에서 레이아웃 관리자의 인스턴스를 사용하면 호출 recyclerView.smoothScrollToPosition(pos);
하면 선택한 위치로 부드럽게 스크롤하여 재활용보기 상단으로 이동합니다.
// 항목 위치 스크롤
linearLayoutManager.scrollToPositionWithOffset(pos, 0);
전화하면 recyclerview.scrollToPosition(position)
됩니다. 괜찮아!
어댑터에서 호출하려면 어댑터에 메소드 getRecyclerview()
를 구현하는 것보다 어댑터에 recyclerview의 인스턴스 또는 recyclerview를 포함하는 활동 또는 단편을 갖도록하십시오.
나는 그것이 당신을 도울 수 있기를 바랍니다.
스피드 레귤레이터와 동일
public class SmoothScrollLinearLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 110f;
private Context mContext;
public SmoothScrollLinearLayoutManager(Context context,int orientation, boolean reverseLayout) {
super(context,orientation,reverseLayout);
mContext = context;
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext()){
//This controls the direction in which smoothScroll looks for your view
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return new PointF(0, 1);
}
//This returns the milliseconds it takes to scroll one pixel.
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class TopSnappedSmoothScroller extends LinearSmoothScroller {
public TopSnappedSmoothScroller(Context context) {
super(context);
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SmoothScrollLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}
나를 위해 일한 것을 시원하게 해보십시오!
변수 만들기 private static int displayedposition = 0;
이제 활동에서 RecyclerView의 위치를 찾으십시오.
myRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager llm = (LinearLayoutManager) myRecyclerView.getLayoutManager();
displayedposition = llm.findFirstVisibleItemPosition();
}
});
이 문장을 원하는 곳에 배치하여 이전 사이트를보기에 표시하십시오.
LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
llm.scrollToPositionWithOffset(displayedposition , youList.size());
글쎄, 그것은 나를 위해 잘 작동 \ o /
버튼을 클릭하면 RecyclerView를 새로 고친 후 스크롤 위치를 복원하기 위해 수행 한 작업 :
if (linearLayoutManager != null) {
index = linearLayoutManager.findFirstVisibleItemPosition();
View v = linearLayoutManager.getChildAt(0);
top = (v == null) ? 0 : (v.getTop() - linearLayoutManager.getPaddingTop());
Log.d("TAG", "visible position " + " " + index);
}
else{
index = 0;
}
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.scrollToPositionWithOffset(index, top);
linearLayoutManager 객체를 생성하기 전에 맨 처음부터 보이는 항목의 오프셋을 가져 와서 인스턴스화 한 후 LinearLayoutManager 객체의 scrollToPositionWithOffset을 호출했습니다.
scroll at particular position
and this helped me alot. by click listener you can get the position in your adapter
layoutmanager.scrollToPosition(int position);
In my case my RecyclerView
have a padding top like this
<android.support.v7.widget.RecyclerView
...
android:paddingTop="100dp"
android:clipToPadding="false"
/>
Then for scroll a item to top, I need to
recyclerViewLinearLayoutManager.scrollToPositionWithOffset(position, -yourRecyclerView.getPaddingTop());
I don't know why I didn't find the best answer but its really simple.
recyclerView.smoothScrollToPosition(position);
No errors
Creates Animations
just call this method simply:
((LinearLayoutManager)recyclerView.getLayoutManager()).scrollToPositionWithOffset(yourItemPosition,0);
instead of:
recyclerView.scrollToPosition(yourItemPosition);
If you want to scroll automatic without show scroll motion then you need to write following code:
**=> mRecyclerView.getLayoutManager().scrollToPosition(position);**
If you want to display scroll motion then you need to add following code.
=>Step 1: You need to declare SmoothScroller.
RecyclerView.SmoothScroller smoothScroller = new
LinearSmoothScroller(this.getApplicationContext()) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
=>step 2: You need to add this code any event you want to perform scroll to specific position.
=>First you need to set target position to SmoothScroller.
**smoothScroller.setTargetPosition(position);**
=>Then you need to set SmoothScroller to LayoutManager.
**mRecyclerView.getLayoutManager().startSmoothScroll(smoothScroller);**
I use the code below to smooth-scroll an item (thisView) to the top.
It works also for GridLayoutManager with views of different heights:
View firstView = mRecyclerView.getChildAt(0);
int toY = firstView.getTop();
int firstPosition = mRecyclerView.getChildAdapterPosition(firstView);
View thisView = mRecyclerView.getChildAt(thisPosition - firstPosition);
int fromY = thisView.getTop();
mRecyclerView.smoothScrollBy(0, fromY - toY);
Seems to work good enough for a quick solution.
참고URL : https://stackoverflow.com/questions/26875061/scroll-recyclerview-to-show-selected-item-on-top
'IT' 카테고리의 다른 글
UITableViewCell은 흰색 배경을 표시하며 iOS7에서 수정할 수 없습니다 (0) | 2020.05.15 |
---|---|
scikit-learn에서 여러 열의 레이블 인코딩 (0) | 2020.05.15 |
SimpleXMLElement 객체로부터 가치 얻기 (0) | 2020.05.15 |
select2 값을 재설정하고 자리 표시 자 표시 (0) | 2020.05.15 |
지도에서 키 조각 가져 오기 (0) | 2020.05.15 |