Android--ViewPager-Fragment
阅读原文时间:2023年07月09日阅读:1

package com.cnn.viewpager02;

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.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;

//因为V4 继承 FragmentActivity
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
private List titleList;
private List fragmentList;
private PagerTitleStrip tabStrip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.viewPage1);  
    tabStrip = (PagerTitleStrip) findViewById(R.id.tab);  
    //tabStrip.set

    fragmentList = new ArrayList<Fragment>();  
    fragmentList.add(new Fragment01());  
    fragmentList.add(new Fragment02());  
    fragmentList.add(new Fragment03());

    titleList = new ArrayList<String>();  
    titleList.add("第一页");  
    titleList.add("第二页");  
    titleList.add("第一页");  
    //FragmentManager fm = getSupportFragmentManager();  
    MyFragmentPagerAdapter adapter  
    = new MyFragmentPagerAdapter( getSupportFragmentManager(), titleList, fragmentList);  
    viewPager.setAdapter(adapter);  
}  

}

package com.cnn.viewpager02;

import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private List titleList;
private List fragmentList;

public MyFragmentPagerAdapter(FragmentManager fm,List<String> titleList,List<Fragment> fragmentList) {  
    super(fm);  
    // TODO 自动生成的构造函数存根  
    this.titleList = titleList;  
    this.fragmentList = fragmentList;  
}

@Override  
public CharSequence getPageTitle(int position) {  
    // TODO 自动生成的方法存根  
    return titleList.get(position);  
}

@Override  
public Fragment getItem(int arg0) {  
    // TODO 自动生成的方法存根  
    return fragmentList.get(arg0);  
}

@Override  
public int getCount() {  
    // TODO 自动生成的方法存根  
    return fragmentList.size();  
}

}

package com.cnn.viewpager02;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment01 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO 自动生成的方法存根
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.item1, container, false);
}
}

package com.cnn.viewpager02;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment02 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO 自动生成的方法存根
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.item2, container, false);
}
}

package com.cnn.viewpager02;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment03 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO 自动生成的方法存根
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.item3, container, false);
}
}



    </android.support.v4.view.PagerTitleStrip>  
</android.support.v4.view.ViewPager>  


<TextView  
    android:id="@+id/textView1"  
    android:layout\_width="wrap\_content"  
    android:layout\_height="wrap\_content"  
    android:text="第一页" />


<TextView  
    android:id="@+id/textView1"  
    android:layout\_width="wrap\_content"  
    android:layout\_height="wrap\_content"  
    android:text="第二页" />


<TextView  
    android:id="@+id/textView1"  
    android:layout\_width="wrap\_content"  
    android:layout\_height="wrap\_content"  
    android:text="第三页" />