============问题描述============
main.xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="100.0dip" android:background="#FFFFFF" > <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="页卡1" android:textColor="#000000" android:textSize="22.0dip" /> <TextView android:id="@+id/text2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="页卡2" android:textColor="#000000" android:textSize="22.0dip" /> <TextView android:id="@+id/text3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="页卡3" android:textColor="#000000" android:textSize="22.0dip" /> </LinearLayout> <ImageView android:id="@+id/cursor" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaleType="matrix" android:src="@drawable/a" /> <android.support.v4.view.ViewPager android:id="@+id/vPager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:background="#000000" android:flipInterval="30" android:persistentDrawingCache="animation" /> </LinearLayout> lay1.xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#158684" > <Button android:id="@+id/lay1_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="lay1的按钮"/> </LinearLayout> lay2.xml代码与lay3.xml代码和lay1代码差不多 activity代码: package com.demo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; /** * Tab页面手势滑动切换以及动画效果 * * @author D.Winter * */ public class MainActivity extends Activity { // ViewPager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。 // android-support-v4.jar private ViewPager mPager;// 页卡内容 private List<View> listViews; // Tab页面列表 private ImageView cursor;// 动画图片 private TextView t1, t2, t3;// 页卡头标 private int offset = 0;// 动画图片偏移量 private int currIndex = 0;// 当前页卡编号 private int baseNum = 3000; private int nowbaseNum = 3000; private int bmpW;// 动画图片宽度 private LayoutInflater inflater; private View view1, view2, view3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); InitLayout(); InitImageView(); InitTextView(); InitViewPager(); } /** * 初始化头标 */ private void InitTextView() { t1 = (TextView) findViewById(R.id.text1); t2 = (TextView) findViewById(R.id.text2); t3 = (TextView) findViewById(R.id.text3); t1.setOnClickListener(new MyOnClickListener(0)); t2.setOnClickListener(new MyOnClickListener(1)); t3.setOnClickListener(new MyOnClickListener(2)); } /** * 初始化ViewPager */ private void InitViewPager() { mPager = (ViewPager) findViewById(R.id.vPager); listViews = new ArrayList<View>(); LayoutInflater mInflater = getLayoutInflater(); listViews.add(view1); listViews.add(view2); listViews.add(view3); mPager.setAdapter(new MyPagerAdapter(listViews)); mPager.setCurrentItem(baseNum); mPager.setOnPageChangeListener(new MyOnPageChangeListener()); } private void InitLayout() { inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 第一个页面的布局 view1 = inflater.inflate(R.layout.lay1, null, false); Button button = (Button) view1.findViewById(R.id.lay1_button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("mxt", "点击了lay1按钮"); } }); // 第二个页面的布局 view2 = inflater.inflate(R.layout.lay2, null, false); Button button2 = (Button) view2.findViewById(R.id.lay2_button); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("mxt", "点击了lay2按钮"); } }); // 第三个页面的布局 view3 = inflater.inflate(R.layout.lay3, null, false); Button button3 = (Button) view3.findViewById(R.id.lay3_button); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("mxt", "点击了lay3按钮"); } }); } /** * 初始化动画 */ private void InitImageView() { cursor = (ImageView) findViewById(R.id.cursor);// 页卡下面的横条 bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a) .getWidth();// 获取图片宽度 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW = dm.widthPixels;// 获取分辨率宽度 offset = (screenW / 3 - bmpW) / 2;// 计算偏移量 Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0); cursor.setImageMatrix(matrix);// 设置动画初始位置 } /** * ViewPager适配器 */ public class MyPagerAdapter extends PagerAdapter { public List<View> mListViews; public MyPagerAdapter(List<View> mListViews) { this.mListViews = mListViews; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { Log.i("POS", "执行了destroyItem"); // if (arg1 >= mListViews.size()-1) { // int newPosition = arg1%mListViews.size(); // arg1 = newPosition; // ((ViewPager) arg0).removeView(mListViews.get(arg1)); // } } @Override public void finishUpdate(View arg0) { } @Override public int getCount() { // return mListViews.size(); return Integer.MAX_VALUE; } @Override public Object instantiateItem(View arg0, int arg1) { Log.i("POS", "执行了instantiateItem"); if (arg1 >= mListViews.size() - 1) { int newPosition = arg1 % mListViews.size(); arg1 = newPosition; } try { ((ViewPager) arg0).addView(mListViews.get(arg1), 0); } catch (Exception e) { e.printStackTrace(); } return mListViews.get(arg1); } // public Object instantiateItem(View container, int position) { // // TODO Auto-generated method stub // return super.instantiateItem(container, position); // } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == (arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } } /** * 头标点击监听 */ public class MyOnClickListener implements View.OnClickListener { private int index = 0; public MyOnClickListener(int i) { index = i; } @Override public void onClick(View v) { if (currIndex != index) { switch (index) { case 0: if (currIndex == 1) { mPager.setCurrentItem(nowbaseNum -1); } else if (currIndex == 2) { mPager.setCurrentItem(nowbaseNum +1); } break; case 1: if (currIndex == 0) { mPager.setCurrentItem(nowbaseNum +1); } else if (currIndex == 2) { mPager.setCurrentItem(nowbaseNum -1); } break; case 2: if (currIndex == 1) { mPager.setCurrentItem(nowbaseNum +1); } else if (currIndex == 0) { mPager.setCurrentItem(nowbaseNum -1); } break; default: break; } } } }; /** * 页卡切换监听 */ public class MyOnPageChangeListener implements OnPageChangeListener { int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量 int two = one * 2;// 页卡1 -> 页卡3 偏移量 @Override public void onPageSelected(int arg0) { Animation animation = null; int pos = arg0 % listViews.size(); nowbaseNum = arg0; switch (pos) { case 0: if (currIndex == 1) { animation = new TranslateAnimation(one, 0, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, 0, 0, 0); } break; case 1: if (currIndex == 0) { animation = new TranslateAnimation(offset, one, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, one, 0, 0); } break; case 2: if (currIndex == 0) { animation = new TranslateAnimation(offset, two, 0, 0); } else if (currIndex == 1) { animation = new TranslateAnimation(one, two, 0, 0); } break; } currIndex = pos; animation.setFillAfter(true);// True:图片停在动画结束位置 animation.setDuration(300); cursor.startAnimation(animation); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } } }============解决方案1============
代码太多不想看。。帮你顶一下吧。。