[Android UI design and development] 1. Guide interface (I) ViewPager introduction and simple implementation, androidviewpager
1. ViewPager implementation
2. ViewPager implementation function
The ViewPager class provides new effects for multi-interface switching. The new effects have the following features:
<1> one of the interfaces is displayed;
<2> when a user slides between the left and right pages, the current screen displays the current interface and a part of the next interface;
<3> after sliding, the page automatically jumps to the selected page.
3.ViewPager
Android-support-v4.jar is a software package officially provided by Google to US compatible with low version Android devices, which contains only in Android 3.0 and above can be used api. Viewpager is one of them. We can do many things, from the simplest navigation to the page menu. So how can we use it? Like ListView, we also need an adapter, Which is PagerAdapter.
ViewPager official document address: http://developer.android.com/reference/android/support/v4/view/ViewPager.html
4. Use of ViewPager
Use it in three steps:
1. Add this component to the layout file.
<android.support.v4.view.ViewPager
android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" >
Note: This component is used to display the left-right sliding interface. If the xml layout file is not loaded, it will not display the content.
2. Load the page card to be displayed
LayoutInflater lf = getLayoutInflater (). from (this); view1 = lf. inflate (R. layout. layout1, null); view2 = lf. inflate (R. layout. layout2, null); view3 = lf. inflate (R. layout. layout3, null); viewList = new ArrayList <View> (); // load the View to be displayed by page into the viewList array. add (view1); viewList. add (view2); viewList. add (view3 );
3. instantiate the ViewPager component in the Activity and set its Adapter (that is, PagerAdapter, with the same method as ListView). Here, we generally need to override PagerAdapter.
Public class MyViewPagerAdapter extends PagerAdapter {private List <View> mListViews; public MyViewPagerAdapter (List <View> mListViews) {this. mListViews = mListViews; // constructor. The parameter is our page card, which is convenient. } @ Override public void destroyItem (ViewGroup container, int position, Object object Object) {container. removeView (mListViews. get (position); // Delete the page card} @ Override public Object instantiateItem (ViewGroup container, int position) {// This method is used to instantiate the page card container. addView (mListViews. get (position), 0); // Add the page card return mListViews. get (position) ;}@ Override public int getCount () {return mListViews. size (); // number of returned page cards} @ Override public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}}
The adapter of ViewPager is PagerAdapter. It is a base class that provides an adapter to fill the page inside ViewPager. You may want to use a more specific implementation, such as FragmentPagerAdapter or FragmentStatePagerAdapter. It should be noted that ViewPager should be used with Fragment, at least Google thinks so officially, but below 3.0, we do not need to do so. Note that when you implement a PagerAdapter, you must cover at least the following methods:
instantiateItem(ViewGroup, int)destroyItem(ViewGroup, int, Object)getCount()isViewFromObject(View, Object)
The official documentation address for PagerAdapter is: http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
Source code: https://github.com/YeXiaoChao/Yc_ui_viewpager
Source: http://blog.csdn.net/yangyu20121224/article/details/8980917