• Android之画廊点击内容显示


    package com.example.Gallery;
     
    import com.example.Gallery.R;
     
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.Toast;
     
    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
        private Gallery gallery;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            gallery = (Gallery)findViewById(R.id.gallery);
            //设置图片适配器
            gallery.setAdapter(new ImageAdapter(this));      
            //设置监听器
            gallery.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                Toast.makeText(MainActivity.this, "点击了第"+(arg2+1)+"张图片", Toast.LENGTH_LONG).show();
                }
            });
        }
    }
     
    class ImageAdapter extends BaseAdapter{
        private Context context;
        //图片源数组
        private Integer[] imageInteger={
                R.drawable.item1,R.drawable.item2,R.drawable.item3,R.drawable.item4,
                R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8,
                R.drawable.item9,R.drawable.item10,R.drawable.item11,R.drawable.item12,
                R.drawable.item13,R.drawable.item14,R.drawable.item15};
        public ImageAdapter(Context c){
            context = c;
        }
        @Override
         
        public int getCount() {
            return imageInteger.length;
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imageInteger[position]);
            imageView.setBackgroundColor(0xFF000000);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
            return imageView;
        }
        }
    

      

    效果执行图:

    不努力,还要青春干什么?
  • 相关阅读:
    做了好几年的程序员,才发现自己天天都在用设计模式!
    先搞清楚这些问题,简历上再写你熟悉Java!
    Java中实现多线程继承Thread类与实现Runnable接口的区别
    JAVA中实现多线程的四种方式
    JDK和Cglib动态代理
    Java中选择排序,冒泡排序,插入排序,快速排序
    java死锁详解
    github常用命令
    字符串之StringBuffer 与 StringBuilder的对比
    基础数据类型之AbstractStringBuilder
  • 原文地址:https://www.cnblogs.com/caidupingblogs/p/4952324.html
Copyright © 2020-2023  润新知