Android RecyclerVIew自动滚动以显示新闻提要等中的所有元素。,
如何平滑地自动滚动Android RecyclerVIew自动滚动以显示新闻提要等中的所有元素。,,android,scroll,android-recyclerview,Android,Scroll,Android Recyclerview,如何平滑地自动滚动RecyclerView,以便用户可以看到RecyclerView的所有元素,并从一开始就再次滚动-如在新闻提要等中 我知道smoothScrollToPosition()和scrollToPosition(),但它们最终会滚动到最后一个元素 我希望RecyclerView设置动画并缓慢移动。我认为这是最好的解决方案 final int speedScroll = 150; final Handler handler = new Handler(); f
RecyclerView
,以便用户可以看到RecyclerView的所有元素,并从一开始就再次滚动-如在新闻提要等中
我知道smoothScrollToPosition()
和scrollToPosition()
,但它们最终会滚动到最后一个元素
我希望RecyclerView设置动画并缓慢移动。我认为这是最好的解决方案
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);
final int speedScroll=150;
最终处理程序=新处理程序();
final Runnable Runnable=新Runnable(){
整数计数=0;
@凌驾
公开募捐{
如果(计数
只是为了对答案稍加改进,它是一款带有平滑动画的自动无限滚动功能。
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);
控制动画更改每英寸毫秒值
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);
}
}
经过反复尝试后,这对我来说非常适合
final RecyclerView mRecyclerViewr;
final ArrayList<String> stringArrayData = new ArrayList<String>
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerViewr.setLayoutManager(linearLayoutManager);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), topPriceBarList);
mRecyclerViewr.setAdapter(customAdapter);
// Auto Scroll Left To Right
final int scrollSpeed = 100; // Scroll Speed in Milliseconds
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
int x = 15; // Pixels To Move/Scroll
boolean flag = true;
// Find Scroll Position By Accessing RecyclerView's LinearLayout's Visible Item Position
int scrollPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
int arraySize = stringArrayData.size(); // Gets RecyclerView's Adapter's Array Size
@Override
public void run() {
if (scrollPosition < arraySize) {
if (scrollPosition == arraySize - 1) {
flag = false;
} else if (scrollPosition <= 1) {
flag = true;
}
if (!flag) {
try {
// Delay in Seconds So User Can Completely Read Till Last String
TimeUnit.SECONDS.sleep(1);
mRecyclerViewr.scrollToPosition(0); // Jumps Back Scroll To Start Point
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Know The Last Visible Item
scrollPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
mRecyclerViewr.smoothScrollBy(x, 0);
handler.postDelayed(this, scrollSpeed);
}
}
};
handler.postDelayed(runnable, scrollSpeed);
在将recyclerview设置为adapter之后,您也可以通过这种方式实现它
final int duration = 10;
final int pixelsToMove = 263;
final Handler mHandler = new Handler(Looper.getMainLooper());
final Runnable SCROLLING_RUNNABLE = new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollBy(pixelsToMove, 0);
mHandler.postDelayed(this, duration);
}
};
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastItem = horizontalLayoutManager.findLastCompletelyVisibleItemPosition();
if (lastItem == horizontalLayoutManager.getItemCount() - 1) {
mHandler.removeCallbacks(SCROLLING_RUNNABLE);
Handler postHandler = new Handler();
postHandler.postDelayed(new Runnable() {
@Override
public void run() {
quickTips.setAdapter(null);
quickTips.setAdapter(adapter);
mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
}
}, 2000);
}
}
});
mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
这是自动滚动回收视图及其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);
RecyclerView RecyclerView=findViewById(R.id.rv_id);
最终整数时间=4000;//这是recyclerview中项目之间滑动的延迟时间
最终适配器=新适配器(数据项);
recyclerView.setAdapter(适配器);
最终LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(rootView.getContext(),LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
//LinearSnapHelper将目标子视图的中心捕捉到附加的RecyclerView的中心,如果需要,这是可选的,您可以使用它
最终LinearSnapHelper LinearSnapHelper=新的LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(recyclerView);
最终计时器=新计时器();
timer.schedule(新TimerTask(){
@凌驾
公开募捐{
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,时间);
用Kotlin简化:
lifecycleScope.launch(Dispatchers.Main, start = CoroutineStart.DEFAULT) {
var position = MINIMUM_POSITION
repeat(AUTO_SCROLL_REPEATING_TIMES) {
delay(SCROLL_DELAY)
rvBanners.smoothScrollToPosition(position)
when {
position+1 == listSize -> position = 0
position == 0 -> position = MINIMUM_POSITION
else -> position++
}
}
}
如果要在特定时间启动自动滚动:
val job = lifecycleScope.launch(Dispatchers.Main, start = CoroutineStart.LAZY) {
var position = MINIMUM_POSITION
repeat(AUTO_SCROLL_REPEATING_TIMES) {
delay(SCROLL_DELAY)
rvBanners.smoothScrollToPosition(position)
when {
position+1 == listSize -> position = 0
position == 0 -> position = MINIMUM_POSITION
else -> position++
}
}
}
...
job.start()
private void hanldeAutoScroll(){
int位置=0;
最后整修时间=2000年;
//最终整数像素移动=90;
final Handler mHandler=新处理程序(Looper.getMainLooper());
最终可运行滚动\u Runnable=new Runnable(){
@凌驾
公开募捐{
位置++;
如果(位置如何获得显示的卡的位置,并从中递增而不是重新开始此滚动不平稳。如何反复启动直到活动运行?@AliAhmed true,手动滚动时最好使用LayoutManager进行滚动,而不是使用Recyclerview本身。自动滚动不会从此继续item.工作正常,但滑块时间太慢,增加到3000毫秒非常完美。在我的模拟器中运行时,CPU似乎工作不正常。我想知道电池消耗是否太高。发生OutOfMemory错误。@AbhilashMaurya没有看到任何内存问题。使用SmoothScrollUsing smoothScrollBy disable onClick for items检查此问题,如何要解决这一问题,请使用平滑滚动位置而不是滚动位置,以便更好地像动画一样滚动。非常完美的答案。谢谢。
lifecycleScope.launch(Dispatchers.Main, start = CoroutineStart.DEFAULT) {
var position = MINIMUM_POSITION
repeat(AUTO_SCROLL_REPEATING_TIMES) {
delay(SCROLL_DELAY)
rvBanners.smoothScrollToPosition(position)
when {
position+1 == listSize -> position = 0
position == 0 -> position = MINIMUM_POSITION
else -> position++
}
}
}
val job = lifecycleScope.launch(Dispatchers.Main, start = CoroutineStart.LAZY) {
var position = MINIMUM_POSITION
repeat(AUTO_SCROLL_REPEATING_TIMES) {
delay(SCROLL_DELAY)
rvBanners.smoothScrollToPosition(position)
when {
position+1 == listSize -> position = 0
position == 0 -> position = MINIMUM_POSITION
else -> position++
}
}
}
...
job.start()
private void hanldeAutoScroll() {
int position=0;
final int duration = 2000;
//final int pixelsToMove = 90;
final Handler mHandler = new Handler(Looper.getMainLooper());
final Runnable SCROLLING_RUNNABLE = new Runnable() {
@Override
public void run() {
position++;
if (position<urls.size())
{
recyclerView.scrollToPosition(position);
}else if (position==urls.size())
{
position=-1;
}
// recyclerView.smoothScrollBy(pixelsToMove, 0);
mHandler.postDelayed(this, duration);
}
};
mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
}