pingpang

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  21 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks

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;

  }

 }
}

posted on 2012-07-17 21:25 往事随风 阅读(1554) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: