ViewPage滑动切换点击切换
2014-12-04 17:35
197 查看
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; 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.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class ViewPageActivity extends Activity { private ViewPager mPager; private List<View> listViews; private ImageView cursor; private TextView t1, t2, t3; private int offset = 0; private int currIndex; private int bmpW; private int tabW; private Toast mToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_page); t1 = (TextView) findViewById(R.id.tv_top1); t2 = (TextView) findViewById(R.id.tv_top2); t3 = (TextView) findViewById(R.id.tv_top3); mPager = (ViewPager) findViewById(R.id.vPage); listViews = new ArrayList<View>(); LayoutInflater mInflater = getLayoutInflater(); listViews.add(mInflater.inflate(R.layout.ly1, null)); listViews.add(mInflater.inflate(R.layout.ly2, null)); listViews.add(mInflater.inflate(R.layout.ly3, null)); 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; tabW = screenW / 3; Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0); cursor.setImageMatrix(matrix); System.out.println("bmpW" + bmpW + "offset" + offset + "screenW" + screenW+"tabW"+tabW); mPager.setAdapter(new MyPagerAdapter(listViews)); mPager.setCurrentItem(0); mPager.setOnPageChangeListener(new MyOnPageChangeListener()); t1.setOnClickListener(new MyOnClickListener(0)); t2.setOnClickListener(new MyOnClickListener(1)); t3.setOnClickListener(new MyOnClickListener(2)); } public class MyOnClickListener implements OnClickListener { private int index = 0; public MyOnClickListener(int i) { index = i; } @Override public void onClick(View v) { mPager.setCurrentItem(index); if (mToast == null) { mToast = Toast.makeText(getApplicationContext(), "index" + index, Toast.LENGTH_SHORT); }else{ mToast.setText("index" + index); } mToast.show(); } } public class MyPagerAdapter extends PagerAdapter { public List<View> mListView; public MyPagerAdapter(List<View> mlistViews) { this.mListView = mlistViews; } @Override public int getCount() { System.out.println(listViews.size() + "listViews"); return listViews.size(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); container.removeView(mListView.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { // return super.instantiateItem(container, position); container.addView(mListView.get(position), 0); return mListView.get(position); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; // return false; } } public class MyOnPageChangeListener implements OnPageChangeListener { int one = offset * 2 + bmpW; int two = one * 2; @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { Animation animation = null; animation = new TranslateAnimation(tabW*currIndex,tabW*arg0 , 0, 0); currIndex = arg0; animation.setFillAfter(true); animation.setDuration(300); cursor.startAnimation(animation); } } }
布局:
<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" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:id="@+id/linlayout_top" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <TextView android:gravity="center" android:id="@+id/tv_top1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="page1" /> <TextView android:gravity="center" android:id="@+id/tv_top2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="page2" /> <TextView android:gravity="center" android:id="@+id/tv_top3" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="page3" /> </LinearLayout> <ImageView android:id="@+id/cursor" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="matrix" android:src="@drawable/a"/> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/vPage" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="#000000" android:persistentDrawingCache="animation" /> </LinearLayout>
viewpage的item布局:
<?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:background="#11ff12" > </LinearLayout>
相关文章推荐
- Android 通过ViewPager实现点击和滑动切换Fragment标签页
- 禁止ViewPager左右滑动,只能点击切换
- Android技术——视图切换(二)借助ViewPage实现Tab滑动导航
- 点击查看大图,支持左右滑动切换、缩放图片
- 浅析iOS多视图滑动点击切换的集成
- viewpager+fragment的懒加载实现微信点击和滑动切换功能(切换效果)
- 利用 ViewPage 实现类似TabHost的功能,仅点击才可切换屏
- Android之ViewPager+Fragment实现页面点击切换和手势滑动
- ViewPager+Fragment实现滑动效果,并且能够点击切换
- Android ViewPager加Fragment实现滑动或者点击页面切换
- ViewPager+Fragment支持导航滑动以及点击切换,触发替换某个tab对应的fragment
- 点击按钮切换页面,版面滑动切换
- 多视图滑动点击切换极速集成
- 最近解决tab滑动及点击快速切换,快速点击压力测试系统会崩溃问题心得
- Android中Fragment点击切换与添加ViewPager滑动切换
- Android简单的Fragment+Viewpager切换(可滑动和点击切换)
- Viewpager和Fragment点击滑动切换
- jquery_mobile移动端幻灯片效果,滑动切换,点击切换
- ViewPager+GridView实现水平滑动和点击背景色切换-填坑
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击