package com.example.viewpagertest; import java.util.ArrayList; import java.util.List; import java.util.zip.Inflater; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; public class MainActivity extends Activity { private List<View> list; private ViewPager pager; private List<String> titList; private PagerTabStrip tabStrip;// 属于Pager的一个子标签 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); list = 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); list.add(view1); list.add(view2); list.add(view3); list.add(view4); // 为页卡设置标题 titList = new ArrayList<String>(); titList.add("第一页"); titList.add("第二页"); titList.add("第三页"); titList.add("第四页"); // 为PagerTabStrip设置一些属性 tabStrip = (PagerTabStrip) findViewById(R.id.tab); tabStrip.setTextColor(Color.BLACK); tabStrip.setBackgroundResource(R.drawable.ic_biaoti); tabStrip.setDrawFullUnderline(true);// 没有下划线 tabStrip.setTabIndicatorColor(Color.GRAY);// 粗线的颜色 // 初始化ViewPager pager = (ViewPager) findViewById(R.id.pager); // 创建PagerAdapter的适配器 MyPagerAdapter adapter = new MyPagerAdapter(list, titList); // viewPager加载适配器 pager.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
package com.example.viewpagertest; import java.util.List; import java.util.zip.Inflater; import android.app.Fragment; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class MyPagerAdapter extends PagerAdapter { private List<View> list; private List<String> titList; public MyPagerAdapter(List<View> list, List<String> titList) { // TODO Auto-generated constructor stub this.list = list; this.titList = titList; } /** * 返回的是页卡的数量 * * **/ @Override public int getCount() { // TODO Auto-generated method stub return list.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) { // TODO Auto-generated method stub container.addView(list.get(position)); return list.get(position); } /** * 销毁一个页卡 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(list.get(position)); } /**设置页卡的标题 * 在添加标题之后加一个getPagerTitle方法 * */ @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titList.get(position); } }
package com.example.viewpagertest; import android.app.Fragment; import android.os.Bundle; 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, container, false); } }
package com.example.viewpagertest; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view2, container, false); } }
package com.example.viewpagertest; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view3, container, false); } }
package com.example.viewpagertest; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment4 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view4, container, false); } }
<?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/pager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <android.support.v4.view.PagerTabStrip android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="60dp" android:layout_gravity="top" > </android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager> </LinearLayout>
<?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/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个页面" /> </LinearLayout>
<?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/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三个页面" />/> </LinearLayout>
<?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/text4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第四个页面" />/> </LinearLayout>
感悟:ViewPager即模仿微信可以左右滑屏。。。 在src下建立4个Fragment ,分别标识Fragment 1,Fragment 2,Fragment 3,Fragment 4.并在AndroidManifest中申明且分别建立4个布局文件View1,2 3 4.并引入到对应的Fragment中 ,注意要继承Fragment不是Activity且实现onCreatView方法,return inflater.inflate(int resource, ViewGroup root, boolean attachToRoot); 然后在main.xml中加入
<android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="wrap_content" android:layout_height="wrap_content" > <android.support.v4.view.PagerTabStrip android:id="@+id/tab" android:layout_width="wrap_content" android:layout_height="wrap_content" > </android.support.v4.view.PagerTabStrip>
分别定义
private ViewPager viewPager;
private List<View> list;
private List<String> titlelist;布局别忘记要R.layout.main
先使view1 2 3 4 布局文件转换成View对象View.inflate(Context context, int resource, ViewGroup root)
,titlelist集合实例化
然后把view 1 2 3 4加入到list集合中
MyPagerAdapter要继承PagerAdapter,继承构造方法并添加没有的构造方法,并把创建构造函数,添加List<View> list, List<String> titlelist 2 个参数
@Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(list.get(position)); return list.get(position); }
@Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(list.get(position)); }
//这里是destroyItem方法
然后在MAinActivity中
private ViewPager viewPager; 是ViewPager。。。。然后在MyPagerAdapter中添加
@Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titlelist.get(position); }
最后就是设置PagerTabStrip的一些参数
tabStrip = (PagerTabStrip) findViewById(R.id.tab); tabStrip.setTextColor(Color.BLACK); tabStrip.setBackgroundResource(R.drawable.ic_biaoti);//背景图片 tabStrip.setDrawFullUnderline(true);// 没有下划线 tabStrip.setTabIndicatorColor(Color.GRAY);// 粗线的颜色
OK。