[英]Problem with RecyclerView.OnScrollListener
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
我了解我误以为只是通过Presenter
重新更新View
的数据,并且有必要让它们全部可用RecyclerView
When loading the next portion of data, RecyclerView
does not have time to display the last elements and immediately loads a new portion. 加载下一部分数据时, RecyclerView
没有时间显示最后一个元素,而是立即加载新部分。
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: 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. 我有相同类型的recyclerview滚动,所以我设置为可以根据需要尝试。
//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. 如果解决方案可以帮助某人,则有必要从setData方法中删除所有初始化。
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)
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.