SRB*_*wat 57 android android-recyclerview
我在其中有一个片段RecyclerView与LinearLayoutManager其中有CardView项目.点击时有一个浮动操作按钮,项目应滚动到顶部.我已经尝试使用scrollToPosition以及scrollToPositionWithOffset与RecyclerView和也LinearLayoutManager,如下图所示.但它根本没有效果.为什么会这样?任何人都可以帮助我.
我已将其RecyclerView直接放在SwipeRefreshViewxml文件中.我setFloatingActionButton一旦设置适配器就打电话给我RecyclerView.
public void setFloatingActionButton(final View view) {
float = (android.support.design.widget.FloatingActionButton) getActivity().findViewById(R.id.float);
float.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecyclerView.smoothScrollToPosition(0);
android.support.design.widget.Snackbar.make(view, "Scrolled to Top", android.support.design.widget.Snackbar.LENGTH_SHORT)
.setAction("Ok", new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
llm.scrollToPosition(0);
}
})
.setActionTextColor(getActivity().getResources().getColor(R.color.coloLink))
.show();
}
});
}
Run Code Online (Sandbox Code Playgroud)
Ant*_*met 125
继续从上述评论,理想情况下,取代
mRecyclerView.smoothScrollToPosition(0);
Run Code Online (Sandbox Code Playgroud)
在onClick浮动动作按钮中
mLayoutManager.scrollToPositionWithOffset(0, 0);
Run Code Online (Sandbox Code Playgroud)
应该管用.您也可以删除SnackBar代码,因为您无论如何都不需要它.所以,你所有的上述方法都应该是这样的
public void setFloatingActionButton(final View view) {
float actionButton = (android.support.design.widget.FloatingActionButton) getActivity()
.findViewById(R.id.float);
actionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
.getLayoutManager();
layoutManager.scrollToPositionWithOffset(0, 0);
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果你说上面的方法不起作用,那么测试一下onClick()是否被调用.尝试在其中添加日志消息,看看是否打印出来.
gun*_*dev 11
使用该方法smoothScrollToPosition()适用于最新的Android版本.
layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);
Run Code Online (Sandbox Code Playgroud)
使用以下命令调用“scrollToPosition(0)”:
public void scrollToPosition(final int position) {
if (mRecyclerView != null) {
getHandler().post(new Runnable() {
@Override
public void run() {
mRecyclerView.scrollToPosition(position);
if (position == 0) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
layoutManager.scrollToPositionWithOffset(0, 0);
mScrollView.fullScroll(View.FOCUS_UP);
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59701 次 |
| 最近记录: |