ViewPager achieve Tab

Since the space of the phone itself is not so big, so in most cases we will use to achieve the expansion of mobile phone tab page. Today I mainly wrote this blog realize how ViewPager tab.
Here we need to be ready layout.
Write pictures described here
A title layout, the layout of a bottom, the middle is a ViewPager control, here I prepared three tab layout.
Write pictures described here

Layout files not do too much explanation, mainly referring to the next activity inside use ViewPager achieve Tab.
First declare several objects

    private ViewPager mViewPager;
    private List<View> mlist=new ArrayList<>();
    private PagerAdapter mAdapter;

    private LinearLayout mL1;
    private LinearLayout mL2;
    private LinearLayout mL3;
    private ImageButton ml1_img;
    private ImageButton ml2_img;
    private ImageButton ml3_img;

The second line of code declares a View List collection type, save each tab page.
ViewPager need to declare PagerAdapter adapter.
The rest of the code is the first picture inside the control declaration.

    LayoutInflater mInflater=LayoutInflater.from(this);
        View tab_01=mInflater.inflate(R.layout.tab1,null);
        View tab_02=mInflater.inflate(R.layout.tab2,null);
        View tab_03=mInflater.inflate(R.layout.tab3,null);

        mlist.add(tab_01);
        mlist.add(tab_02);
        mlist.add(tab_03);

List tab added to the collection after which, the statement PagerAdapter inner classes.

mAdapter=new PagerAdapter() {

            @Override
            public int getCount() {
                return mlist.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mlist.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view=mlist.get(position);
                container.addView(view);
                return view;
            }
        };

The default method before two generation, we need to again rewrite the two methods, one is to initialize item, one is to remove item.
After PagerAdapter finished, () method linked by setAdapter ViewPager of.

mViewPager.setAdapter(mAdapter);

At this point we run slide around, can achieve page switching, but the bottom of the page bottom without any change. The next blog say how to achieve bottom of the following page switching control and consistency.

Published 37 original articles · won praise 10 · views 10000 +

Guess you like

Origin blog.csdn.net/OneLinee/article/details/78926945
Recommended