pagerTabStrip例子
viewpager(pagerTabStrip)例子:
<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.example.viewpagertest.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/viewpager" > <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>
package com.example.viewpagertest; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; public class MainActivity extends Activity { ViewPager mViewPager = null; PagerTabStrip mTabStrip = null; PagerAdapter mPagerAdapter = null; List<View> mViewContainer = new ArrayList<View>(); List<String> mTitleContainer = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPager = (ViewPager) findViewById(R.id.viewpager); mTabStrip = (PagerTabStrip) findViewById(R.id.tabstrip); mTabStrip.setDrawFullUnderline(false); mTabStrip.setBackgroundColor(0x000000); mTabStrip.setTabIndicatorColor(0xff0000); //mTabStrip.setTextSpacing(20); View view01 = LayoutInflater.from(this).inflate(R.layout.fragment01, null); View view02 = LayoutInflater.from(this).inflate(R.layout.fragment02, null); View view03 = LayoutInflater.from(this).inflate(R.layout.fragment03, null); mViewContainer.add(view01); mViewContainer.add(view02); mViewContainer.add(view03); mTitleContainer.add("login"); mTitleContainer.add("title"); mTitleContainer.add("demo"); mViewPager.setAdapter(new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return super.getItemPosition(object); } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return mTitleContainer.get(position); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(mViewContainer.get(position)); return mViewContainer.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(mViewContainer.get(position)); } @Override public int getCount() { // TODO Auto-generated method stub return mViewContainer.size(); } }); } @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); } }