pet*_*986 48 android scroll smooth-scrolling gridlayoutmanager android-recyclerview
我正在使用基本的RecyclerView和GridLayoutManager.我观察到,smoothScrollToPosition和scrollToPosition也没有正常工作.
a)使用时smoothScrollToPosition
我经常收到错误RecyclerView
"RecyclerView:平滑滚动时传递到目标位置."
并且RecyclerView
没有正确滚动(通常它会错过目标行).这主要是在我试图滚动到某行的第一项时
b)使用scrollToPosition
它时似乎工作得很好但是大多数时候我只能看到行的第一项而其余的都没有显示.
你能给我一些提示,如何正确地完成至少一种方法吗?
非常感谢!
pet*_*986 65
最后我能够让它发挥作用!LinearLayoutManager.scrollToPositionWithOffset(int, int)
做了伎俩.
Ram*_*amz 14
我也有同样的问题,但设法通过自定义SmoothScroller来解决问题
让Custom LayoutManager如下所示
public class CustomLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 50f;
private Context mContext;
public CustomLayoutManager(Context context) {
super(context);
mContext = context;
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
RecyclerView.State state, final int position) {
LinearSmoothScroller smoothScroller =
new LinearSmoothScroller(mContext) {
//This controls the direction in which smoothScroll looks
//for your view
@Override
public PointF computeScrollVectorForPosition
(int targetPosition) {
return CustomLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
//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);
}
}
Run Code Online (Sandbox Code Playgroud)
(在上面给出的代码中注释的文档)请将上面的LayoutManager设置为recyerview
CustomLayoutManagerlayoutManager = new CustomLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.smoothScrollToPosition(position);
Run Code Online (Sandbox Code Playgroud)
通过使用自定义布局管理器
scrollToPosition也适用于我的情况你可以使用
recyclerView.scrollToPosition(position)
Run Code Online (Sandbox Code Playgroud)
另外如果你想调整smoothScrollToPosition的速度请覆盖
private static final float MILLISECONDS_PER_INCH = 50f;
Run Code Online (Sandbox Code Playgroud)
在CustomLayoutManager中.因此,如果我们将值设置为1f,则smoothScrollToPosition将更快,如scrollToPosition.increasing值使延迟和减少将使滚动的速度.希望这会有用.
zep*_*hyr 12
在我的情况下,
`mRecyclerView.scrollToPosition(10);`
Run Code Online (Sandbox Code Playgroud)
也没用.但
`mRecyclerView.smoothScrollToPosition(10);`
Run Code Online (Sandbox Code Playgroud)
对我来说很好......
单击EditText从RecyclerView中的任何位置向下滚动到底部.
edittext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rv_commentList.postDelayed(new Runnable() {
@Override
public void run() {
rv_commentList.scrollToPosition(rv_commentList.getAdapter().getItemCount() - 1);
}
}, 1000);
}
});
Run Code Online (Sandbox Code Playgroud)
前面提到的任何解决方案可能不起作用的另一个原因是,如果您的 RecyclerView 嵌入在 NestedScrollView 中。在这种情况下,您必须在 NestedScrollView 上调用滚动操作。
例如:
nestedScrollview.smoothScrollTo(0,0)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
57657 次 |
最近记录: |