码迷,mamicode.com
首页 > 其他好文 > 详细

Tab系列 03 FragmentPagerAdapter + Fragment + ViewPager

时间:2016-07-07 22:36:04      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

 

使用ViewPager + Fragment  + FragmentPagerAdapter 实现tab内容切换

创建一个Activity继承FragmentActivity

适配器: FragmentPagerAdapter 

 

与tab系列01 ViewPager + xml 的区别, 

ViewPager + Fragment 的区别

Adapter 加载数据

 

 

 

 

 

 

 

 

    package com.example.tabdemo01;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
    TextView tv_header;
    Fragment fm1;
    Fragment fm2;
    Fragment fm3;
    Fragment fm4;
    private FragmentPagerAdapter  adapter;
    ViewPager viewPager;
    private List<Fragment> mFragments = new ArrayList<Fragment>();  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //将所有的Fragment 隐藏
        onweixin(null);
    }

    public void initView() {
        tv_header = (TextView) findViewById(R.id.tv_headr);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        Fragment fm1 = new Fragment01();
        Fragment fm2 = new Fragment02();
        Fragment fm3 = new Fragment03();
        Fragment fm4 = new Fragment04();
        mFragments.add(fm1);
        mFragments.add(fm2);
        mFragments.add(fm3);
        mFragments.add(fm4);
        
        
        adapter = new FragmentPagerAdapter( getSupportFragmentManager()) {

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                // TODO Auto-generated method stub
                return mFragments.get(arg0);
            }
        };
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            
            @Override
            public void onPageSelected(int arg0) {
                switch (arg0) {
                case 0:
                    onweixin(null);
                    break;
                case 1:
                    onFriend(null);
                    break;
                case 2:
                    onContacts(null);
                    break;
                case 3:
                    onMyself(null);
                    break;
                default:
                    break;
                }
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }
    public void onweixin(View view){    
        tv_header.setText("微信");
        viewPager.setCurrentItem(0);
    }
    public void onFriend(View view){
        tv_header.setText("好友");
        viewPager.setCurrentItem(1);
    }
    public void onContacts(View view){
        tv_header.setText("联系人");
        viewPager.setCurrentItem(2);
    }
    public void onMyself(View view){
        tv_header.setText("我的");
        viewPager.setCurrentItem(3);
    }
    
    
    /**
     * 将制定fragment 隐藏Fragment
     * @param fragment
     */
    public void hideFragments(FragmentTransaction ft, Fragment...fragment){
        for (Fragment fragment2 : fragment) {
            if(fragment2 != null){
                ft.hide(fragment2);
            }
        }
    }


}

 

Tab系列 03 FragmentPagerAdapter + Fragment + ViewPager

标签:

原文地址:http://www.cnblogs.com/chengbao/p/5651738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!