Scrolling in the opposite direction does not work.
I understand that I am mistaken just to re-update the data in the View
through the Presenter
, and it is necessary that they are all available RecyclerView
When loading the next portion of data, RecyclerView
does not have time to display the last elements and immediately loads a new portion.
What am I doing wrong?
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();
Presenter :
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();
Try this
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
}
}
}
}
I have the same type of recyclerview scrolling so I am set this you can try it as you need.
//Pagination
private int startPosition = 0;
private boolean isLoading = true;
//recycler view scroll listener for pagination
rvCoupons.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int pastVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (isLoading) {
if ((visibleItemCount + pastVisibleItem) >= totalItemCount) {
isLoading = false;
filterDataRequest.setStart(startPosition);
viewModel.callSearchCouponsApi(true, filterDataRequest);
}
}
}
}
});
//On Api Success
if (response != null && !response.getData().isEmpty()) {
if (startPosition == 0)
mCouponList.clear();
if (response.getData() != null && !response.getData().isEmpty()) {
startPosition += response.getData().size();
mCouponList.addAll(response.getData());
if (mSearchCouponAdapter != null) mSearchCouponAdapter.notifyDataSetChanged();
isLoading = true;
} else {
isLoading = false;
}
}
If the solution helps someone, it was necessary to remove all initialization from the setData method.
public class HomeActivity extends AppCompatActivity implements HomeView{
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
@BindView(R.id.btn_refresh)
FloatingActionButton btn_refresh;
HomePresenter presenter;
private List<PokemonData.Pokemon> items = new ArrayList<>();
private static int offset;
private static boolean isLoading;
private RecyclerViewHomeAdapter homeAdapter;
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;
homeAdapter = new RecyclerViewHomeAdapter(items, 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) {
offset+=30;
presenter.getData(offset);
isLoading = false;
homeAdapter.setPokemonList(items);
}
}
});
}
@Override
public void setData(List<PokemonData.Pokemon> pokemons) {
items.addAll(pokemons);
homeAdapter.setPokemonList(items)
}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.