Android uses ViewPager, Fragment, and PagerTabStrip to achieve multi-page sliding

Source: Internet
Author: User

This article mainly introducesHow to Use ViewPager, Fragment, and PagerTabStrip to achieve multi-page sliding. That is, the effect of the google play homepage and Sina Weibo message (at, comment, private message, broadcast) page. ViewPager + Fragment is a combination of google recommendation methods, which is much more efficient than TabActivity + Activity.

1. Add the android support package
Because the above classes are provided only in the android support package, we should first add the package.
In Eclipse-> Window-> Android SDK Manager, select Extras-> Android Support Library in the list for installation. After the download is complete, go to the android-sdk \ extras \ android \ support directory, and select v4 To Go To The v4 directory, copy the android-support-v4.jar file to the libs directory of the project (if not created) and the ADT will automatically import it to the project during compilation.

2. Create layout for ViewPager, The content is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <android.support.v4.view.ViewPager        android:id="@+id/viewPager"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" >        <android.support.v4.view.PagerTabStrip                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="top" />    </android.support.v4.view.ViewPager></LinearLayout>

ViewPager is used to manage layout and display page data by swiping left and right. PagerTabStrip is used to display the page title. android: layout_gravity = "top" indicates that the title is on the top, and bottom can be set.

 

3. Create a FragmentActivity page
The FragmentActivity page contains the ViewPager element, which can be used to display Fragment. The definition is as follows:

FragmentActivity page public class ViewPagerDemo extends FragmentActivity {/** page list **/List <Fragment> fragmentList = new ArrayList <Fragment> (); /** page title list **/List <String> titleList = new ArrayList <String> (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. view_pager_demo); ViewPager vp = (ViewPager) findViewById (R. id. ViewPager); fragmentList. add (new ViewPagerFragment1 ("Page 1"); fragmentList. add (new ViewPagerFragment1 ("Page 2"); fragmentList. add (new ViewPagerFragment1 ("Page 3"); titleList. add ("title 1"); titleList. add ("title 2"); titleList. add ("title 3"); vp. setAdapter (new myPagerAdapter (getSupportFragmentManager (), fragmentList, titleList);}/*** define adapter *** @ author gxwu@lewatek.com 2012-11-15 */class myPagerA Dapter extends FragmentPagerAdapter {private List <Fragment> fragmentList; private List <String> titleList; public myPagerAdapter (FragmentManager fm, List <Fragment> fragmentList, List <String> titleList) {super (fm); this. fragmentList = fragmentList; this. titleList = titleList;}/*** get each page */@ Override public Fragment getItem (int arg0) {return (fragmentList = null | fragmentList. size () = 0 )? Null: fragmentList. get (arg0);}/*** title of each page */@ Override public CharSequence getPageTitle (int position) {return (titleList. size ()> position )? TitleList. get (position): "";}/*** total number of pages */@ Override public int getCount () {return fragmentList = null? 0: fragmentList. size ();}}}

MyPagerAdapter is integrated from ragmentPagerAdapter to provide a data source for ViewPager.

The onCreate function obtains the ViewPager instance and sets the data source. getsuppfrfragmentmanager indicates that the Fragment manager is obtained. ViewPagerFragment1 indicates a specific page, which is described below.

 

4. Create a Fragment page
The Fragment page is the page to be displayed by sliding left and right. You can create a new class to integrate Fragment and rewrite the onCreateView function. The onCreateView function is equivalent to the onCreate function of the Activity. As follows:

Public class ViewPagerFragment1 extends Fragment {private String text; private TextView TV = null; public ViewPagerFragment1 (String text) {super (); this. text = text;}/*** overwrites this function. The inflater inflate function is used to obtain the view and returns the final result */@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View v = inflater. inflate (R. layout. view_pager_fragment_demo1, container, false); TV = (TextView) v. findViewById (R. id. viewPagerText); TV. setText (text); return v ;}}

The simple effect is as follows:

Free source code: http://download.csdn.net/detail/lan410812571/5859567

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.