Android--ViewPager-Fragment
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<String> titleList; private List<Fragment> 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<String> titleList; private List<Fragment> 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); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewPage1" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v4.view.PagerTitleStrip android:id="@+id/tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" > </android.support.v4.view.PagerTitleStrip> </android.support.v4.view.ViewPager> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一页" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二页" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三页" /> </LinearLayout>