当前位置:  开发笔记 > 后端 > 正文

如何滚动到recyclerview内的偏移量?

如何解决《如何滚动到recyclerview内的偏移量?》经验,为你挑选了1个好方法。

如何使用recyclelerview内的偏移滚动到特定位置?

我用过

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

我也用过scrollto

mRecyclerView.scrollTo(0, scrolled);

我也使用了smoothScrolltoposition

mRecyclerView.smoothScrollToPosition(scrolled);

但似乎没有人为我工作.

如果有人对此有所了解,请帮助我

UPDATE

功能我在哪里设置我的Recyclerview的适配器

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() {
            @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");
    }
}

Roman T... 6

这是我的答案.组

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

这提供了RecyclerView,适配器和数据的方向(替换...为上下文,列表,数组或所需的其他数据).比设定

linearLayoutManager.scrollToPositionWithOffset(position, offset);

现在有趣的事情:

这将滚动到位置并将其放在列表的顶部,如果VERTICAL线性布局管理器在左侧与HORIZONTAL

如果偏移为正,则VERTICAL布局管理器将向下滚动此位置,offset以像素为单位(向右滚动HORIZONTAL布局管理器)

如果position为0且offset为正,则不会发生任何事情,因为RecyclerView没有任何项目位于0位置的顶部(左侧是HORIZONTAL布局管理器)

如果position为0且offset为负,则VERTICAL布局管理器将向上滚动0位置偏移(以像素为单位)(向左与HORIZONTAL)

如果RecyclerView的高度(HORIZONTAL方向的宽度)小于屏幕尺寸,也不会发生任何事情

如果要滚动到的位置是最后或接近结束并且其高度(在HORIZONTAL方向的情况下的宽度)相对较小,它也将错误地滚动

希望这将通过RecyclerView的布局管理器为您阐明情况



1> Roman T...:

这是我的答案.组

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

这提供了RecyclerView,适配器和数据的方向(替换...为上下文,列表,数组或所需的其他数据).比设定

linearLayoutManager.scrollToPositionWithOffset(position, offset);

现在有趣的事情:

这将滚动到位置并将其放在列表的顶部,如果VERTICAL线性布局管理器在左侧与HORIZONTAL

如果偏移为正,则VERTICAL布局管理器将向下滚动此位置,offset以像素为单位(向右滚动HORIZONTAL布局管理器)

如果position为0且offset为正,则不会发生任何事情,因为RecyclerView没有任何项目位于0位置的顶部(左侧是HORIZONTAL布局管理器)

如果position为0且offset为负,则VERTICAL布局管理器将向上滚动0位置偏移(以像素为单位)(向左与HORIZONTAL)

如果RecyclerView的高度(HORIZONTAL方向的宽度)小于屏幕尺寸,也不会发生任何事情

如果要滚动到的位置是最后或接近结束并且其高度(在HORIZONTAL方向的情况下的宽度)相对较小,它也将错误地滚动

希望这将通过RecyclerView的布局管理器为您阐明情况

推荐阅读
Gbom2402851125
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有