public class MainActivity extends Activity { private ViewPager viewPager; //声明一个ViewPager组件 private PagerTabStrip tabTitle; //声明ViewPager标题 private View tab01,tab02,tab03,tab04; //声明ViewPager中的几个标签页 private List<View> listView; //声明一个View类型List用于装载 ViewPager中的几个标签页 private List<String> pagerTitle;//声明一个Stirng类型的List用于装载ViewPager标题 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建活动时调用 initViewPager(); } private void initViewPager() { //获取ViewPager中的几个标签页 tab01 = LayoutInflater.from(this).inflate(R.layout.tab01, null); tab02 = LayoutInflater.from(this).inflate(R.layout.tab02, null); tab03 = LayoutInflater.from(this).inflate(R.layout.tab03, null); tab04 = LayoutInflater.from(this).inflate(R.layout.tab04, null); //实例化listView同时装载 ViewPager中的几个标签页 listView = new ArrayList<View>(); listView.add(tab01); listView.add(tab02); listView.add(tab03); listView.add(tab04); //获取ViewPager标题 tabTitle = (PagerTabStrip) this.findViewById(R.id.tabstrip); //实例化pagerTitle同时装载ViewPager标题 pagerTitle = new ArrayList<String>(); pagerTitle.add("选项卡01"); pagerTitle.add("选项卡02"); pagerTitle.add("选项卡03"); pagerTitle.add("选项卡04"); //获取ViewPager组件 viewPager = (ViewPager) this.findViewById(R.id.MyViewPager); //为viewpager设置适配器 viewPager.setAdapter(new PagerAdapter() { @Override public CharSequence getPageTitle(int position) { return pagerTitle.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(listView.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(listView.get(position)); return listView.get(position); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } @Override public int getCount() { return listView.size(); } }); } //activity_main.xml <RelativeLayout 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.shi.viewpager.MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/MyViewPager" android:layout_height="match_parent" android:layout_width="match_parent"> <android.support.v4.view.PagerTabStrip android:id="@+id/tabstrip" android:layout_width="wrap_content" android:layout_height="50dip" android:gravity="center" /> </android.support.v4.view.ViewPager> </RelativeLayout> //tab01.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:layout_height="match_parent" android:layout_width="match_parent" android:text="01" /> </LinearLayout> //tab02.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:layout_height="match_parent" android:layout_width="match_parent" android:text="02" /> </LinearLayout> //tab03.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:layout_height="match_parent" android:layout_width="match_parent" android:text="03" /> </LinearLayout> //tab04.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:layout_height="match_parent" android:layout_width="match_parent" android:text="04" /> </LinearLayout> //图不弄上来!哈哈!谁会告诉一下谢谢
Android ViewPager简单的使用方法
猜你喜欢
转载自a754782339.iteye.com/blog/2265048
今日推荐
周排行