ViewPager不同架構(gòu)方式的區(qū)別
由普通layout構(gòu)成的viewPager
一般用于展示類的頁(yè)面(如新的app打開出現(xiàn)的引導(dǎo)頁(yè))
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的個(gè)數(shù) ????@Override ????public?int?getCount()?{ ????????return?viewList.size(); ????} ????//view?是否來(lái)源于對(duì)象 ????@Override ????public?boolean?isViewFromObject(View?view,?Object?o)?{ ????????return?view?==?o; ????} ????//實(shí)例化一個(gè)?頁(yè)卡 ????@Override ????public?Object?instantiateItem(ViewGroup?container,?int?position)?{ ????????container.addView(viewList.get(position)); ????????return?viewList.get(position); ????} ????//銷毀一個(gè)?頁(yè)卡 ????@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("第一頁(yè)"); ????????titleList.add("第二頁(yè)"); ????????titleList.add("第三頁(yè)"); ????????titleList.add("第四頁(yè)"); ????????MyPagerAdapter?adapter?=?new?MyPagerAdapter(viewList,titleList); ????????ViewPager?viewPager?=?(ViewPager)findViewById(R.id.pager); ????????viewPager.setAdapter(adapter); ????} }
由Fragment構(gòu)成的ViewPager1
使用FragmentPagerAdapter適配器(不會(huì)自動(dòng)銷毀超過(guò)三個(gè)的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構(gòu)成的ViewPager2
使用FragmentStatePagerAdapter適配器(超過(guò)三個(gè)會(huì)自動(dòng)銷毀)
只需要適配器繼承FragmentStatePagerAdapter即可