将“按钮”上的“回收站”视图滚动到特定位置

时间:2018-07-20 09:46:23

标签: java android

我正在使用网格布局管理器使用Recycler视图。我想在按钮单击时滚动回收视图。(在按钮单击时,我正在传递位置,我的项目将突出显示)。所以我想在顶部显示突出显示的项目。可能是该商品位于其他位置。

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();

}

接口抽象方法---我要从中滚动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);

}

1 个答案:

答案 0 :(得分:1)

您可以使用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);
        }
    });