实现ViewPager要适配器,一般的使用PagerAdapter,对于fragment,我们所使用的是FragmentPagerAdapter和FragmentPagerStateAdapter。
FragementPagerAdapter和FragmentPagerStateAdapter的区别
FragmentPagerAdapter和FragmentPagerStateAdapter都继承自PagerAdapter。
FragmentPagerAdapter中每生成一个Fragment都会保存在内存里,比较适合相对静态的,数量也比较少的页。
FragmentPagerStateAdapter适合处理有很多页,并且数据动态性较大、占用内存较多的情况。
与FragmentPagerAdapter一致,是继承自PagerAdapter,但是正如”State”所表明的含义一样,该PagerAdapter的实现将只保留当前页面,当页面离开时,就会被消除,释放其资源。在页面需要显示时,生成新的页面。这样实现的好处就是当拥有大量的页面时,不必在内存中占用大量的内存。
FragmentPagerAdapter的派生类,只需要重写getItem(int position)和getCount()就行了。使用Fragment的List对象来保存Fragment。
public class FragAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
public FragAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
fragmentList=fragments;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
创建两个Fragement类
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment1"
/>
</LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment2"
/>
</LinearLayout>
Fragment1.class
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view= inflater.inflate(R.layout.fragment1, container, false);
return view;
}
}
Fragment2.class
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view= inflater.inflate(R.layout.fragment2, container, false);
return view;
}
}
MainActivity中
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="vpfd.com.viewpagerframent.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
MainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//构造适配器
List<Fragment> fragments=new ArrayList<Fragment>();
fragments.add(new Fragment1());
fragments.add(new Fragment2());
FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments);
//设定适配器
ViewPager vp = (ViewPager)findViewById(R.id.vp);
vp.setAdapter(adapter);
}
}
最后效果如图