标签:
功能:滑动切换视图,具有标题栏。
1.在布局文件中加入android.support.v4.view.ViewPager:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/vp_pager" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.PagerTitleStrip android:id="@+id/vts_id" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v4.view.ViewPager> </LinearLayout>
2.为显示每个ViewPager页面新建xml文件:vp_page1.xml,vp_page2.xml,vp_page3.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="TextView2" /> </LinearLayout>
3.在Activity中新建内部类并关联PagerAdapter:
public class ViewPagerDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_viewpager_demo); ViewPager vp = (ViewPager) findViewById(R.id.vp_pager); LayoutInflater inflater = getLayoutInflater(); View view1 = inflater.inflate(R.layout.vp_page1, null); View view2 = inflater.inflate(R.layout.vp_page2, null); View view3 = inflater.inflate(R.layout.vp_page3, null); List<View> views = new ArrayList<View>(); views.add(view1); views.add(view2); views.add(view3); List<String> titles = new ArrayList<String>(); titles.add("标题1"); titles.add("标题2"); titles.add("标题3"); MyViewPagerAdapter adapter = new MyViewPagerAdapter(views, titles); vp.setAdapter(adapter); } // 把View关联到ViewPager上 class MyViewPagerAdapter extends PagerAdapter { private List<View> views; private List<String> titles; public MyViewPagerAdapter() { } public MyViewPagerAdapter(List<View> views, List<String> titles) { this.views = views; this.titles = titles; } // 设置ViewPager标题 @Override public CharSequence getPageTitle(int position) { return titles.get(position); } // Viewpage有几个界面 @Override public int getCount() { return views.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } // 添加一个界面 @Override public Object instantiateItem(ViewGroup container, int position) { View view = views.get(position); ViewPager viewPager = (ViewPager) container; viewPager.addView(view); return view; } // 删除一个界面 // container:ViewPager // post:当前界面的位置 // object:view对象 @Override public void destroyItem(ViewGroup container, int position, Object object) { View view = views.get(position); ViewPager viewPager = (ViewPager) container; viewPager.removeView(view); } } }
ViewPager可滑动页面+PagerTitleStrip标题
标签:
原文地址:http://www.cnblogs.com/mada0/p/4826979.html