• Android GridView的使用


    Android的GridView控件用于把一系列的空间组织成一个二维的网格显示出来应用的比较多的就是组合图片显示下面我就详细讲一个例子

    首先写一个类继承BaseAdapter

    1. Java代码     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    package com.yarin.android.Examples_04_19;
     
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
     
    public class ImageAdapter extends BaseAdapter
    {
        // 定义Context
        private Context     mContext;
        // 定义整型数组 即图片源
        private Integer[]   mImageIds   =
        {
                R.drawable.img1,
                R.drawable.img2,
                R.drawable.img3,
                R.drawable.img4,
                R.drawable.img5,
                R.drawable.img6,
                R.drawable.img7,
                R.drawable.img8,
                R.drawable.img1,
        };
     
        public ImageAdapter(Context c)
        {
            mContext = c;
        }
     
        // 获取图片的个数
        public int getCount()
        {
            return mImageIds.length;
        }
     
        // 获取图片在库中的位置
        public Object getItem(int position)
        {
            return position;
        }
     
     
        // 获取图片ID
        public long getItemId(int position)
        {
            return position;
        }
     
     
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView;
            if (convertView == null)
            {
                // 给ImageView设置资源
                imageView = new ImageView(mContext);
                // 设置布局 图片120×120显示
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                // 设置显示比例类型
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            }
            else
            {
                imageView = (ImageView) convertView;
            }
     
            imageView.setImageResource(mImageIds[position]);
            return imageView;
        }
     
    }

    2. Java代码     悦德财富:https://www.yuedecaifu.com

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    package com.yarin.android.Examples_04_19;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.GridView;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemClickListener;
     
    public class Activity01 extends Activity
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
     
            //取得GridView对象
            GridView gridview = (GridView) findViewById(R.id.gridview);
            //添加元素给gridview
            gridview.setAdapter(new ImageAdapter(this));
     
            // 设置Gallery的背景
            gridview.setBackgroundResource(R.drawable.bg0);
     
            //事件监听
            gridview.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id)
                {
                    Toast.makeText(Activity01.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    3. XML代码     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml version="1.0" encoding="utf-8"?>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:columnWidth="90dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
    />
  • 相关阅读:
    CSS3实现0.5px的边框
    解决nvm导致终端启动慢的问题
    解决zsh: command not found: gitk,将git路径/usr/bin/git修改为/usr/local/bin/git
    MAC之zsh终端使用nvm安装指定版本node
    解决npm包node-sass下载失败问题
    forEach中使用async await的问题
    使用module-alias别名简化CommonJS的require路径
    关系型数据库的ACID
    Node.js ORM 框架对比
    mysql字符集 utf8 和utf8mb4 的区别
  • 原文地址:https://www.cnblogs.com/oceansea/p/5969789.html
Copyright © 2020-2023  润新知