0

I have a RecyclerView nested in a scrollView in one of my activities.

I implemented dynamic data loading for the recycler so that when x visible objects are left it will automatically issue an API call to fetch the next items.

for some reason, being nested in a scroll view cancels that out. I actually have a couple of these API calls when activity starts, maybe because the scroll view wraps the content of the recycler, and the recycler wraps its own content. at any rate this is bad for me, and even worse is the fact that further dynamic loads are not performed when I scroll down recycler content.

I did manage to fix bad scrolling behaviour by using NestedScrollView and recycler.setNestedScrollingEnabled(false).

I cant plant the the content above the recyclerView that I want to scroll along with the recycler as a static header in the recycler layout as it is not static content but 2 other recyclers and a bunch of other stuff I need my activity to have control over.

any ideas?

1 Answers1

0

THis is late but you could try adding an OnScrollListener it always works for me even when RecyclerView is nested:

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();
private List<Movie> movies;
private RecyclerView recyclerView;
private GridLayoutManager gridLayout;
private MoviesAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    movies = new ArrayList<>();
    getMoviesFromDB(0);

    gridLayout = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(gridLayout);

    adapter = new MoviesAdapter(this, movies);
    recyclerView.setAdapter(adapter);

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            if (gridLayout.findLastCompletelyVisibleItemPosition() == movies.size() - 1) {
                getMoviesFromDB(movies.get(movies.size() - 1).getId());
            }

        }
    });
}

private void getMoviesFromDB(int id) {

    AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
        @Override
        protected Void doInBackground(Integer... movieIds) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("http://192.168.1.35/movies.php?id=" + movieIds[0])
                    .build();
            try {
                Response response = client.newCall(request).execute();

                JSONArray array = new JSONArray(response.body().string());

                for (int i = 0; i < array.length(); i++) {

                    JSONObject object = array.getJSONObject(i);

                    Movie movie = new Movie(object.getInt("id"), object.getString("movie_name"),
                            object.getString("movie_image"), object.getString("movie_genre"));

                    MainActivity.this.movies.add(movie);
                }


            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            adapter.notifyDataSetChanged();
        }
    };

    asyncTask.execute(id);
}}

With this,only when the user scrolls will the api call check for more.Kind of like in google play store.

Kennedy Kambo
  • 372
  • 4
  • 24