【问题标题】:Scroll Recycler view on Button click to specific position [duplicate]将按钮上的回收站视图滚动到特定位置[重复]
【发布时间】:2018-12-28 14:26:34
【问题描述】:

我正在使用网格布局管理器使用 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);

}

【问题讨论】:

  • 你能发布你的代码吗?
  • @nik 你检查了吗
  • @Nilesh Rathod 解决方案没有帮助

标签: java android


【解决方案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);
        }
    });

【讨论】:

  • 我使用了很多这样的解决方案,但都不起作用。我有一个特定的位置可以传递一个方法。您的方法将在创建回收站视图期间起作用。但是当我当时使用不同方法的上述代码时,它将无法正常工作。
  • 分享你的代码
  • 你检查了吗
猜你喜欢
  • 2017-10-05
  • 1970-01-01
  • 2021-10-19
  • 2021-06-06
  • 1970-01-01
  • 2019-05-26
  • 2016-09-22
  • 1970-01-01
  • 2018-11-17
相关资源
最近更新 更多