Moh*_*sif 7 android scroll recycler-adapter android-recyclerview
如何自动RecyclerView平滑滚动,以便用户可以看到RecyclerView的所有元素并从头开始再次滚动 - 如同News Feed等.
我知道smoothScrollToPosition(),scrollToPosition()但他们最终会滚动到最后一个元素.
我希望RecyclerView能够动画并且移动缓慢.
Md *_*ury 19
只是为了改善答案,它是自动滚动无限和平滑动画.
final int speedScroll = 1200;
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
int count = 0;
boolean flag = true;
@Override
public void run() {
if(count < adapter.getItemCount()){
if(count==adapter.getItemCount()-1){
flag = false;
}else if(count == 0){
flag = true;
}
if(flag) count++;
else count--;
recyclerView.smoothScrollToPosition(count);
handler.postDelayed(this,speedScroll);
}
}
};
handler.postDelayed(runnable,speedScroll);
Run Code Online (Sandbox Code Playgroud)
这正是您的答案,但如果链接到更流畅的动画,则使用LayoutManager
recyclerView.setLayoutManager(new CustomLinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
Run Code Online (Sandbox Code Playgroud)
控制动画改变MILLISECONDS_PER_INCH值.
public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager(Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
private static final float MILLISECONDS_PER_INCH = 200f;
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return CustomLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这是最好的解决方案.
final int speedScroll = 150;
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
int count = 0;
@Override
public void run() {
if(count < list.size()){
recyclerView.scrollToPosition(++count);
handler.postDelayed(this,speedScroll);
}
}
};
handler.postDelayed(runnable,speedScroll);
Run Code Online (Sandbox Code Playgroud)
这是自动滚动 RecyclerView及其100%工作的最佳方式:
RecyclerView recyclerView = findViewById(R.id.rv_id);
final int time = 4000; // it's the delay time for sliding between items in recyclerview
final Adapter adapter = new Adapter(dataItems);
recyclerView.setAdapter(adapter);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
//The LinearSnapHelper will snap the center of the target child view to the center of the attached RecyclerView , it's optional if you want , you can use it
final LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(recyclerView);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (linearLayoutManager.findLastCompletelyVisibleItemPosition() < (adapter.getItemCount() - 1)) {
linearLayoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1);
}
else if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == (adapter.getItemCount() - 1)) {
linearLayoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), 0);
}
}
}, 0, time);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12363 次 |
| 最近记录: |