android教程viewpager自動迴圈和手動迴圈_Android

來源:互聯網
上載者:User

複製代碼 代碼如下:

package com.imoyo.expert;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

public class ExActivity extends Activity {
 ViewPager viewPager;
 ArrayList<View> list;
 ViewGroup main, group;
 boolean cunhuan = false;
 ImageView imageView;
 ImageView[] imageViews;
 private static int c_id = 0;
 static int page_id = 1;
 private Runnable switchTask = new Runnable() {
  public void run() {
   if (cunhuan) {
    viewPager.setCurrentItem(page_id); 
    page_id++;
   }
   cunhuan=true;
   Log.e("is_run", cunhuan+"");
   mHandler.postDelayed(switchTask, 3000);

  }
 };
 Handler mHandler = new Handler();
 @Override
 protected void onResume() {
  super.onResume();
  switchTask.run();
 }
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  getView();

  imageViews = new ImageView[list.size()];
  ViewGroup main = (ViewGroup) inflater.inflate(R.layout.activity_main,
    null);

  ViewGroup group = (ViewGroup) main.findViewById(R.id.viewGroup);

  viewPager = (ViewPager) main.findViewById(R.id.viewPager);

  for (int i = 0; i < 5; i++) {
   imageView = new ImageView(this);
   imageView.setLayoutParams(new LayoutParams(12, 12));
   imageViews[i] = imageView;
   if (i == 0) {
    imageViews[i].setBackgroundResource(R.drawable.img_blue_point);
   } else {
    imageViews[i].setBackgroundResource(R.drawable.img_white_point);
   }
   group.addView(imageView);
  }

  setContentView(main);
  ViewPagerAdapter adapter = new ViewPagerAdapter(this, list);
  viewPager.setAdapter(adapter);
  viewPager.setOnPageChangeListener(listener);
  viewPager.setCurrentItem(2);

 }

 public void getView() {
  inflater = getLayoutInflater();
  list = new ArrayList<View>();
  list.add(inflater.inflate(R.layout.item4, null));
  list.add(inflater.inflate(R.layout.item5, null));

  list.add(inflater.inflate(R.layout.item1, null));
  list.add(inflater.inflate(R.layout.item2, null));
  list.add(inflater.inflate(R.layout.item3, null));
  list.add(inflater.inflate(R.layout.item4, null));
  list.add(inflater.inflate(R.layout.item5, null));

  list.add(inflater.inflate(R.layout.item1, null));
  list.add(inflater.inflate(R.layout.item2, null));
 }

 OnPageChangeListener listener = new OnPageChangeListener() {

  @Override
  public void onPageSelected(int arg0) {

   if (arg0 == 0) {
    viewPager.setCurrentItem(5, false);
    page_id=5;
   } else if (arg0 == 8) {
    viewPager.setCurrentItem(3, false);
    page_id=3;
   }else{
    page_id=arg0;
   }
   Log.e("arg", arg0 + "");

   for (int i = 2; i < 7; i++) {
    imageViews[i - 2]
      .setBackgroundResource(R.drawable.img_white_point);
   }
   switch (arg0) {
   case 7:
   case 2:
    imageViews[0]
      .setBackgroundResource(R.drawable.img_blue_point);
    break;
   case 3:
   case 8:
    imageViews[1]
      .setBackgroundResource(R.drawable.img_blue_point);
    break;
   case 4:
    imageViews[2]
      .setBackgroundResource(R.drawable.img_blue_point);
    break;
   case 0:
   case 5:
    imageViews[3]
      .setBackgroundResource(R.drawable.img_blue_point);
    break;
   case 1:
   case 6:
    imageViews[4]
      .setBackgroundResource(R.drawable.img_blue_point);
    break;
   }

  }

  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {
  }

  @Override
  public void onPageScrollStateChanged(int arg0) {

  }
 };
 private LayoutInflater inflater;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.