Sunday, May 21, 2017

How do i implement Endless Scroll Using Android Volley and Laravel as api?

I've been searching for this all my life...(well not all my life but for a very long time now).. I've seen libraries and other codes explaining how to implement this. From what i've read so far, i have to use pagination and request for a set of data at a time but i still don't know how to make that request using volley.

My App is supposed to work like instagram and I'm using Recyclerview. I want to load a set of data at a time so that when the user gets to buttom, another set will be loaded.

This is my activity_main.xml

<android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mSwipeRefresh">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/mRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"/>

    </android.support.v4.widget.SwipeRefreshLayout>

This is how i'm making the volley request in my MainActivity.java

String url = "http://url/api/get_items";
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Log.d(TAG, response);
            dealArrayList = new JsonConverter<Deal>().toArrayList(response, Deal.class);
            adapter = new DealAdapter(getApplicationContext(),dealArrayList);
            mRecyclerView.setAdapter(adapter);
            LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
            mRecyclerView.setLayoutManager(manager);
            adapter.notifyDataSetChanged();



        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, error.toString());
            Toast.makeText(getApplicationContext(), "Bad Network Connection. Please Try Again", Toast.LENGTH_LONG).show();

        }
    }
    );

    int socketTimeout = 30000; // 30 seconds. You can change it
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

    stringRequest.setRetryPolicy(policy);
    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
    mSwipeRefresh.setRefreshing(false);

Thanks in advance.



via Max Billionaire

Advertisement