码迷,mamicode.com
首页 > 其他好文 > 详细

Viewpager

时间:2018-05-20 20:39:32      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:extc   esc   off   最大   改变   sage   eid   void   you   

技术分享图片

private ViewPager vp_main;
 private TextView tv_main_title;
 private LinearLayout ll_main_point_group;
 private ArrayList<ImageView> imageViews;
 
 //图片资源
 private int[] imageIDs = {
   R.drawable.a,R.drawable.b,R.drawable.c,
   R.drawable.d,R.drawable.e
   };
 //图片集合标题
 private String[] imgDescriptions = {
   "游行","放价","家电","新机","火锅"
 };
 //上一次亮的位置
 private int perPosition = 0;
 private Handler handler = new Handler(){
  public void handleMessage(android.os.Message msg) {
   int item = vp_main.getCurrentItem()+1;
   vp_main.setCurrentItem(item);
   //延迟发信息
   handler.sendEmptyMessageDelayed(0, 3000);
  };
 };
 //是否已经拖拽
 private Boolean isDriaging =false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  
  /**
   * lisetView的使用
   * 在布局文件中定义ViewPager
   * 在代码中实列话Viewpager
   *
   */
  vp_main = (ViewPager) findViewById(R.id.vp_main);
  tv_main_title = (TextView) findViewById(R.id.tv_main_title);
  ll_main_point_group = (LinearLayout) findViewById(R.id.ll_main_point_group);
  
  //3.准备数据
  imageViews=new ArrayList<ImageView>();
  for (int i = 0; i < imageIDs.length; i++) {
   ImageView imageView = new ImageView(this);
   imageView.setBackgroundResource(imageIDs[i]);
   //添加到集合
   imageViews.add(imageView);
   //添加小圆点
   ImageView point = new ImageView(this);
   point.setBackgroundResource(R.drawable.point_selector);
   LinearLayout.LayoutParams params=
     new LinearLayout.LayoutParams(8,8);
   if (i==0) {
    point.setEnabled(true);//显示红色
   } else {
    point.setEnabled(false);//显示灰色
    params.leftMargin=8;
   }
   point.setLayoutParams(params);
   ll_main_point_group.addView(point);

  }
  
  //设置适配器PagerAdapter-Item布局——绑定数据
  vp_main.setAdapter(new MyPagerAdapter());
  //设置监听Viewpager页面的改变
  vp_main.setOnPageChangeListener(new MyOnPageChangeListener());
  //设置中间位置,要保证是imageViews的整数数倍
  int item = Integer.MAX_VALUE / 2 -
    Integer.MAX_VALUE / 2 % imageViews.size();
  vp_main.setCurrentItem(item);
  tv_main_title.setText(imgDescriptions[perPosition]);
  //发信息
  handler.sendEmptyMessageDelayed(0, 3000);

 }
 
 class MyOnPageChangeListener implements OnPageChangeListener{
/**
 * 当页面滚动状态变化的时候回调这个方法
 * 静止->滑动 或 滑动->静止
 */
  @Override
  public void onPageScrollStateChanged(int state) {
   // TODO Auto-generated method stub
   if (state == ViewPager.SCROLL_STATE_DRAGGING) {
    isDriaging = true;
    Log.e("TAG","SCROLL_STATE_DRAGGING-->拖拽");
   }else if (state == ViewPager.SCROLL_STATE_SETTLING) {
    Log.e("TAG", "SCROLL_STATE_DRAGGING-->滚动");
   }else if (state == ViewPager.SCROLL_STATE_IDLE && isDriaging) {
    isDriaging=false;
    Log.e("TAG", "SCROLL_STATE_DRAGGING-->静止");
    handler.removeCallbacksAndMessages( null);
    handler.sendEmptyMessageDelayed(0, 3000);

   }
  }
/**
 * 当页面滚动了的时候回调支个方法
 * position  当前页面的位置
 * positionOffset  滚动页面的百分比
 * positionOffsetPixels  在屏幕上滑动的像素
 */
  @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
   // TODO Auto-generated method stub
   
  }
