RecyclerView onScrolled方法始终或从未调用过

问题描述 投票:1回答:1

背景:我通过添加addOnScrollListener函数与RecyclerView实现分页逻辑。我正在使用MVVM模式(https://developer.android.com/jetpack/docs/guide)。我的目的是通过GridLayoutManager显示在GridLayout中对TheMovieDB的API调用产生的电影。

面临的问题:始终或永远不会调用滚动方法:

1)我什至无需滚动即可触发它。

2)在启动应用程序时触发,但在滚动时根本不会触发

这是我的代码

public void getMoviesLL(String categoria, int pagina, MutableLiveData < Resource < List < Movie >>> moviesData) {
    Service apiService = Client.getClient().create(Service.class);
    Call < MoviesResponse > call;

    Log.d(TAG, "CHIAMATA " + pagina);
    call = apiService.getTMDB(categoria, Constants.API_KEY, Constants.LINGUA, pagina);

    call.enqueue(new Callback < MoviesResponse > () {

        @Override
        public void onResponse(@NonNull Call < MoviesResponse > call, @NonNull Response < MoviesResponse > response) {
            if (response.isSuccessful() && response.body() != null) {

                Resource < List < Movie >> resource = new Resource < >();

                if (moviesData.getValue() != null && moviesData.getValue().getData() != null) {
                    List < Movie > currentMovieList = moviesData.getValue().getData();

                    currentMovieList.remove(currentMovieList.size() - 1);
                    currentMovieList.addAll(response.body().getResults());
                    resource.setData(currentMovieList);
                } else {
                    resource.setData(response.body().getResults());
                }

                resource.setTotalResults(response.body().getTotalResults());
                resource.setStatusCode(response.code());
                resource.setStatusMessage(response.message());
                resource.setLoading(false);
                Log.d(TAG, "LOADING FALSE");
                moviesData.postValue(resource);
            } else if (response.errorBody() != null) {
                Resource < List < Movie >> resource = new Resource < >();
                resource.setStatusCode(response.code());
                try {
                    resource.setStatusMessage(response.errorBody().string() + "- " + response.message());
                } catch(IOException e) {
                    e.printStackTrace();
                }
                moviesData.postValue(resource);
            }
        }

        @Override
        public void onFailure(@NonNull Call < MoviesResponse > call, @NonNull Throwable t) {
            Resource < List < Movie >> resource = new Resource < >();
            resource.setStatusMessage(t.getMessage());
            moviesData.postValue(resource);
        }
    });

}

这里是模特

 public class Resource<T> {
        private T data;
        private int totalResults;
        private int statusCode;
        private String statusMessage;
        private boolean isLoading;

        public Resource() {}

        public Resource(T data, int totalResults, int statusCode, String statusMessage, boolean isLoading) {
            this.data = data;
            this.totalResults = totalResults;
            this.statusCode = statusCode;
            this.statusMessage = statusMessage;
            this.isLoading = isLoading;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public int getTotalResults() {
            return totalResults;
        }

        public void setTotalResults(int totalResults) {
            this.totalResults = totalResults;
        }

        public int getStatusCode() {
            return statusCode;
        }

        public void setStatusCode(int statusCode) {
            this.statusCode = statusCode;
        }

        public String getStatusMessage() {
            return statusMessage;
        }

        public void setStatusMessage(String statusMessage) {
            this.statusMessage = statusMessage;
        }

        public boolean isLoading() {
            return isLoading;
        }

        public void setLoading(boolean loading) {
            isLoading = loading;
        }

        @Override
        public String toString() {
            return "Resource{" +
                    "data=" + data +
                    ", totalResults=" + totalResults +
                    ", statusCode=" + statusCode +
                    ", statusMessage='" + statusMessage + '\'' +
                    ", isLoading=" + isLoading +
                    '}';
        }
    } 

查看模型类

public class NuoviArriviViewModel extends ViewModel {
        private static final String TAG = "NuoviArriviViewModel";
        private MutableLiveData<Resource<List<Movie>>> film;
        int page = 1;
        private int currentResults;
        private boolean isLoading = false;

        public MutableLiveData<Resource<List<Movie>>> getProssimeUscite() {
            if(film == null) {
                film = new MutableLiveData<>();
                MoviesRepository.getInstance().getMoviesLL("now_playing", page, film);
            }
            return film;
        }

        public MutableLiveData<Resource<List<Movie>>> getMoreProssimeUscite() {
            MoviesRepository.getInstance().getMoviesLL("now_playing", page, film);
            return film;
        }

        public MutableLiveData<Resource<List<Movie>>> getMoviesLiveData() {
            return film;
        }

        public int getPage() {
            return page;
        }

        public void setPage(int page) {
            this.page = page;
        }

        public int getCurrentResults() {
            return currentResults;
        }

        public void setCurrentResults(int currentResults) {
            this.currentResults = currentResults;
        }

        public boolean isLoading() {
            return isLoading;
        }

        public void setLoading(boolean loading) {
            isLoading = loading;
        }
    }

OnScroll侦听器

prossimeUsciteRV.addOnScrollListener(new RecyclerView.OnScrollListener() {

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

        Log.d(TAG, "entro in onScrolled");

        totalItemCount = layoutManager.getItemCount();
        lastVisibleItem = layoutManager.findLastVisibleItemPosition();
        visibleItemCount = layoutManager.getChildCount();

        // Condition to enable the loading of other news while the user scrolls the list
        if (totalItemCount == visibleItemCount || (totalItemCount <= (lastVisibleItem + threshold) && dy > 0 && !nuoviArriviViewModel.isLoading()) && nuoviArriviViewModel.getMoviesLiveData().getValue() != null && nuoviArriviViewModel.getCurrentResults() != nuoviArriviViewModel.getMoviesLiveData().getValue().getTotalResults()) {
            Resource < List < Movie >> movieListResource = new Resource < >();

            MutableLiveData < Resource < List < Movie >>> movieListMutableLiveData = nuoviArriviViewModel.getMoviesLiveData();

            if (movieListMutableLiveData.getValue() != null) {

                nuoviArriviViewModel.setLoading(true);

                List < Movie > currentMovieList = movieListMutableLiveData.getValue().getData();

                // It adds a null element to enable the visualization of the loading item (it is managed by the class nuoviArriviAdapter)
                currentMovieList.add(null);
                movieListResource.setData(currentMovieList);
                movieListResource.setStatusMessage(movieListMutableLiveData.getValue().getStatusMessage());
                movieListResource.setTotalResults(movieListMutableLiveData.getValue().getTotalResults());
                movieListResource.setStatusCode(movieListMutableLiveData.getValue().getStatusCode());
                movieListResource.setLoading(true);
                movieListMutableLiveData.postValue(movieListResource);

                int page = nuoviArriviViewModel.getPage() + 1;
                nuoviArriviViewModel.setPage(page);

                nuoviArriviViewModel.loadMore();
            }
        }
    }
});

XML文件

 <?xml version="1.0" encoding="utf-8"?>

        <GridLayout
            android:layout_width="match_parent"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/nuovi_arrivi_fragment"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:orientation="vertical"
            tools:context=".ui.nuovi_arrivi.NuoviArriviFragment">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view_nuovi_arrivi"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:orientation="vertical" />

        </GridLayout>
android android-recyclerview gridlayoutmanager
1个回答
0
投票

问题是我将布局包装在NestedScrollView中,因此无法正确触发onScrolled事件

© www.soinside.com 2019 - 2024. All rights reserved.