How to implement a paging feature in Android
One day I needed to create a paging feature in Android. Something to create a swipe action in Android, like the home screen. After searching in Google, I discovered that was possible to make this effects using a HorizontalScrollView. It’s a good idea. So, I adapted some codes according my necessity. I created a class (PaginationLayout). You just need to add elements into this layout and it will do all work.
Basically the PaginationLayout class creates two views: a bar with “Previous” and “Next” buttons and a horizontal scroll, one below the other. The paging effect is built using the scroll. Gestures are calculated to move and stop just to pass an exact page (visible screen size). It also applies to the click event of the buttons.
The complete code the project is exposed in the Github: here or direct download zip file here
A video was added on Youtube to quickly show the feature:
Usage example:
public class Sample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // creates the Pagination Layout PaginationLayout paginationLayout = new PaginationLayout(this); paginationLayout.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // creates content only for sample TableLayout table = new TableLayout(this); TableRow row = new TableRow(this); TableRow row2 = new TableRow(this); table.addView(row); .... and blablabla ... for(int i = 0; i< 50;i++){ Button button = new Button(this); button.setText("Button " + i); if (i%2==0) { row.addView(button); } else { row2.addView(button); } } // add the content in pagination paginationLayout.addView(table); // set pagination layout setContentView(paginationLayout); } } }