别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述RecyclerView smoothScroll位于中心位置。安卓相关的知识,希望能为你提供帮助。
我正在为我的RecyclerView
使用水平布局管理器。我需要以下一种方式制作RecyclerView
:当点击某个项目时 - 将smoothScrool放到该位置并将该项目放在RecyclerView
的中心(如果可能,例如20中的10项)。
【RecyclerView smoothScroll位于中心位置。安卓】所以,我对smoothScrollToPosition()
没有问题,但如何把项目放在RecyclerView
的中心???
谢谢!
答案是的,这是可能的。
通过实施RecyclerView.SmoothScroller
的方法onTargetFound(View, State, Action)
。
/**
* Called when the target position is laid out. This is the last callback SmoothScroller
* will receive and it should update the provided {@link Action} to define the scroll
* details towards the target view.
* @param targetViewThe view element which render the target position.
* @param stateTransient state of RecyclerView
* @param actionAction instance that you should update to define final scroll action
*towards the targetView
*/
abstract protected void onTargetFound(View targetView, State state, Action action);
特别是在
LinearLayoutManager
与LinearSmoothScroller
:public class CenterLayoutManager extends LinearLayoutManager {public CenterLayoutManager(Context context) {
super(context);
}public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}private static class CenterSmoothScroller extends LinearSmoothScroller {CenterSmoothScroller(Context context) {
super(context);
}@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
}
}
另一答案对答案的改进 - 没有必要覆盖
LinearLayoutManager
从上一个答案:
public class CenterSmoothScroller extends LinearSmoothScroller {CenterSmoothScroller(Context context) {
super(context);
}@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
}
在这里如何使用它:
RecyclerView.LayoutManager lm = new GridLayoutManager(...): // or whatever layout manager you need...RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
lm.startSmoothScroll(smoothScroller);
另一答案以防有人在接受的答案中需要Kotlin相当于班级。
class CenterLayoutManager : LinearLayoutManager {
constructor(context: Context) : super(context)
constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int) {
val centerSmoothScroller = CenterSmoothScroller(recyclerView.context)
centerSmoothScroller.targetPosition = position
startSmoothScroll(centerSmoothScroller)
}private class CenterSmoothScroller(context: Context) : LinearSmoothScroller(context) {
override fun calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int): Int = (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2)
}
}
另一答案从现在开始(2019年2月),我可以轻松地在ListView中使用此代码
(ListView)word_list_view.smoothScrollToPositionFromTop(your_item_index, center_position.y);
RecyclerView未经验证,我猜也是一样的。
推荐阅读
- 如何在android中的日历上的特定日期设置点
- 在Android中创建用户可选择的字符串列表的最简单方法是什么()
- 使用回收视图时无法在Android模拟器中看到应用栏
- Android TV RecyclerView重点互动
- 如何解决无法识别应用的tf.app.run()错误()
- 重复条目(android / support / v7 / recyclerview / extensions / ListAdapter.class [复制])
- 适用于所有移动设备的Webview App并上传到Play商店
- Android WebView - 重置HTTP会话
- Android WebView err_unknown_url_scheme