android viewpager 学习使用总结
2016-07-01 11:32
429 查看
<span style="font-family: Arial, Helvetica, sans-serif;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span>
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#222222" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="50dp" android:background="#999999" android:gravity="center" android:text="页面1" android:textColor="#222222" /> <TextView android:id="@+id/textView2" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="50dp" android:background="#999999" android:gravity="center" android:text="页面2" android:textColor="#222222" /> <TextView android:id="@+id/textView3" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="50dp" android:background="#999999" android:gravity="center" android:text="页面3" android:textColor="#222222" /> </LinearLayout> <ImageView android:id="@+id/cursor" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaleType="matrix" android:src="@drawable/cursor" android:background="#222222"/> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
package com.example.myview;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.support.v4.view.ViewPager;
public class MyAdapter extends PagerAdapter{
List<View> viewLists;
public MyAdapter(List<View> lists)
{
viewLists = lists;
}
@Override
public int getCount() { //获得size
// TODO Auto-generated method stub
return viewLists.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
4000
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public void destroyItem(View view, int position, Object object) //销毁Item
{
((ViewPager) view).removeView(viewLists.get(position));
}
@Override
public Object instantiateItem(View view, int position) //实例化Item
{
((ViewPager) view).addView(viewLists.get(position), 0);
return viewLists.get(position);
}
}
package com.example.myview; import java.util.List; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.support.v4.view.ViewPager; import android.view.View; import android.widget.TextView; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.DisplayMetrics; import android.graphics.Matrix; import android.widget.ImageView; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; public class MainActivity extends Activity { private ViewPager viewPager; private ImageView imageView; private List<View> lists = new ArrayList<View>(); private MyAdapter myAdapter; private Bitmap cursor; private int offSet; private int currentItem; private Matrix matrix = new Matrix(); private int bmWidth; private Animation animation; private TextView textView1; private TextView textView2; private TextView textView3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById (R.id.cursor); textView1 = (TextView) findViewById (R.id.textView1); textView2 = (TextView) findViewById (R.id.textView2); textView3 = (TextView) findViewById (R.id.textView3); lists.add(getLayoutInflater().inflate(R.layout.layout1, null)); lists.add(getLayoutInflater().inflate(R.layout.layout2, null)); lists.add(getLayoutInflater().inflate(R.layout.layout3, null)); initeCursor(); myAdapter = new MyAdapter(lists); viewPager = (ViewPager) findViewById (R.id.viewPager); viewPager.setAdapter(myAdapter); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int arg0) { //当滑动式,顶部的imageView是通过animation缓慢的滑动 // TODO Auto-generated method stub switch (arg0) { case 0: if (currentItem == 1) { animation = new TranslateAnimation( offSet * 2 + bmWidth, 0 , 0, 0); } else if(currentItem == 2) { animation = new TranslateAnimation( offSet * 4 + 2 * bmWidth, 0, 0, 0); } break; case 1: if (currentItem == 0) { animation = new TranslateAnimation( 0, offSet * 2 + bmWidth, 0, 0); } else if (currentItem == 2) { animation = new TranslateAnimation( * offSet + 2 * bmWidth, offSet * 2 + bmWidth, 0, 0); } break; case 2: if (currentItem == 0) { animation = new TranslateAnimation( 0, 4 * offSet + 2 * bmWidth, 0, 0); } else if (currentItem == 1) { animation = new TranslateAnimation( offSet * 2 + bmWidth, 4 * offSet + 2 * bmWidth, 0, 0); } } currentItem = arg0; animation.setDuration(500); animation.setFillAfter(true); imageView.startAnimation(animation); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); textView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub viewPager.setCurrentItem(0); } }); textView2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub viewPager.setCurrentItem(1); } }); textView3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub viewPager.setCurrentItem(2); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private void initeCursor() { cursor = BitmapFactory.decodeResource(getResources(), R.drawable.cursor); bmWidth = cursor.getWidth(); DisplayMetrics dm; dm = getResources().getDisplayMetrics(); offSet = (dm.widthPixels - 3 * bmWidth) / 6; matrix.setTranslate(offSet, 0); imageView.setImageMatrix(matrix); //需要iamgeView的scaleType为matrix currentItem = 0; } }
代码都看的懂,三个页的xml就不增加了,页面也没有什么内容,都可以自己定义
相关文章推荐
- Android App开发中ViewPager组件的入门使用教程
- 使用ViewPager实现高仿launcher左右拖动效果
- Android App中用Handler实现ViewPager页面的自动切换
- Android应用中使用ViewPager实现类似QQ的界面切换效果
- Android中ViewPager组件的基本用法及实现图片切换的示例
- 灵活使用Android中ActionBar和ViewPager切换页面
- Android实现千变万化的ViewPager切换动画
- Android中viewPager使用指南
- Android 使用 ViewPager循环广告位的实现
- Android中ViewPager实现滑动指示条及与Fragment的配合
- 详解Android App中ViewPager使用PagerAdapter的方法
- Android viewpager中动态添加view并实现伪无限循环的方法
- Android应用中使用ViewPager和ViewPager指示器来制作Tab标签
- Android ViewPager制作新手导航页(动态加载)
- 详解Android App中创建ViewPager组件的方法
- Android App中ViewPager与Fragment结合的一些问题解决
- 实现轮转广告带底部指示的自定义ViewPager控件
- 自定义RadioButton和ViewPager实现TabHost带滑动的页卡效果
- 实例讲解Android中ViewPager组件的一些进阶使用技巧
- Android 开发之BottomBar+ViewPager+Fragment实现炫酷的底部导航效果