java - 单击按钮将回收器 View 滚动到特定位置

标签 java android

我正在使用网格布局管理器来使用回收器 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/

相关文章:

android - NDK r16b 独立工具链错误 : "/usr/bin/ld: this linker was not configured to use sysroots"

java - 当没有条件可显示的项目时隐藏 recyclerview

java - 如何将字符串传递到组合框?

java - Map<String, Integer> 中的问题计数

java - getAdapterPosition 在 ViewHolder 类中返回 -1

Android 将下载的数据保存到 SD

c# - 如何在 Java 中使用 C# 数据集?

java - 使用 SimpleDateFormat 将字符串转换为日期会返回随机日期

java - Android Studio、Java 8 和函数

android - 无法实例化 Activity 组件信息 Java.lang.nullpointerexception