Android ViewPager should and skills
1. The ViewPager function in android is similar to scrollView in iOS, and implements the most basic left and right sliding function of the page. This class exists in Google's compatibility package, so remember to add "android-support-v4.jar" to BuilldPath When referencing"
2. in implementation, ViewPager needs to bind an Adapter to return the number of page views and the specific form in the Adapter. This is similar to the tableView implementation in iOS. The numberOfCell and CellForRow in the UITableView proxy must be implemented. One is used to return the total amount of data, and the other is used to return each specific piece of data.
3. ViewPager is usually used with Fragment. Add multiple fragment (pages) to ViewPager to slide left and right.
4. Look at the simplest ViewPager code: Suppose we already have three Fragment, whose background colors are red, green, and blue respectively.
1) Add a ViewPager in the xml file. The Code is as follows:
2) In activtiy. this ViewPager is used in java. now we have ViewPager, and we need an Adapter. We usually inherit from the FragmentPagerAdapter to define our Adapter to rewrite the function of returning the number and Item. Define an array in Adaoter and add the above Fragment (if you want to dynamically add an added interface, OK). The Code is as follows:
public static class TabAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener { private ArrayList
frg = new ArrayList
(); public TabAdapter(FragmentManager fm) { super(fm); CountingFragment f1 = new CountingFragment(); frg.add(f1); CursorFragment f2 = new CursorFragment(); frg.add(f2); FragmentPagerSupport f3 = new FragmentPagerSupport(); frg.add(f3); notifyDataSetChanged(); } @Override public Fragment getItem(int i) { return frg.get(i); } @Override public int getCount() { return frg.size(); } @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { } @Override public void onPageScrollStateChanged(int i) { } }}
To listen to the event of ViewPager movement changes, we have implemented ViewPager. OnPageChangeListener, which implements three functions to see what the name means. GetCount returns the number of Fragment of ViewPager, and getItem returns the Item at each position.
3) Associate ViewPgaer with Adapter
mViewPager = (ViewPager)findViewById(R.id.mViewPager); mTabAdapter = new TabAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mTabAdapter);
Getsuppfrfragmentmanager returns a Fragment manager, which is a required parameter of the FragmentPagerAdapter.
After the above steps, we have implemented a simple ViewPager that can slide left and right.
Introduction to some attributes
When we need to press the button to control the left and right sliding of ViewPager, we use the attribute
Boolean |
ArrowScroll (int direction): parameter 1 for forward paging, parameter 2 for backward Paging |
Make ViewPager not respond to the sliding switching event. For example, make the nested child ViewPager not respond to the sliding switching event. Instead, it only responds to the click switching event and submits the sliding event to the parent ViewPager for processing ., Use
Boolean |
CanScrollHorizontally (int direction) Check if this view can be scrolled horizontally in a certain direction. |
@Override public boolean canScrollHorizontally(int direction) { // TODO Auto-generated method stub return false; }