简体   繁体   English

在分页时,RecyclerView会跳到顶部

[英]RecyclerView jumps to the top when paginating

Trying to implement pagination in RecyclerView, but when the RecyclerView reaches it's end and applications starts loading new portion of data the RecyclerView jumps to it's top. 试图在RecyclerView中实现分页,但是当RecyclerView到达终点并且应用程序开始加载新的数据部分时,RecyclerView会跳转到它的顶部。

cityListRecyclerview = (RecyclerView) findViewById(R.id.recyclerView);
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    cityListRecyclerview.setLayoutManager(linearLayoutManager);
    loadFromUrl();

    cityListRecyclerview.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            visibleItemCount = linearLayoutManager.getChildCount();
            totalItemCount = linearLayoutManager.getItemCount();
            pastVisiblesItems = linearLayoutManager.findFirstVisibleItemPosition();

            if (loading) {
                if ( (visibleItemCount+pastVisiblesItems) >= totalItemCount) {
                    loading = false;
                    page++;
                    url += Api.OFFSET_URL + String.valueOf(page);
                    loadFromUrl();
                    Log.v("LIST", "Last Item Wow !");
                }
            }
        }
    });

The loadFromUrl() method: loadFromUrl()方法:

public void loadFromUrl() {

    Ion.with(MainActivity.this)
            .load(url)
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {

                @Override
                public void onCompleted(Exception e, JsonObject jsonObject) {
                    progressDialog.dismiss();

                    JsonArray jsonArray = jsonObject.getAsJsonArray("flats");

                    jsonObjectToPass = jsonArray;

                    for(int i = 0; i < jsonArray.size(); i++) {
                        JsonElement obj = jsonArray.get(i);
                        JsonObject jsonObject1 = obj.getAsJsonObject();
                       ............ here parsing JSON object and nothing interesting
                        adapter = new OffersAdapter(MainActivity.this, cityList);
                        adapter.setClickListener(MainActivity.this);
                        loading = true;
                        cityListRecyclerview.setAdapter(adapter);
                    }

                }

            });

}

The XML file: XML文件:

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

And the gradle file: 和gradle文件:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'}

So can anyone tell what am I doing wrong? 所以有人能说出我做错了什么吗?

So the problem was really stupid! 所以问题真是太愚蠢了! First i moved this two things into onCreate() 首先,我将这两件事转移到onCreate()

adapter = new OffersAdapter(MainActivity.this, cityList);
cityListRecyclerview.setAdapter(adapter);

And added 并补充说

adapter.notifyDataSetChanged();

In loadFromUrl() method, and now everything works well :D Maybe if someone will have the same problem, then he'll see my post :D 在loadFromUrl()方法中,现在一切正常:D也许如果有人会遇到同样的问题,那么他会看到我的帖子:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM