标签:
view1源代码及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/view01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个界面"/> </LinearLayout>
package com.example.viewpager; import android.os.Bundle; import android.support.v4.app.Fragment;//兼容3.0以下的版本 import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view1, null); } }
view2,view3,view4同上
两个适配器:
myPagerAdapter:
package com.example.viewpager; 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>titleList; public MyPagerAdapter(List<View> viewList,List<String>titleList) { super(); this.viewList = viewList; this.titleList = titleList; } /** * 返回也可的数量 */ @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; } /** * 自动实例化和销毁页卡 * 当前显示第2个界面,滑到第3个界面,就会自动实例化第四界面,销毁第一个界面 * 实例化一个页卡 */ @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(viewList.get(position)); return viewList.get(position); } /** * 销毁一个页卡 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(viewList.get(position)); } /** * 设置viewPager页卡标题 * */ @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titleList.get(position); } }
myFragmentPagerAdapter:
package com.example.viewpager; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; /** * FragmentPagerAdapter * 该类没有自动创建和自动销毁的功能 * * FragmentStatePagerAdapter * 有自动创建和销毁页卡的功能 * 需重写instantiateItem();destroyItem(); * * */ public class MyFragmentPagerAdapter extends FragmentPagerAdapter{ private List<Fragment> fragmentList; private List<String> titleList; public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList) { super(fm); this.fragmentList = fragmentList; this.titleList = titleList; } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titleList.get(position); } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return fragmentList.get(arg0); } @Override public int getCount() { // TODO Auto-generated method stub return fragmentList.size(); } }
主程序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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.viewpager.MainActivity" > <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:id="@+id/strip" 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.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager> </LinearLayout>
源代码:
package com.example.viewpager; /* ViewPager可以使视图滑动 * ViewPager可以看做一个装载view对象或fragment的一个集合 * 加载显示的页卡 * 将layout布局转化为view对象和 * (1)LayoutInflater if = getLayoutInflater().from(this); * if.inflate(resouce,root); * (2)View.inflate(context,resouce,root); * * 配置适配器: * (1)PagerAdapter 数据源:List<View> * (2)FragmentPagerAdapter 数据源:List<Fragment> * (3)FragmentStatePagerAdapter 数据源:List<Fragment> 监听器的使用 OnPageChangeListener() --用来监控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.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends FragmentActivity implements OnPageChangeListener{ private List<View> viewList; private ViewPager pager; private PagerTabStrip strip; private List<String> titleList; private PagerTabStrip tab; private List<Fragment> fragmentList; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList = new ArrayList<View>(); titleList = new ArrayList<String>(); /** * 通过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); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); //为ViewPager设置标题 titleList.add("第一页"); titleList.add("第二页"); titleList.add("第三页"); titleList.add("第四页"); //为PagerTabStrip设置属性 tab = (PagerTabStrip) findViewById(R.id.strip); tab.setBackgroundColor(Color.BLACK); tab.setTextColor(Color.YELLOW); tab.setDrawFullUnderline(false); //创建PagerAdapter的适配器 MyPagerAdapter adapter = new MyPagerAdapter(viewList,titleList); //加载适配器 pager = (ViewPager) findViewById(R.id.pager); // pager.setAdapter(adapter); //通过Fragment对象最为ViewPager的数据源 fragmentList = new ArrayList<Fragment>(); fragmentList.add(new Fragment1()); fragmentList.add(new Fragment2()); fragmentList.add(new Fragment3()); fragmentList.add(new Fragment4()); //创建FragmentPagerAdapter的适配器 MyFragmentPagerAdapter fragmentAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, titleList); pager.setAdapter(fragmentAdapter); //设置监听器 pager.setOnPageChangeListener(this);; } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub Toast.makeText(this, "当前是第"+(arg0+1)+"个界面", Toast.LENGTH_SHORT).show(); } }
标签:
原文地址:http://www.cnblogs.com/fangg/p/5582889.html