关闭

What:How:实现GridView功能的核心框架

标签: androidlayoutattributesintegerencodingnull
750人阅读 评论(0) 收藏 举报
分类:

(本文是以官网提供的一个实例进行讲解,关于该实例可通过我上传的资源或者访问android开发官网来获取,具体链接见本文最后)

实现该功能的重点或主要工作:

    定制自己的适配器(adapter),下面有对该问题的展开

实现该功能的核心组成:

    1.在res/layout,中相应的xml文件中声明GridView视图,该文件的可以定制GridView的各种参数,eg

   

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>
</LinearLayout>

    2.在res/drawable中加入自己的图片资源,eg

(其中sample_0~sample_7是我们假如的,当然我们可以在数量上和名称上做出相应的改动)

    3.为自己的GridView定制适配器,这方面可以查看我的另一片文章eg

//为GridView定义自己的适配器
    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setAdjustViewBounds(false);
                imageView.setScaleType(ImageView.ScaleType.CENTER);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7,
                R.drawable.sample_0, R.drawable.sample_1,
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7,
                R.drawable.sample_0, R.drawable.sample_1,
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7,
                R.drawable.sample_0, R.drawable.sample_1,
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7
        };
    }

    4.在activity中实现相应的逻辑功能(GridView实例化,设置监听器等),eg

import android.widget.GridView;

import android.widget.AdapterView.OnItemClickListener;

...

GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setNumColumns(3);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(Ch04_GridViewActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });

本文对应的资源:

    1.GridView官方教程

    2.本文对应实例源代码


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:197716次
    • 积分:2315
    • 等级:
    • 排名:第16109名
    • 原创:66篇
    • 转载:27篇
    • 译文:0篇
    • 评论:14条
    最新评论