gpt4 book ai didi

java - RecyclerView.OnScrollListener 的问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:39 26 4
gpt4 key购买 nike

向相反方向滚动不起作用。

我明白我错了只是通过Presenter重新更新View中的数据,并且必须它们都可用RecyclerView

当加载下一部分数据时,RecyclerView没有时间显示最后一个元素,并立即加载新的部分。

我做错了什么?

public class HomeActivity extends AppCompatActivity implements HomeView{


@BindView(R.id.recyclerView)
RecyclerView recyclerView;

HomePresenter presenter;

private static int offset;

private static boolean isLoading;

public static void setIsLoading(boolean isLoading) {
HomeActivity.isLoading = isLoading;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);

presenter = new HomePresenter(this);
presenter.getData(offset);
offset = 0;
isLoading = true;
}


@Override
public void setData(List<PokemonData.Pokemon> pokemons) {
RecyclerViewHomeAdapter homeAdapter = new RecyclerViewHomeAdapter(pokemons, this);
recyclerView.setAdapter(homeAdapter);
GridLayoutManager layoutManager = new GridLayoutManager(this, 2, RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);


recyclerView.addOnScrollListener(new RecyclerViewOnScroll() {
@Override
protected void loadMore() {

if (isLoading) {
isLoading = false;
offset+=30;
presenter.getData(offset);
}
}
});
homeAdapter.notifyDataSetChanged();

演讲者:

    void getData(int offset) {

Call<PokemonData> pokemonDataCall = Utils.getApi().getData(offset, 30);

pokemonDataCall.enqueue(new Callback<PokemonData>() {
@Override
public void onResponse(Call<PokemonData> call, Response<PokemonData> response) {

HomeActivity.setIsLoading(true);

if (response.isSuccessful() && response.body() != null) {
view.setData(response.body().getResults());
}
else {
view.onErrorLoading(response.message());
}
}

@Override
public void onFailure(Call<PokemonData> call, Throwable t) {
HomeActivity.setIsLoading(true);
view.onErrorLoading(t.getLocalizedMessage());
}
});
}

RecyclerViewOnScroll:

public abstract class RecyclerViewOnScroll extends RecyclerView.OnScrollListener {

@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if ( dy > 0) {

GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
int visibleItemCount = gridLayoutManager.getChildCount();
int totalItemCount = gridLayoutManager.getItemCount();
int firstVisibleItems = gridLayoutManager.findFirstVisibleItemPosition();

if ((visibleItemCount + firstVisibleItems) >= totalItemCount) {
loadMore();
}
}
}

protected abstract void loadMore();

最佳答案

试试这个

private Boolean mIsMoreLoading = false;
private Boolean mIsLastPage = false;


@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if ( dy > 0) {

GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
int visibleItemCount = gridLayoutManager.getChildCount();
int totalItemCount = gridLayoutManager.getItemCount();
int firstVisibleItems = gridLayoutManager.findFirstVisibleItemPosition();

if ((!mIsMoreLoading) && (!mIsLastPage)) {
if ((firstVisibleItems + visibleItemCount) >= totalItemCount
&& firstVisibleItems >= 0
&& totalItemCount >= 20 // the items in one request) {
loadMore()
mIsMoreLoading = true
}
}
}
}

关于java - RecyclerView.OnScrollListener 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57037594/

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