/**
 * 当某个页面被选中了的时候回调
 * position 被选中页面的位置
 */
  @Override
  public void onPageSelected(int position) {
   int realPosition = position % imageViews.size();
   // 设置对应页面的文本信息
   tv_main_title.setText(imgDescriptions[realPosition]);
   //把上一个高亮的设置为灰色
   ll_main_point_group.getChildAt(perPosition)
   .setEnabled(false);
   //当前的设置为红色
   ll_main_point_group.getChildAt(realPosition)
   .setEnabled(true);
   perPosition = realPosition;
   
  }}
 
 class MyPagerAdapter extends PagerAdapter{
//得到的图片的总数
  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   //return imageViews.size();
  return Integer.MAX_VALUE;//int类型的最大值
  }
  /**
   * 相当于getView方法
   * container  viewpage的自身
   * position  当前实列化页面的当前位置
   */
  @Override
  public Object instantiateItem(ViewGroup container, int position) {
   int realPosition = position % imageViews.size();
   ImageView imageView = imageViews.get(realPosition);
   container.addView(imageView);//添加到viewpage中
   Log.e("TAG", "instantiateItem-->"+position +",imageView-->"+imageView);
   imageView.setOnTouchListener(new View.OnTouchListener() {
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
     switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN://手指按下
      Log.e("TAG", "手指按下");
      handler.removeCallbacksAndMessages(null); 
      break;
     case MotionEvent.ACTION_MOVE://手指移动
      Log.e("TAG", "手指移动");
      break;
     case MotionEvent.ACTION_CANCEL://事件取消
      Log.e("TAG", "事件取消");
//      handler.removeCallbacksAndMessages(null);
//      handler.sendEmptyMessageDelayed(0, 3000);
      break;
     case MotionEvent.ACTION_UP://手指抬起
      Log.e("TAG", "手指抬起");
      handler.removeCallbacksAndMessages( null);
      handler.sendEmptyMessageDelayed(0, 3000);

      break;

     default:
      break;
     }
     return false;
    }
   });
   imageView.setTag(position);
   imageView.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
     Log.e("TAG", "点击事件");
     int position = (Integer) v.getTag()% imageViews.size();
     String text = imgDescriptions[position];
     Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
     // TODO Auto-generated method stu
    }
   });
   return imageView;
  }
  /**
   * 比较view和object是否视同一个实列
   * view 页面
   * object  是instantiateitem返回的结构
   */
  @Override
  public boolean isViewFromObject(View view, Object object) {
   // TODO Auto-generated method stub
   return view == object;
  }
  /**
   * 释放资源
   * container  viewpager
   * position 要释放的位置
   * object 要释放的界面
   */
  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
   Log.e("TAG", "destroyItem-->"+position +
     ",object-->"+object);

   container.removeView((View)object);
  }
 }

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <size
        android:width="8dp"
        android:height="8dp"
        />
    <solid
        android:color="#44000000"
        />

</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <size
        android:width="8dp"
        android:height="8dp"
        />
    <solid
        android:color="#ff0000"
        />

</shape>

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_main"
        android:layout_width="match_parent"
        android:layout_height="180dp"
         />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignBottom="@id/vp_main"
        android:background="#44000000"
        >
        <TextView
            android:id="@+id/tv_main_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:gravity="center_horizontal"
            android:text="广告"
            android:textColor="#FFFFFF"
            />
        <LinearLayout
            android:id="@+id/ll_main_point_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal"
            >
           
        </LinearLayout>
       
    </LinearLayout>

</RelativeLayout>

 

Viewpager

标签:extc   esc   off   最大   改变   sage   eid   void   you   

原文地址:https://www.cnblogs.com/feng8026/p/9064416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!