Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement pagination in Android listview

Tags:

I am working for Android app, in which I need to show listview with items. But There are more elements to show in listview.

I decided to implement pagination . I tried searching in Google but does not find any related information.

Can anybody help me please..

like image 306
Naveen Avatar asked May 21 '13 03:05

Naveen


People also ask

How to implement infinite scrolling (pagination) in listview?

We face many requirements in our daily application development for infinite scrolling (pagination)in ListView and we are like. We start creating ListView then create a list of widgets and set scroll listener and set error or failure scenarios and then lastly, stop the pagination when data loading is completed.

What are the different types of pagination in Android?

There are various types of pagination that can be applied to mobile apps: Numbered Pagination. Infinite/Endless Pagination. In this piece we want to explore all sorts of pagination examples for android. Android ListView Next/Previous Pagination Tutorial

What is pagination in Android recyclerview?

Pagination is one of the most important factors which helps to reduce the loading time inside our app and increase the performance of our data which is represented in the form of Lists. In this article, we will take a look at adding pagination in our Android RecyclerView .

How to use cardviews as viewitems in listview?

In your android studio go to File — New — New Project. Type Project Name. Choose minimum sdk. From templates choose empty activity. Step 2 – Add CardView Dependency. The second step is to add CardView as a dependency in your build.gradle (Module:app). This is beacuse we will use CardViews as Viewitems in our ListView.


1 Answers

Implementing pagination is very simple.

Take look at this...

public class MainActivity extends Activity {        private ListView listview;     private TextView title;     private ArrayList<String> data;     ArrayAdapter<String> sd;       public int TOTAL_LIST_ITEMS = 1030;     public int NUM_ITEMS_PAGE   = 100;     private int noOfBtns;     private Button[] btns;      public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          listview = (ListView)findViewById(R.id.list);         title    = (TextView)findViewById(R.id.title);          Btnfooter();          data = new ArrayList<String>();          /*          * The ArrayList data contains all the list items          */         for(int i=0;i<TOTAL_LIST_ITEMS;i++)         {             data.add("This is Item "+(i+1));         }          loadList(0);          CheckBtnBackGroud(0);      }      private void Btnfooter()     {         int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;         val = val==0?0:1;         noOfBtns=TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE+val;          LinearLayout ll = (LinearLayout)findViewById(R.id.btnLay);          btns = new Button[noOfBtns];          for(int i=0;i<noOfBtns;i++)         {             btns[i] =   new Button(this);             btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));             btns[i].setText(""+(i+1));              LinearLayout.LayoutParams lp = new     LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);             ll.addView(btns[i], lp);              final int j = i;             btns[j].setOnClickListener(new OnClickListener() {                  public void onClick(View v)                 {                     loadList(j);                     CheckBtnBackGroud(j);                 }             });         }      }      /**      * Method for Checking Button Backgrounds      */     private void CheckBtnBackGroud(int index)     {         title.setText("Page "+(index+1)+" of "+noOfBtns);         for(int i=0;i<noOfBtns;i++)         {             if(i==index)             {                 btns[index].setBackgroundDrawable(getResources().getDrawable(R.drawable.box_green));                 btns[i].setTextColor(getResources().getColor(android.R.color.white));             }             else             {                 btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));                 btns[i].setTextColor(getResources().getColor(android.R.color.black));             }         }      }      /**      * Method for loading data in listview      * @param number      */     private void loadList(int number)     {         ArrayList<String> sort = new ArrayList<String>();          int start = number * NUM_ITEMS_PAGE;         for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++)         {             if(i<data.size())             {                 sort.add(data.get(i));             }             else             {                 break;             }         }         sd = new ArrayAdapter<String>(this,                 android.R.layout.simple_list_item_1,                 sort);         listview.setAdapter(sd);     } } 

Xml file:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >  <TextView     android:id="@+id/title"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:textColor="@android:color/black"     android:gravity="center"     android:textSize="16sp"     android:background="@android:color/darker_gray"     android:padding="10dp"/>  <ListView     android:id="@+id/list"     android:divider="#000"     android:dividerHeight="1dp"     android:cacheColorHint="#00000000"     android:layout_width="fill_parent"     android:layout_height="0dp"     android:layout_weight="1"     android:fadingEdge="none"/>  <HorizontalScrollView     android:layout_width="fill_parent"     android:layout_height="wrap_content">     <LinearLayout         android:id="@+id/btnLay"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:orientation="horizontal" >     </LinearLayout> </HorizontalScrollView> </LinearLayout> 

For more clear explanation and source code visit this links

ListView Pagination Ex-1

ListView Pagination Ex-2

like image 140
Ram kiran Pachigolla Avatar answered Oct 21 '22 00:10

Ram kiran Pachigolla