Android官方文档—User Interface(Layouts)(Grid View)

Grid View

GridView是一个ViewGroup,它以二维可滚动网格显示项目。使用ListAdapter将网格项自动插入到布局中。

有关如何使用适配器动态插入视图的介绍,请阅读使用适配器构建布局。

示例


在本教程中,您将创建一个图像缩略图网格。选择项目后,Toast消息将显示图像的位置。

1.启动名为HelloGridView的新项目。

2.找一些您想要使用的照片,或下载这些示例图像。将图像文件保存到项目的res / drawable /目录中。

3.打开res / layout / main.xml文件并插入以下内容:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

这个GridView将填满整个屏幕。属性是相当自我解释的。有关有效属性的更多信息,请参阅GridView参考。

4.打开HelloGridView.java并为onCreate()方法插入以下代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

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

为内容视图设置main.xml布局后,将使用findViewById(int)从布局中捕获GridView。然后,setAdapter()方法将自定义适配器(ImageAdapter)设置为要在网格中显示的所有项目的源。 ImageAdapter将在下一步中创建。

要在单击网格中的项目时执行某些操作,将向setOnItemClickListener()方法传递新的AdapterView.OnItemClickListener。此匿名实例定义onItemClick()回调方法以显示Toast,该Toast显示所选项的索引位置(从零开始)(在实际场景中,该位置可用于获取其他任务的完整大小的图像)。

5.创建一个名为ImageAdapter的新类,它扩展了BaseAdapter:

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.setScaleType(ImageView.ScaleType.CENTER_CROP);
            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
    };
}

首先,这实现了从BaseAdapter继承的一些必需方法。构造函数和getCount()是不言自明的。通常,getItem(int)应返回适配器中指定位置的实际对象,但此示例忽略它。同样,getItemId(int)应该返回项的行id,但这里不需要它。

第一个必要的方法是getView()。此方法为添加到ImageAdapter的每个图像创建一个新视图。当调用它时,传入一个View,它通常是一个循环对象(至少在调用一次之后),所以检查对象是否为null。如果为null,则会实例化ImageView并为其配置所需的图像呈现属性:

  • setLayoutParams(ViewGroup.LayoutParams)设置视图的高度和宽度 - 这可确保无论可绘制的大小如何,每个图像都会根据需要调整大小并裁剪以适合这些尺寸。
  • setScaleType(ImageView.ScaleType)声明应该向中心裁剪图像(如有必要)。
  • setPadding(int,int,int,int)定义所有边的填充。 (请注意,如果图像具有不同的宽高比,那么如果图像与给予ImageView的尺寸不匹配,则较少的填充将导致更多的图像裁剪。)

如果传递给getView()的View不为null,则使用循环的View对象初始化本地ImageView。

在getView()方法的末尾,传递给方法的位置整数用于从mThumbIds数组中选择一个图像,该数组被设置为ImageView的图像资源。

剩下的就是定义可绘制资源的mThumbIds数组。

6.运行该应用程序。

尝试通过调整其属性来尝试GridView和ImageView元素的行为。例如,不要使用setLayoutParams(ViewGroup.LayoutParams),而是尝试使用setAdjustViewBounds(boolean)。

猜你喜欢

转载自blog.csdn.net/weixin_42703445/article/details/83821052