当前位置: 首页 > 知识库问答 >
问题:

如何在recyclerview中滚动到偏移量?

柯建修
2023-03-14

如何在recyclerview中滚动到具有偏移量的特定位置?

我用过

public LinearLayoutManager linearlayoutmanager;
linearlayoutmanager.scrollToPositionWithOffset(0, scrolled);

我也曾经

mRecyclerView.scrollTo(0, scrolled);

此外,我使用了skrolltopoplace

mRecyclerView.smoothScrollToPosition(scrolled);

但似乎没有一个适合我。

如果有人知道这件事,你能帮我吗

使现代化

功能,其中我设置适配器的我的回收站

public void Nearby_Search_requestData() {
    OkHttpClient client = new OkHttpClient();
    client = MySSLTrust.trustcert(getActivity());

    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(SEARCH_MERCHANT_ENDPOINT).setClient(new OkClient(client)).build();
    final RetrofitInterface api = adapter.create(RetrofitInterface.class);
    if (api != null) {
        api.post_Nearby_Search(page.toString(), new Callback<Nearby_Search_All>() {
            @Override
            public void success(final Nearby_Search_All server_data, Response response) {
                if (server_data.getSuccess() == 1) {
                    if (server_data.getResults().size() == 0) {
                        if (page != 0) {
                            mRecyclerView.scrollToPosition(firstVis);
                        } else {
                            Log.d("hii", "zero");
            //                NO_MERCHANT.setVisibility(View.VISIBLE);
                            mAdapter = new searchrecyclerviewadapter(getActivity(), data, font);
                            mRecyclerView.setAdapter(mAdapter);
                        }
                    } else {
                        Log.d("scroll", "hii");
            //            NO_MERCHANT.setVisibility(View.INVISIBLE);
                        if (server_data.getSize() > server_data.getResults().size()) {
                            stay_on_this_page = true;
                        } else {
                            stay_on_this_page = false;
                        }
                        if (page == 0) {
                            prev_total = 0;
                            data.clear();
                            Log.d("hii", "Page is zero " + prev_total);
                            list_size = server_data.getSize();
                        } else {
                            Log.d("hii", "prev_total is " + prev_total + " Size is " + server_data.getSize() + " page is " + page);
                            prev_total = server_data.getSize() * page;
                            Log.d("hii", "Now prev_total is " + prev_total);
                        }
                        total_curr = server_data.getResults().size();

                        Log.d("hii", "first place is " + server_data.getResults().get(0).getShop_name());

                        for (int i = 0; i < total_curr; i++) {
                            information current = new information();
                            current.title = server_data.getResults().get(i).getShop_name();
                            String street = server_data.getResults().get(i).getAddress().getStreet();
                            String area = server_data.getResults().get(i).getAddress().getArea();
                            current.Address = street + " " + area;
                            String[] splited = area.split(",");
                            current.area1 = splited[1];
                            current.type = server_data.getResults().get(i).getShop_type();
                            current.rating = server_data.getResults().get(i).getRating();
                            current.distance = server_data.getResults().get(i).getDistance();
                            current.jeb_no = server_data.getResults().get(i).getJeb_no();
                            current.total_photos = server_data.getResults().get(i).getTotal_photos();
                            if (current.total_photos  == 0) {

                            } else if (current.total_photos  == 1) {
                                current.photo_1 = server_data.getResults().get(i).getPhotos().getPhoto_1();
                            } else if (current.total_photos  == 2) {
                                current.photo_1 = server_data.getResults().get(i).getPhotos().getPhoto_1();
                                current.photo_2 = server_data.getResults().get(i).getPhotos().getPhoto_2();
                            } else {
                                current.photo_1 = server_data.getResults().get(i).getPhotos().getPhoto_1();
                                current.photo_2 = server_data.getResults().get(i).getPhotos().getPhoto_2();
                            }
                            data.add(current);
                        }
                        if (adapterinitialised) {
                            mAdapter.notifyDataSetChanged();
                        } else {
                            mAdapter = new searchrecyclerviewadapter(getActivity(), data, font);
                          //  mAdapter.setClickListener(Search.class);
                            mRecyclerView.setAdapter(mAdapter);

                            adapterinitialised = true;
                        }
                    }
                 //   mRecyclerView.scrollToPosition(firstVis);
                    Log.d("scroll", "hee");
                    scrolled = scrolled + 10;
                    Log.d("scroll", "scrolled: " + scrolled);
                    // ALL BELOW CODE I HAVE TRIED FOR SCROLLING BUT NONE WAS HELPFUL
                /*    mRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            mRecyclerView.smoothScrollToPosition(scrolled);

                            // or do mRecyclerView.scrollTo(0, scrolled);
                        }
                    });
                    */
                 //   l.scrollToPositionWithOffset(0, scrolled);
                //    mRecyclerView.smoothScrollToPosition(scrolled);
                //    mRecyclerView.scrollBy(0, scrolled);
                //    l.scrollToPosition();
                //        mRecyclerView.scrollToPosition(scrolled);
                //    mRecyclerView.smoothScrollBy(0, scrolled);
                //    mRecyclerView.scrollBy(0, 10);
                //    mRecyclerView.scrollTo(0, 10);

                } else {
                    Log.d("hii", server_data.getMessage());
                    Toast.makeText(getActivity(), server_data.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                Log.d("hii", "failure");
                Log.e("hii", retrofitError.toString());
                Toast.makeText(getActivity(), retrofitError.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    } else {
        Log.d("hii", "api null");
    }
}

共有3个答案

宫俊远
2023-03-14

这对我有效:

recyclerView.postDelayed(new Runnable() {
    @Override
    public void run() {
        recyclerView.smoothScrollToPosition(0);
    }
}, 10);


recyclerView.post(new Runnable() {
    @Override
    public void run() {
        recyclerView.smoothScrollToPosition(0);
    }
});
闻人昕
2023-03-14

我相信您想要的是运行此滚动,但请在RecyclerView上发布runnable并在此处运行smoothScrollToPosition:

 // use either post or postDelayed
 mRecyclerView.postDelayed(new Runnable() {
        @Override
        public void run() {
            mRecyclerView.smoothScrollToPosition(scrolled);

            // or do mRecyclerView.scrollTo(0, scrolled);
        }
    }, 1000);

如果要跳过延迟,请执行以下操作:

 mRecyclerView.post(new Runnable() {
        @Override
        public void run() {
            mRecyclerView.smoothScrollToPosition(scrolled);

            // or do mRecyclerView.scrollTo(0, scrolled);
        }
    });
朱浩大
2023-03-14

这是我的答案。设置

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//or HORIZONTAL
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new Adapter(...);
recyclerView.setAdapter(adapter);

这提供了回收视图、适配器和数据的方向(用上下文、列表、数组或其他所需数据替换)。比设定

linearLayoutManager.scrollToPositionWithOffset(position, offset);

有趣的是:

  • 这将滚动到位置,并将其置于列表顶部,如果垂直线性布局管理器位于左侧,则显示为水平

希望这将澄清的情况下,你与回收站的布局经理

 类似资料:
  • 我想了解为什么我不能在LinearLayoutManager上使用scrollToPositionWithOffset方法?请看图片了解我的意思: 一点背景: 图像中的第一行(带有)是滚动RecyclerView以使位置(在本例中为50)可见-这通常意味着所选位置显示在可见的RecyclerView的底部(其中位置50在“滚动”后首先可见)。而我总是想在顶部展示它。根据我的研究,使用这个方法似乎是

  • 如何禁用滚动?

  • 问题是我有组件的层次结构。我有一个父组件、子组件和子组件(便于在代码中导航)。在父级组件中,我有一个ScrollView和一个变量(不是动画变量)contentOffset(处于父级状态),每当滚动发生时,该变量都会发生变化。变量间隔介于[-100100]之间。 父成分 子组件 子组件 我将contentOffset变量作为prop传递给子组件,然后传递给子组件的子组件。如果我使用代码,缩放间隔将

  • 问题内容: 这是我的onCreate,它处理适配器,LinearLayoutManager等。我尝试了每个选项以尝试将其滚动到顶部,但不能。我什至尝试创建自己的自定义LinearLayoutManager。 问题答案: 您尝试过还是? RecyclerView不会在其中实现任何滚动逻辑,而是更加具体,它会根据实现的布局转到特定的索引,在您的情况下是线性的。

  • 我正在做一个音乐播放器。因此,在相册部分,我尝试加载毕加索库的相册图像。图像加载正确,但当我滚动时,就会出现应用滞后问题。我也尝试使用glide,但性能没有改善。我认为此问题是由于此链接的图像加载而出现的。“content://media/external/audio/albumart“。是否有其他方法?如果没有,那么如何提高滚动性能..”