ViewPager不同架构方式的区别
扫描二维码
随时随地手机看文章
由普通layout构成的viewPager
一般用于展示类的页面(如新的app打开出现的引导页)
activity_main.xml
定义适配器
package com.example.administrator.viewpager; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * Created by Administrator on 2015/7/22. */ public class MyPagerAdapter extends PagerAdapter{ private ListviewList; private ListtitleList; public MyPagerAdapter(ListviewList,ListtitleList){ this.viewList = viewList; this.titleList = titleList; } //返回viewPager的个数 @Override public int getCount() { return viewList.size(); } //view 是否来源于对象 @Override public boolean isViewFromObject(View view, Object o) { return view == o; } //实例化一个 页卡 @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } //销毁一个 页卡 @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } }
MainActivity.java
package com.example.administrator.viewpager; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewParent; import java.util.ArrayList; import java.util.List; public class MainActivity extends ActionBarActivity { private ListviewList; private ListtitleList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View view1 = View.inflate(this,R.layout.view1,null); View view2 = View.inflate(this,R.layout.view2,null); View view3 = View.inflate(this,R.layout.view3,null); View view4 = View.inflate(this,R.layout.view4,null); viewList = new ArrayList(); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); titleList = new ArrayList(); titleList.add("第一页"); titleList.add("第二页"); titleList.add("第三页"); titleList.add("第四页"); MyPagerAdapter adapter = new MyPagerAdapter(viewList,titleList); ViewPager viewPager = (ViewPager)findViewById(R.id.pager); viewPager.setAdapter(adapter); } }
由Fragment构成的ViewPager1
使用FragmentPagerAdapter适配器(不会自动销毁超过三个的pager
MyFragmentPagerAdapter
package com.example.administrator.viewpager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /** * Created by Administrator on 2015/7/22. */ public class MyFragmentPagerAdapter extends FragmentPagerAdapter{ private ListfragmentList; private ListtitleList; public MyFragmentPagerAdapter(FragmentManager fm, ListfragmentList,ListtitleList) { super(fm); this.fragmentList = fragmentList; this.titleList = titleList; } @Override public Fragment getItem(int i) { return fragmentList.get(i); } @Override public int getCount() { return fragmentList.size(); } @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } }
MainActivity
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList,titleList); ViewPager viewPager = (ViewPager)findViewById(R.id.pager); viewPager.setAdapter(adapter);
由Fragment构成的ViewPager2
使用FragmentStatePagerAdapter适配器(超过三个会自动销毁)
只需要适配器继承FragmentStatePagerAdapter即可