Search code examples
androidlistviewpaginationscrollview

Auto pagination fails on fast scroll listView


I am creating a ListView using Holder
when I scroll slowly everything works perfectly fine and Endless ListView works great but when I open the page and scroll fast to end of the List ListViewfail to scroll further without any error at Logcat.
Here is my Adapter class getView method

 @Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    View row=view;
    Holder holder=null;
    if(row==null){
        LayoutInflater inflater= (LayoutInflater)mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
        row= inflater.inflate(R.layout.half_movie,viewGroup,false);
        holder=new Holder(row);
        row.setTag(holder);
    }else{


        holder= (Holder) row.getTag();



    }
    //here setting all holers
    Picasso.with(mContext).load("https://image.tmdb.org/t/p/w185"+temp.getImageUrl()).into(holder.poster);
    if(reachedEndOfList(i)) loadMoreData();
    return row;
}
private boolean reachedEndOfList(int position) {
    // can check if close or exactly at the end
    return position == list.size() - 4;
}

private void loadMoreData() {
    if(isNetworkAvailable()) {
        new MyAdapter.AsyncTaskParseJson().execute(url);
    }else{

        Toast.makeText(mContext, "Sorry No internet Connection Found!", Toast.LENGTH_SHORT).show();

    }
}
public class AsyncTaskParseJson extends AsyncTask<String,String ,void> {

    JSONArray dataJsonArr = null;
    ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {

        progressDialog = ProgressDialog.show(mContext,
                "Movie Pal!",
                "Loading Movies List..");
    }

    @Override
    protected void doInBackground(String... arg0) {



        String yourJsonStringUrl = arg0[0];
         yourJsonStringUrl=yourJsonStringUrl+"&page="+pageNmber;
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl.replace(" ","%20"));

        System.out.println(json);
        try {
        JSONArray jsonArray = json.getJSONArray("results");
        for (int i=0; i<jsonArray.length(); i++) {
            Movie newMovie = new Movie();

           //setting to class object

            list.add(newMovie);
        }
        pageNmber++;} catch (JSONException e) {
            e.printStackTrace();
        }
        return json;

    }

    @Override
    protected void onPostExecute() {
        progressDialog.dismiss();

    }
}

I don't know what i am missing here please help!


Solution

  • You need to call notifyDataSetChanged (or similar function like notifyDataSetInsert) when the new data is loaded so it knows that more data exists and it should allow more scrolling.

    Also, you may not be adding enough padding (just 4 items) between the end of the list and when you fetch more data. That may result in stuttering scrolling- you'll reach the end and stop until more data is downloaded, then you can scroll again. As an example- I start fetching new data 10 items before the end, and I'm just hitting a local db, not a remote server.