RecyclerView.OnScrollListener问题

时间:2019-07-15 10:11:17

标签: java android android-recyclerview

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

我了解我误以为只是通过View重新更新Presenter中的数据,并且有必要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();

3 个答案:

答案 0 :(得分:0)

尝试一下

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

答案 1 :(得分:0)

我具有相同类型的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;
        }
    }

答案 2 :(得分:0)

如果该解决方案可以帮助某人,则必须从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)

}}