Android Technology--View switch (ii) tab navigation with ViewPage

Source: Internet
Author: User

Early Android (ANDROID.SUPPORT.V4) provides a series of classes that support fragment sliding switching, mainly:

1, Viewpager: It fragment container, you can manage multiple fragment at the same time, and allow multiple fragment to provide animation effect when switching.

2, Fragmentpageradapter:adapter class, used to provide multiple fragment for Viewpager. This class usually needs to be extended.

3. Pagertitlestrip: Used in conjunction with Viewpager to display "Navigation bar" on Viewpager.

Second, the use of viewpage to achieve tab sliding navigation is as follows:

Step1: The extension gets its own fragmentpageradapter

Step2: The extension gets its own simpleonpagechangelistener. This main time is triggered when the Viewpager slide is selected, so in this case it is usually the code to place the switch fragment.

Step3: Add your own fragmentpageradapter and your own simpleonpagechangelistener to Viewpager.

Examples are as follows:

(this project was added on the basis of the "Android technology-sliding switch (i) tab navigation" project above)

/tab_nav/src/com/example/tab_nav/meinvfragment_v4.java file (actually this class is exactly the same as the meinvfragment in the previous section, just Meinvfragment_ V4 is inherited from Android.support.v4.app.Fragment only)

Package Com.example.tab_nav;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ImageView;
public class Meinvfragment_v4 extends Fragment
{
public static int[] Img_ids;
public static String Fragment_key = "Fragment_key";
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate)
{
View v = inflater.inflate (r.layout.fragment_layout, NULL);
ImageView IMG_MEINV = (ImageView) V.findviewbyid (R.ID.IMG_MEINV);
Gets the parameter bundle passed in when the fragment was created
Bundle args = Getarguments ();
Img_meinv.setimageresource (Img_ids[args.getint (Fragment_key));
return v;
}
}

/tab_nav/src/com/example/tab_nav/viewpageractivity.java file:

Package Com.example.tab_nav;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentManager;
Import Android.support.v4.app.FragmentPagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
public class Viewpageractivity extends fragmentactivity
Since fragmentpageradapter is defined only in ANDROID.SUPPORT.V4
And the instantiation of Android.support.v4.app.FragmentActivity needs to pass in Android.support.v4.app.FragmentManager,
This particular fragmentmanager can only be obtained by means of the fragmentactivity Getsupportfragmentmanager method,
So viewpageractivity must inherit android.support.v4.app.FragmentActivity.
{
@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_view_pager);
Meinvfragment_v4.img_ids = new int[] {r.drawable.linzhiling,
R.drawable.liuyan, R.drawable.yangmi};
Viewpager pager = (Viewpager) Findviewbyid (R.id.pager);
Add Fragmentpageradapter for Viewpager
Myfragmentpageradapter adapter = new Myfragmentpageradapter (
Getsupportfragmentmanager ());
Pager.setadapter (adapter);
Add an event listener for Viewpager
Mysimpleonpagechangelistener listener = new Mysimpleonpagechangelistener (
Pager);
Pager.setonpagechangelistener (listener);
}
Extended to get their own fragmentpageradapter
Class Myfragmentpageradapter extends Fragmentpageradapter
{
Public Myfragmentpageradapter (fragmentmanager FM)
{
Super (FM);
TODO auto-generated Constructor stub
}
Gets the fragment of the specified location
@Override
Public Fragment getItem (int arg0)
{
Fragment f = new meinvfragment_v4 ();
Bundle B = new bundle ();
B.putint (Meinvfragment.fragment_key, arg0);
F.setarguments (b);
return F;
}
Indicates that the adapter contains the number of fragment
@Override
public int GetCount ()
{
TODO auto-generated Method Stub
return MeiNvFragment_V4.img_ids.length;
}
@Override
Public charsequence getpagetitle (int position)
{
Return "First" + (position + 1) + "beauty";
}
}
Extended to get their own simpleonpagechangelistener
Class Mysimpleonpagechangelistener extends Simpleonpagechangelistener
{
Private Viewpager Pager;
Public Mysimpleonpagechangelistener (Viewpager p)
{
pager = p;
}
public void onpageselected (int position)
{
Pager.setcurrentitem (position);
}
}
}
/tab_nav/res/layout/activity_view_pager.xml File:

<android.support.v4.view.viewpager xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/pager"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<android.support.v4.view.pagertitlestrip
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_gravity= "Top" >
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
Effects after starting the viewpageractivity:


is sliding from the first beauty to the second beauty.


Android Technology--View switch (ii) tab navigation with ViewPage

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.