一手遮天 Android - view(集合类): GridView 基础 2

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(集合类): GridView 基础 2

示例如下:

/view/collection/GridViewDemo2.java

/**
 * GridView - 网格控件
 *
 * 本例演示
 * 1、GridView 通过 BaseAdapter 显示数据
 * 2、GridView 的常用属性的说明(参见 xml 中的说明)
 * 3、其他更多知识点请参见 ListView 的说明(GridView 和 ListView 都继承自 AbsListView)
 */

package com.webabcd.androiddemo.view.collection;

import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import com.webabcd.androiddemo.R;

import java.util.ArrayList;
import java.util.List;

public class GridViewDemo2 extends AppCompatActivity {

    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    private GridView mGridView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_collection_gridviewdemo2);

        mGridView1 = findViewById(R.id.gridView1);
        mButton1 = findViewById(R.id.button1);
        mButton2 = findViewById(R.id.button2);
        mButton3 = findViewById(R.id.button3);
        mButton4 = findViewById(R.id.button4);

        sample();
    }

    private void sample() {
        // 构造数据
        List<MyData> myDataList = new ArrayList<MyData>();
        myDataList.add(new MyData(R.drawable.img_sample_son));
        myDataList.add(new MyData(R.drawable.img_sample_son));
        myDataList.add(new MyData(R.drawable.img_sample_son));
        myDataList.add(new MyData(R.drawable.img_sample_son));
        myDataList.add(new MyData(R.drawable.img_sample_son));
        myDataList.add(new MyData(R.drawable.img_sample_son));
        myDataList.add(new MyData(R.drawable.img_sample_son));
        myDataList.add(new MyData(R.drawable.img_sample_son));
        myDataList.add(new MyData(R.drawable.img_sample_son));

        // 实例化自定义的 BaseAdapter
        MyAdapter myAdapter = new MyAdapter(myDataList, this);
        mGridView1.setAdapter(myAdapter);

        // 设置 stretchMode 属性
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 相当于 xml 中的 stretchMode="none"
                mGridView1.setStretchMode(GridView.NO_STRETCH);
            }
        });
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 相当于 xml 中的 stretchMode="spacingWidth"
                mGridView1.setStretchMode(GridView.STRETCH_SPACING);
            }
        });
        mButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 相当于 xml 中的 stretchMode="columnWidth"
                mGridView1.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
            }
        });
        mButton4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 相当于 xml 中的 stretchMode="spacingWidthUniform"
                mGridView1.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
            }
        });
    }

    // 自定义实体类
    class MyData {
        private int _logoId;

        public MyData() {
        }

        public MyData(int logoId) {
            this._logoId = logoId;
        }

        public int getLogoId() {
            return _logoId;
        }

        public void setLogoId(int logoId) {
            this._logoId = logoId;
        }
    }

    // 自定义 BaseAdapter
    class MyAdapter extends BaseAdapter {

        private List<MyData> _myDataList;
        private Context _context;

        public MyAdapter(List<MyData> myDataList, Context context) {
            this._myDataList = myDataList;
            this._context = context;
        }

        // 需要呈现的 item 的总数
        @Override
        public int getCount() {
            return _myDataList.size();
        }

        // 返回指定索引位置的 item 的对象
        @Override
        public Object getItem(int position) {
            return _myDataList.get(position);
        }

        // 返回指定索引位置的 item 的 id
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(_context).inflate(R.layout.item_view_collection_gridviewdemo2, parent, false);

                holder = new ViewHolder();
                holder.imgLogo = (ImageView) convertView.findViewById(R.id.imgLogo);
                convertView.setTag(holder); // 将 holder 保存到 convertView 中
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.imgLogo.setBackgroundResource(_myDataList.get(position).getLogoId());

            return convertView;
        }

        class ViewHolder {
            ImageView imgLogo;
        }
    }
}

/layout/activity_view_collection_gridviewdemo2.xml

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

    <!--
        horizontalSpacing - 水平方向单元格之间的间距
        verticalSpacing - 垂直方向单元格之间的间距
        gravity - 单元格内元素相对于单元格的对齐方式

        numColumns - 当此值指定为一个大于 0 的整数时
            指定网格控件的列数(网格列宽会平均分,行高和行数会自适应)

        numColumns - 当此值指定为 auto_fit 时(auto_fit 就是 -1)
            1、列数会自适应,列宽会参考 columnWidth 值,行高和行数会自适应
            2、stretchMode - 列的拉伸方式
                   none - 不拉伸,列的宽度就是 columnWidth 值
                   spacingWidth - 列会填满 GridView 的宽度,列之间的间距会被拉伸,列的宽度就是 columnWidth 值
                   columnWidth - 列会填满 GridView 的宽度,列本身会被拉伸,即列的宽度会大于等于 columnWidth 值
                   spacingWidthUniform - 列会填满 GridView 的宽度,列本身和列之间的间距都 会被拉伸
    -->
    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:columnWidth="60dp"
        android:stretchMode="none"/>

    <!--
        以下几个按钮用于动态修改 GridView 的 stretchMode 属性,以便观察效果
    -->
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="stretchMode:none"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="stretchMode:spacingWidth"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="stretchMode:columnWidth"/>
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="stretchMode:spacingWidthUniform"/>

</LinearLayout>

/layout/item_view_collection_gridviewdemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imgLogo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY">

</ImageView>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-06-02 08:14  webabcd  阅读(51)  评论(0编辑  收藏  举报