在上培训课的时候,老师一直在将ViewPager是现在的主流,一直想去好好的了解一下,今天去网上学习了一下 ,做一个总结:
ViewPager其实就是后来谷歌提供给我们的一个组件,就像TextView,Button一样,只不过它是ViewGroup,在使用它前要有v4的包,既然是组件,当然要在xml中定义,
标签的样式基本是这样:
<android.support.v4.view.ViewPager android:id="viewPager" android:layout_width="wrap_content" android:layout_height="wrap_content" />
在xml文件内定义好了以后。接下来就是编辑Activity文件,最重要的就是实现一个适配器PagerAdapter,使用的方法和ListView里面实现BaseAdapter是一样的,下面是代码:
PagerAdapter myPagerAdapter=new PagerAdapter(){ @Override public int getCount() { return viewList.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } @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); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } };
适配器必须要实现的是四个方法:分别是getCount(),isViewFromObject(View arg0, Object arg1),instantiateItem(ViewGroup container, int position),destroyItem(ViewGroup container, int position,Object object)具体的使用就看文档了。在适配器里面我们还可以定义标签等一些属性。
下面是全部代码:
Activity:
package com.example.viewpager; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.PagerTitleStrip; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.Button; public class MainActivity extends Activity { private ViewPager viewPager=null; private List<View> viewList=null; private List<String> titleList=null; private PagerTabStrip pageTab=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setValue(); } private void setValue(){ viewPager=(ViewPager) findViewById(R.id.viewPager); pageTab=(PagerTabStrip) findViewById(R.id.pagerTab); pageTab.setTextSpacing(20); pageTab.setTabIndicatorColor(Color.YELLOW); pageTab.setDrawFullUnderline(false); pageTab.setBackgroundColor(Color.BLUE); LayoutInflater flater=LayoutInflater.from(this); View view1=flater.inflate(R.layout.layout_01, null); View view2=flater.inflate(R.layout.layout_02, null); View view3=flater.inflate(R.layout.layout_03, null); viewList=new ArrayList<View>(); viewList.add(view1); viewList.add(view2); viewList.add(view3); titleList=new ArrayList<String>(); titleList.add("标签一"); titleList.add("标签二"); titleList.add("标签三"); PagerAdapter myPagerAdapter=new PagerAdapter(){ @Override public int getCount() { return viewList.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } @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); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } }; viewPager.setAdapter(myPagerAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" > <android.support.v4.view.PagerTabStrip android:id="@+id/pagerTab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" /> </android.support.v4.view.ViewPager> </LinearLayout>
其他三个子页面我就不复制了。