pwittchen / InfiniteScroll

Infinite Scroll (Endless Scrolling) for RecyclerView in Android
Apache License 2.0
190 stars 26 forks source link

A problem with scrolling #12

Closed waelalameen closed 7 years ago

waelalameen commented 7 years ago

The RecyclerView is keep repeating the same four items infinitely when scrolling and fetching them from server.

pwittchen commented 7 years ago

Can you provide any code snippet or steps to reproduce the problem? Failing unit test or any kind of automated test showing that problem would be perfect. In SearchTwitter app I used this library as well and it works fine during fetching data from the server. When there's nothing new to fetch, you cannot scroll down anymore.

waelalameen commented 7 years ago

Here's my code, i used Volley Libarary

private InfiniteScrollListener getInfiniteScroll() {
        return new InfiniteScrollListener(4, linearLayoutManager) {
            @Override
            public void onScrolledToEnd(int firstVisibleItemPosition) {
                String token = new SavePreferences(MainActivity.this).getValues("userWebToken", "webToken");
                firstPos = firstVisibleItemPosition;

                gson = new Gson();
                mainQuestionsInfo = new MainQuestions(token, String.valueOf(page));
                final String jsonString = gson.toJson(mainQuestionsInfo);

                StringRequest stringRequest = new StringRequest(Request.Method.POST, NetworkInfo.HOST_URL + "questions",
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Log.d("resultQ", response);
                                try {
                                    gson = new Gson();
                                    questionList.addAll(Arrays.asList(gson.fromJson(response, Question[].class)));

                                    for (Question ques : questionList) {
                                        if (ques.getResult().equalsIgnoreCase("Success")) {
                                            myAdapter = new MyAdapter(MainActivity.this, questionList, true);
                                            recyclerView.setAdapter(myAdapter);
                                            swipeRefreshLayout.setRefreshing(false);;
                                        } else {
                                            swipeRefreshLayout.setRefreshing(false);
                                        }
                                    }

                                    //progressBar.setVisibility(View.GONE);
                                    myAdapter.notifyDataSetChanged();
                                    refreshView(recyclerView, myAdapter, firstPos);

                                } catch (Exception e) {
                                    Log.e("err", e.getMessage());
                                    swipeRefreshLayout.setRefreshing(false);
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        try {
                            Log.i("VolleyError", error.getMessage());
                        } catch (Exception e) {
                            e.getMessage();
                        }
                    }
                })
                {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> map = new HashMap<>();
                        map.put("info", jsonString);
                        Log.d("question_info", jsonString);
                        return map;
                    }
                };

                MySingleton.getInstance(MainActivity.this).addToRequestQueue(stringRequest);
            }
}
pwittchen commented 7 years ago

It's hard to say for me, what could be wrong here. This code is not so clean. I guess, the problem must be in the logic you implemented inside onScrolledToEnd(int) method or in Volley library and it needs tests or debugging. I used InfiniteScroll library in the sample app and in the SearchTwitter app and I haven't occurred that problem, so I suppose it's specific for your project. It may be also related to the queue you're using in the end of the code.

waelalameen commented 7 years ago

I finally solved the problem and yes it was in the logic and not the library, thank you so much for help

pwittchen commented 7 years ago

Great!

Just being curious... Can you tell, how did you solve that problem?

waelalameen commented 7 years ago

I moved the StringRequest into a seperate method and i also have been forgotten to increment page variable by one while sending the request.

pwittchen commented 7 years ago

That's nice. Everything is clear, so I think we can close this issue now. :)