endless recyclerview along with load onscroll from server in recyclerview using cardview to load images and text

自古美人都是妖i 提交于 2019-12-03 22:58:12

The concept behind Pagination :

  1. You pass additional variable/value to backend(in your case PHP) let's say "last_seen". For initial fetching of values you will pass the last_seen value as 0, this means php will have a logic of getting values after this value upto some limit(eg. PHP will send the results to android from index 1 to 20).
  2. And as you know your upper limit is 20, so next time at page load that is in RecyclerView.OnScrollListener you will call the PHP again. But this time your last_seen will be 20 and PHP will provide you with results from 21 to 40, and so on.

The concept for Swipe-to-Refresh :

While Implementing Swipe-to-Refresh, it has a method named onRefresh() you will apply the initial PHP call here with last_seen as 0 and it will fetch the initial data from backend.

For Implementation go through following links :

  1. Endless RecyclerView OnScrollListener
  2. http://android-pratap.blogspot.in/2015/01/endless-recyclerview-onscrolllistener.html
  3. Basic Pagination Tutorial (PHP).

I also written a new blog post for the requirement you asked here. Check this Android Feed Example.

You have to create a pagination script on your server. I used the following code.

<?php 

//Getting the page number which is to be displayed  
$page = $_GET['page'];  

//Initially we show the data from 1st row that means the 0th row 
$start = 0; 

//Limit is 3 that means we will show 3 items at once
$limit = 3; 

//Importing the database connection 
require_once('dbConnect.php');

//Counting the total item available in the database 
$total = mysqli_num_rows(mysqli_query($con, "SELECT id from feed "));

//We can go atmost to page number total/limit
$page_limit = $total/$limit; 

//If the page number is more than the limit we cannot show anything 
if($page<=$page_limit){

    //Calculating start for every given page number 
    $start = ($page - 1) * $limit; 

    //SQL query to fetch data of a range 
    $sql = "SELECT * from feed limit $start, $limit";

    //Getting result 
    $result = mysqli_query($con,$sql); 

    //Adding results to an array 
    $res = array(); 

    while($row = mysqli_fetch_array($result)){
        array_push($res, array(
            "name"=>$row['name'],
            "publisher"=>$row['publisher'],
            "image"=>$row['image'])
            );
    }
    //Displaying the array in json format 
    echo json_encode($res);
}else{
        echo "over";
}

The rest is same.. additional I added a listener to load more records on the list after reaching the bottom of the list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!