我正在使用网格布局管理器来使用回收器 View 。我想在单击按钮时滚动回收 View 。(在单击按钮时我正在传递位置,我的项目将突出显示)。所以我想在顶部显示突出显示的项目。可能该项目位于不同的位置。
onViewCreated 我正在初始化的地方-----
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recycleView = view.findViewById(R.id.recycler_vieww);
lLayout = new GridLayoutManager(context, 3);
recycleView.setLayoutManager(lLayout);
/* ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);
recycleView.addItemDecoration(itemDecoration);*/
String channelId = "";
if (tvStatus != null && tvStatus.getData() != null && tvStatus.getData().getDetails().get(0).getChannelData() != null) {
if (tvStatus.getData().getDetails().get(0).getCurrentFeature().equalsIgnoreCase("channel")) {
//rcAdapter = new Televison_Adapter(context, chnlList,"43", shared, readDb);
if (tvStatus.getData().getDetails().get(0).getChannelData() != null) {
channelId = tvStatus.getData().getDetails().get(0).getChannelData().getChannelId();
}
}
}
rcAdapter = new Televison_Adapter(context, chnlList, channelId, shared, readDb,this);
rcAdapter.setOnChannelSelectListner(this);
rcAdapter.setOnSelectedListItemListner(this);
recycleView.setAdapter(rcAdapter);
rcAdapter.notifyDataSetChanged();
}
接口(interface)抽象方法 ---从我想要滚动我的 recyclerView 的位置开始,位置是从适配器类传递的。
@Override
public void getItemPosition(final int position) {
/*RecyclerView.SmoothScroller smoothScroller = new
LinearSmoothScroller(context) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
smoothScroller.setTargetPosition(position);
lLayout.startSmoothScroll(smoothScroller);*/
recycleView.scrollTo(0,1000);
}
最佳答案
您可以使用scrollTo()
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.scrollToPosition(adapter.getItemCount() - 1);
// Here adapter.getItemCount()== child count
}
});
或者 smoothScrollToPosition()。
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);
}
});
关于java - 单击按钮将回收器 View 滚动到特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51439762/