标签:
MainActivity的使用
1 package viewpagedemo.example.administrator.viewpagedemo; 2 3 import android.app.Activity; 4 import android.graphics.Color; 5 import android.support.v4.view.PagerTabStrip; 6 import android.support.v4.view.PagerTitleStrip; 7 import android.support.v4.view.ViewPager; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.view.View; 11 12 import java.util.ArrayList; 13 import java.util.List; 14 /*ViewPager实现(界面)导航,eg视图滑动 15 加入ViewPager 16 <android.support.v4.view.ViewPager 17 android:id="@+id/pager" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent" 20 android:layout_gravity="center"> 21 </android.support.v4.view.ViewPager> 22 23 加载显示的页卡 24 将Layout布局转换为View对象 25 1/ LayoutInflater if=getLayoutInflater().from(this); 26 if.inflate(resource,root); 27 2/ view.inflate(context,resource,root); 28 29 ViewPager可以看成容器可以装view对象或者Fragment对象 30 31 配置适配器Adapter 32 1/PagerAdapter 数据源:List<View> 33 2/FragmentPagerAdapter 数据源: List<Fragment> 34 3/FragmentStatePagerAdapter 数据源: List<Fragment>*/ 35 public class MainActivity extends Activity { 36 private PagerTabStrip tabStrip; 37 private List<View>viewList; 38 private ViewPager viewPager; 39 40 private List<String> titleList; 41 private PagerTabStrip pagerTabStrip; 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.main); 46 /*通过View对象建立ViewPager的数据源*/ 47 viewList=new ArrayList<View>(); 48 View view1 = View.inflate(this, R.layout.view1, null); 49 View view2 = View.inflate(this, R.layout.view2, null); 50 View view3 = View.inflate(this, R.layout.view3, null); 51 View view4 = View.inflate(this, R.layout.view4, null); 52 viewList.add(view1); 53 viewList.add(view2); 54 viewList.add(view3); 55 viewList.add(view4); 56 titleList=new ArrayList<String>(); 57 titleList.add("第一页"); 58 titleList.add("第二页"); 59 titleList.add("第三页"); 60 titleList.add("第四页"); 61 tabStrip= (PagerTabStrip) findViewById(R.id.paperTab); 62 tabStrip.setBackgroundColor(Color.RED);/*设置背景颜色,Color.RED,大写*/ 63 tabStrip.setTextColor(Color.YELLOW);/*设置文本颜色*/ 64 tabStrip.setDrawFullUnderline(false);/*去掉长线*/ 65 tabStrip.setTabIndicatorColor(Color.GREEN);/*设置短线颜色*/ 66 // 初始化viewPager 67 viewPager= (ViewPager) findViewById(R.id.viewPager); 68 // 创建PagerAdapter的适配器 69 MyPagerAdapt adapt = new MyPagerAdapt(viewList,titleList); 70 // ViewPager加载适配器 71 viewPager.setAdapter(adapt); 72 } 73 }
MyPagerAdapt继承自PagerAdapter
1 package viewpagedemo.example.administrator.viewpagedemo; 2 3 import android.support.v4.view.PagerAdapter; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.ListView; 7 8 import java.util.List; 9 10 /** 11 * Created by Administrator on 2016/5/7. 12 */ 13 /*返回的是页卡的数量*/ 14 public class MyPagerAdapt extends PagerAdapter { 15 private List<View>viewList; 16 private List<String>titleList; 17 public MyPagerAdapt(List<View>viewList,List<String> titleList){ 18 this.viewList=viewList; 19 this.titleList=titleList; 20 } 21 @Override 22 public int getCount() { 23 return viewList.size(); 24 } 25 /*View是否来自对像*/ 26 @Override 27 public boolean isViewFromObject(View arg0, Object arg1) { 28 29 return arg0==arg1; 30 } 31 /*实例化页卡*/ 32 @Override 33 public Object instantiateItem(ViewGroup container, int position) { 34 container.addView(viewList.get(position)); 35 return viewList.get(position); 36 } 37 /*销毁页卡*/ 38 @Override 39 public void destroyItem(ViewGroup container, int position, Object object) { 40 container.removeView(viewList.get(position)); 41 } 42 /* 43 * 设置ViewPager的标题*/ 44 @Override 45 public CharSequence getPageTitle(int position) { 46 return titleList.get(position); 47 } 48 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <android.support.v4.view.ViewPager 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:id="@+id/viewPager" 9 android:layout_gravity="center"> 10 <android.support.v4.view.PagerTabStrip 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:id="@+id/paperTab" 14 android:layout_gravity="top"></android.support.v4.view.PagerTabStrip> 15 <android.support.v4.view.PagerTitleStrip 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:id="@+id/paperTitle" 19 android:layout_gravity="bottom"></android.support.v4.view.PagerTitleStrip> 20 21 </android.support.v4.view.ViewPager> 22 </LinearLayout> 23 <!--android.support.v4.view.PagerTabStrip是android.support.v4.view.ViewPager子标签-->
view1
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:text="第一个界面"/> 9 </LinearLayout>
view2
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:text="第二个界面"/> 9 </LinearLayout>
view3
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:text="第三个界面"/> 9 </LinearLayout>
view4
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:text="第四个界面"/> 9 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/633sylss/p/5470913.html