gpt4 book ai didi

java - RecyclerView 分页时跳到顶部

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:08 25 4
gpt4 key购买 nike

尝试在 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 !");
}
}
}
});

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);
}

}

});

}

XML 文件:

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

还有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'}

那么谁能告诉我我做错了什么?

最佳答案

所以这个问题真的很蠢!首先,我将这两件事移到了 onCreate()

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

并添加了

adapter.notifyDataSetChanged();

在 loadFromUrl() 方法中,现在一切正常 :D 也许如果有人遇到同样的问题,那么他会看到我的帖子 :D

关于java - RecyclerView 分页时跳到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29354550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com