Android Gallery无限循环

package gongzibai.co.cc;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class Gallery1Activity extends
  Activity {
 public static final int CHANGE_BANNER = 1;
 private int cur_index = 0;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(
   Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  final DetialGallery gallery = (DetialGallery) findViewById(R.id.widget1);
  ImageAdapter imageAdapter = new ImageAdapter(
    Gallery1Activity.this);

  gallery.setAdapter(imageAdapter);

  final Handler handler = new Handler() {
   public void handleMessage(
     Message msg) {

    switch (msg.what) {

    case CHANGE_BANNER:

     gallery.onKeyDown(
       KeyEvent.KEYCODE_DPAD_RIGHT,
       null);

     break;

    default:

     break;

    }

   };
  };
  // new Thread(new Runnable() {
  // int flag = 1;
  //
  // public void run() {
  //
  // while (true) {
  //
  // handler.sendEmptyMessage(CHANGE_BANNER);
  //
  // try {
  //
  // Thread.sleep(1000);
  //
  // } catch (InterruptedException e) {
  //
  // e.printStackTrace();
  //
  // }
  //
  // }
  //
  // }
  //
  // }
  //
  // ).start();

  gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(
     AdapterView<?> arg0,
     View arg1,
     int arg2, long arg3) {
    // TODO Auto-generated method stub
    Toast.makeText(
      Gallery1Activity.this,
      "这是第" + arg2
        + "张图片",
      2).show();
   }

   @Override
   public void onNothingSelected(
     AdapterView<?> arg0) {
    // TODO Auto-generated method stub

   }
  });

 }

 class ImageAdapter extends
   BaseAdapter {
  Context context;
  int[] imgResId = new int[] {
    R.drawable.icon,
    R.drawable.icon2,
    R.drawable.icon3,
    R.drawable.icon,
    R.drawable.icon2,
    R.drawable.icon3,

  };

  public ImageAdapter(
    Gallery1Activity gallery1Activity) {
   // TODO Auto-generated constructor stub
   super();
   this.context = gallery1Activity;
  }

  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   // return Integer.MAX_VALUE;
   return Integer.MAX_VALUE;

  }

  @Override
  public Object getItem(
    int position) {
   // TODO Auto-generated method stub

   return position;
   //
   // return position;
   // return position;

  }

  @Override
  public long getItemId(
    int position) {
   // TODO Auto-generated method stub
   return position;
  }

  @Override
  public View getView(int arg0,
    View arg1,
    ViewGroup arg2) {
   // TODO Auto-generated method stub
   ImageView imageView = new ImageView(
     context);
   imageView
     .setImageResource(imgResId[arg0%imgResId.length]);
   imageView
     .setScaleType(ImageView.ScaleType.FIT_XY);
   imageView
     .setLayoutParams(new Gallery.LayoutParams(
       175, 200));

   return imageView;

  }

 } }

相关文章
相关标签/搜索