标签:
跟滑动页面一样的效果不过带个标题。
与滑动页面一样的步骤先配置六个页面
然后新建main.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" > <android.support.v4.view.ViewPager android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/pager" > <android.support.v4.view.PagerTabStrip android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:id="@+id/tab" ><!-- 最上的分类,换成PagerTitleStrip在底部 --> </android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager> </LinearLayout>
然后主文件
package com.example.deemo; import java.util.ArrayList; import java.util.List; import android.R.string; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.view.View; public class MainActivity extends Activity { private List<View>viewList;//初始化 private ViewPager pager; private PagerTabStrip tab; private List<String>titleList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); viewList=new ArrayList<View>(); //通过view对象作为ViewPager的数据源 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); View view5=View.inflate(this, R.layout.view5, null); View view6=View.inflate(this, R.layout.view6, null); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); viewList.add(view5); viewList.add(view6); //为viewpager页卡设置标题 titleList=new ArrayList<String>(); titleList.add("神户小鸟"); titleList.add("凤千早"); titleList.add("千里朱音"); titleList.add("中津静流"); titleList.add("此花露西娅"); titleList.add("篝"); //为tab设置属性,剩余的看看文档 tab=(PagerTabStrip) findViewById(R.id.tab); tab.setBackgroundColor(Color.BLACK); tab.setTextColor(Color.WHITE); tab.setDrawFullUnderline(false);//下划线? pager=(ViewPager) findViewById(R.id.pager);//初始化viewpager MyPagerAdapter adapter=new MyPagerAdapter(viewList,titleList);//创建适配器 pager.setAdapter(adapter);//加载适配器 } }
记得新建MyPagerAdapter.java进行配置
package com.example.deemo; import java.util.List; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class MyPagerAdapter extends PagerAdapter{ private List<View>viewList; private List<String>titleisList; public MyPagerAdapter(List<View>viewList,List<String>titleisList){ this.viewList=viewList; this.titleisList=titleisList; } //返回的是页卡的数量 @Override public int getCount() { // TODO Auto-generated method stub return viewList.size(); } //判断当前view是否来自对象 @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub 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)); } //设置标题页卡 @Override public CharSequence getPageTitle(int position) { return titleisList.get(position); } }
主要看最后一个设置标题。
标签:
原文地址:http://www.cnblogs.com/webgavin/p/5728648.html