Courtesy: GitHub

Android HorizontalPaging Sample

This sample shows how to implement tabs, using Fragments and a ViewPager.

Introduction

This sample implements tabs using the deprecated ActionBar.TabListener. It uses ViewPager and FragmentPagerAdapter to handle swiping between tabs and displaying the selected tab content.

  1. Create an Activity that extends FragmentActivity, with a ViewPager for its layout.
  2. Implement ActionBar.TabListener interface.
  3. Create a class that extends FragmentPagerAdapter and override its getItem(int position), getCount() and getPageTitle(int position) methods.
  4. In the onCreate(Bundle savedInstanceState) method of your activity, set navigation mode to tabs for the ActionBar using setNavigationMode(ActionBar.NAVIGATION_MODE_TABS). Note: This is DEPRECATED as of Android Lollipop.
  5. Set your custom FragmentPagerAdapter on your ViewPager.
  6. Implement setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()) on your ViewPager to know the selected tab, so you can update your ActionBar with setSelectedNavigationItem(position).

Pre-requisites

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Screenshots

Screenshot Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-HorizontalPaging

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile "com.android.support:support-v4:26.1.0"

compile "com.android.support:gridlayout-v7:26.1.0"

compile "com.android.support:cardview-v7:26.1.0"

compile "com.android.support:appcompat-v7:26.1.0"

compileSdkVersion 26

minSdkVersion 11

targetSdkVersion 26

package com.example.android.horizontalpaging.tests

versionCode 1

versionName 1.0

package com.example.android.horizontalpaging

versionCode 1

versionName 1.0

MainActivity

MainActivity

The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will keep every loaded fragment in memory. If this becomes too memory intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.

The {@link ViewPager} that will host the section contents.

Create the activity. Sets up an {@link android.app.ActionBar} with tabs, and then configures the {@link ViewPager} contained inside R.layout.activity_main. *

A {@link SectionsPagerAdapter} will be instantiated to hold the different pages of fragments that are to be displayed. A {@link android.support.v4.view.ViewPager.SimpleOnPageChangeListener} will also be configured to receive callbacks when the user swipes between pages in the ViewPager. * @param savedInstanceState

Update {@link ViewPager} after a tab has been selected in the ActionBar. * @param tab Tab that was selected. @param fragmentTransaction A {@link android.app.FragmentTransaction} for queuing fragment operations to execute once this method returns. This FragmentTransaction does not support being added to the back stack.

Unused. Required for {@link android.app.ActionBar.TabListener}.

Unused. Required for {@link android.app.ActionBar.TabListener}.

A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the sections/tabs/pages. This provides the data for the {@link ViewPager}.

Get fragment corresponding to a specific position. This will be used to populate the contents of the {@link ViewPager}. * @param position Position to fetch fragment for. @return Fragment for specified position.

Get number of pages the {@link ViewPager} should render. * @return Number of fragments to be rendered as pages.

Get title for each of the pages. This will be displayed on each of the tabs. * @param position Page to fetch title for. @return Title for specified page.

A dummy fragment representing a section of the app, but that simply displays dummy text. This would be replaced with your application's content.

The fragment argument representing the section number for this fragment.