详解Android App中创建ViewPager组件的方法

现在很多app一打开就是一个ViewPager,然后可以用手指滑,每滑一次就换一张图,底下还会有圈圈表示说现在滑到第几章~

通常这些图片都是放功能简介或是使用教学之类的,我的需求很简单,就是上面提到的那样而已。

有两种做法,一种是找现有套件,查了一堆资料每个都跟我推荐ViewPagerIndicator这套,我之前也看过这套,只是看起来需要有fragment再加上google play范例好像载不到了,所以只好自己实做一个。

Viewpager的实作可参考Android ViewPager使用详解里面的程序码:

@Overrideprotected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_intro);

mViewPager = (ViewPager) findViewById(R.id.viewpager);

final LayoutInflater mInflater = getLayoutInflater().from(this);

View v1 = mInflater.inflate(R.layout.intro_layout_1, null);

View v2 = mInflater.inflate(R.layout.intro_layout_2, null);

View v3 = mInflater.inflate(R.layout.intro_layout_3, null);

View v4 = mInflater.inflate(R.layout.intro_layout_4, null);

viewList = new ArrayList<View>();

viewList.add(v1);

viewList.add(v2);

viewList.add(v3);

viewList.add(v4);

mViewPager.setAdapter(new MyViewPagerAdapter(viewList));

mViewPager.setCurrentItem(0);}

MyViewPagerAdapter

public class MyViewPagerAdapter extends PagerAdapter {

private List<View> mListViews;

public MyViewPagerAdapter(List<View> mListViews) {

this.mListViews = mListViews;

}

@Override

public void destroyItem(ViewGroup container, int position, Object object) {

container.removeView((View) object);

}

@Override

public Object instantiateItem(ViewGroup container, int position) {

View view = mListViews.get(position);

container.addView(view);

return view;

}

@Override

public int getCount() {

return mListViews.size();

}

@Override

public boolean isViewFromObject(View arg0, Object arg1) {

return arg0==arg1;

}}

这样子你就有一个ViewPager了


下面我们来具体看一下ViewPager的用法:

一、ViewPager创建步骤

① 在XML布局中加入android.support.v4.view.ViewPager

② 加载显示的页卡将Layout布局转换为View对象

  (1) 

LayoutInflater lf getLayoutInflater().from(this);

lf.inflate(resource,root);

  (2)

View.inflate(context,resource,root);

③ 配置Adapter(三种Adapter)

 (1)PagerAdapter 数据源:List<View>

 (2)FragmentPagerAdapter 数据源:List<Fragment>

 (3)FragmentStatePagerAdapter 数据源:List<Fragment>

二、代码示例

新建四个Fragment和他们的布局下面是MainActivity的xml布局:

<android.support.v4.view.ViewPager

android:id="@+id/pager"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center" >

<android.support.v4.view.PagerTabStrip

android:id="@+id/tab"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="top" >

</android.support.v4.view.PagerTabStrip>

<!-- 底部显示标题与上面的顶部显示不能同时出现

<android.support.v4.view.PagerTitleStrip

android:id="@+id/title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom" >

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

-->

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

第一种PagerAdapter

public class MyPagerAdapter extends PagerAdapter {

private List<View> viewList;

private List<String> titleList;

public MyPagerAdapter(List<View> viewList,List<String> titleList){

this.viewList = viewList;

this.titleList = titleList;

}

/**

* 返回页卡的数量

*/

@Override

public int getCount() {

return viewList.size();

}

/**

* view是否来自对象

*/

@Override

public boolean isViewFromObject(View arg0, Object arg1) {

return arg0==arg1;

}

/**

* 实例化一个页卡

*/

@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));

}

/**

* 设置ViewPager的标题

*/

@Override

public CharSequence getPageTitle(int position) {

return titleList.get(position);

}

}

第二种FragmentPagerAdapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

private List<Fragment> fragList;

private List<String> titleList;

public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment> fragList,List<String> titleList) {

super(fm);

this.fragList = fragList;

this.titleList = titleList;

}

@Override

public Fragment getItem(int arg0) {

return fragList.get(arg0);

}

@Override

public CharSequence getPageTitle(int position) {

return titleList.get(position);

}

@Override

public int getCount() {

return fragList.size();

}

}

第三种FragmentStatePagerAdapter(该适配器可以动态销毁Fragment)

public class MyFragmentPagerAdapter2 extends FragmentStatePagerAdapter {//该适配器可以动态销毁

private List<Fragment> fragList;

private List<String> titleList;

public MyFragmentPagerAdapter2(FragmentManager fm,List<Fragment> fragList,List<String> titleList) {

super(fm);

this.fragList = fragList;

this.titleList = titleList;

}

@Override

public Fragment getItem(int arg0) {

return fragList.get(arg0);

}

@Override

public CharSequence getPageTitle(int position) {

return titleList.get(position);

}

@Override

public int getCount() {

return fragList.size();

}

@Override

public Object instantiateItem(ViewGroup arg0, int arg1) {

return super.instantiateItem(arg0, arg1);

}

@Override

public void destroyItem(ViewGroup container, int position, Object object) {

super.destroyItem(container, position, object);

}

}

MainActivity里使用getSupportFragmentManager()该Activity必须继承FragmentActivity:

public class MainActivity extends FragmentActivity implements OnPageChangeListener{

private List<View> viewList;

private List<String> titleList;

private ViewPager pager;

private PagerTabStrip tab;//顶部标题

private List<Fragment> fragList;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

viewList = new ArrayList<View>();

titleList = new ArrayList<String>();

tab = (PagerTabStrip) findViewById(R.id.tab);

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.add(view1);

viewList.add(view2);

viewList.add(view3);

viewList.add(view4);

fragList = new ArrayList<Fragment>();

fragList.add(new Fragment1());

fragList.add(new Fragment2());

fragList.add(new Fragment3());

fragList.add(new Fragment4());

//为ViewPager页卡设置标题

titleList.add("第一页");

titleList.add("第二页");

titleList.add("第三页");

titleList.add("第四页");

//为PagerTabStrip设置一些属性

tab.setBackgroundColor(Color.WHITE);

tab.setDrawFullUnderline(false);

tab.setTabIndicatorColor(Color.BLUE);

pager = (ViewPager) findViewById(R.id.pager);

//MyPagerAdapter adapter = new MyPagerAdapter(viewList,titleList);

//MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList, titleList);

/**

* 使用getSupportFragmentManager()该Activity必须继承FragmentActivity

*/

MyFragmentPagerAdapter2 adapter = new MyFragmentPagerAdapter2(getSupportFragmentManager(), fragList, titleList);

pager.setAdapter(adapter);

pager.setOnPageChangeListener(this);

}

@Override

public void onPageScrollStateChanged(int arg0) {

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override

public void onPageSelected(int arg0) {

Toast.makeText(this,"当前是第"+(arg0+1)+"个界面", 0).show();

}

}

以上是 详解Android App中创建ViewPager组件的方法 的全部内容, 来源链接: utcz.com/z/330113.html

回到顶部