点击工具栏时,将RecyclerView滚动到顶部时遇到问题。我要做的是使其看起来像IOS。当我点击工具栏时,我只是向上滚动到顶部。我实现了以下代码,但不起作用。 setSafeClickListener被调用,但是smoothScrollToPosition没有执行任何操作。我希望收到您的一些样品或提示!
private fun myRecyclerView(myListAdapter: MyRecyclerAdapter) {
recyclerView = binding.myRecycleView
recyclerView.isMotionEventSplittingEnabled = false
// myLayoutManager extends the LinearLayoutManager
recyclerView.layoutManager = myLayoutManager(context!!)
recyclerView.adapter = couponListAdapter
val appCompat = (activity as AppCompatActivity)
val toolbar = appCompat.my_tool as androidx.appcompat.widget.Toolbar
toolbar?.setSafeOnClickListener {
recyclerView.smoothScrollToPosition(0)
}
}
答案 0 :(得分:0)
删除
recyclerView.smoothScrollToPosition(0)
并在工具栏的onClick中将其替换为
myLayoutManager.scrollToPositionWithOffset(0, 0);
我希望它能正常工作。
答案 1 :(得分:0)
将以下代码添加到您的活动中
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
现在,您设置要滚动到的位置:
smoothScroller.setTargetPosition(position);
并将该SmoothScroller传递给LayoutManager:
myLayoutManager.startSmoothScroll(smoothScroller);