结合PagerTabStrip实现界面切换
MainActivity
package com.example.viewpagertest; import java.util.ArrayList; 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.View; import android.view.ViewGroup; import android.view.animation.AlphaAnimation; import android.widget.Toast; public class MainActivity extends Activity implements OnPageChangeListener { private ViewPager viewPager; private PagerTabStrip pagerTabStrip; private String[] titles = { "金泰妍1", "金泰妍2", "金泰妍3", "金泰妍4" }; private ArrayList<View> views = new ArrayList<View>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewpager); pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagertab); initViews(); viewPager.setAdapter(new MyPagerAdapter(views)); pagerTabStrip.setBackgroundColor(getResources().getColor( android.R.color.holo_blue_light)); pagerTabStrip.setTabIndicatorColor(getResources().getColor( android.R.color.holo_blue_bright)); pagerTabStrip.setTextColor(getResources().getColor( android.R.color.white)); viewPager.setOnPageChangeListener(this); } //加载视图 private void initViews() { views.add(getLayoutInflater().inflate(R.layout.layout_1, null)); views.add(getLayoutInflater().inflate(R.layout.layout_2, null)); views.add(getLayoutInflater().inflate(R.layout.layout_3, null)); views.add(getLayoutInflater().inflate(R.layout.layout_4, null)); } class MyPagerAdapter extends PagerAdapter { private ArrayList<View> views; public MyPagerAdapter(ArrayList<View> views) { this.views = views; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } // 实例化选项卡 public Object instantiateItem(ViewGroup container, int position) { View v = views.get(position); container.addView(v); return v; } // 删除选项卡 public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(views.get(position)); } // 判断视图是否为返回的对象 public boolean isViewFromObject(View view, Object o) { // TODO Auto-generated method stub return view == o; } // 至少要实现以上四个方法:getCount、instantiateItem、destroyItem、isViewFromObject // 获取标题 public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titles[position]; } } /* * (non-Javadoc) * * @see android.support.v4.view.ViewPager.OnPageChangeListener# * onPageScrollStateChanged(int) */ @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see * android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled * (int, float, int) */ @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see * android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected * (int) */ @Override public void onPageSelected(int position) { // TODO Auto-generated method stub // 滑动页卡实现渐变动画,并且Toast选中信息 View v = views.get(position); AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); alphaAnimation.setDuration(3000); v.startAnimation(alphaAnimation); Toast.makeText(MainActivity.this, "你选择了" + titles[position], Toast.LENGTH_SHORT).show(); } }
<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" tools:context="com.example.viewpagertest.MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > <android.support.v4.view.PagerTabStrip android:id="@+id/pagertab" android:layout_width="wrap_content" android:layout_height="wrap_content" 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" > <ImageView android:id="@+id/img1" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/a" /> </LinearLayout>
效果图
加餐:http://blog.csdn.net/wangjinyu501/article/details/8169924
http://blog.csdn.net/xiaanming/article/details/8966621