2.14GridView(网格视图)_Adapter适配器_Android

TOC

GridView(网格视图)_Adapter适配器_Android

  • android:columnWidth:设置列的宽度
  • android:gravity:组件对其方式
  • android:horizontalSpacing:水平方向每个单元格的间距
  • android:verticalSpacing:垂直方向每个单元格的间距
  • android:numColumns:设置列数
  • android:stretchMode:设置拉伸模式,可选值如下: none:不拉伸;spacingWidth:拉伸元素
  • 间的间隔空隙 columnWidth:仅仅拉伸表格元素自身 spacingWidthUniform:既拉元素间距又拉伸他们之间的间隔空隙

总页面

<?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:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:numColumns="3" >
    <!--numColumns  一行有几列
        verticalSpacing上下网格间距
        horizontalSpacing 水平间距
    -->

</GridView>

Activity

package com.ttit.helloworld;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ttit.helloworld.adapter.GridViewAdpater;
import com.ttit.helloworld.entity.Icon;

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

public class GridViewActivity extends AppCompatActivity {
    private Context mContext;
    private GridView grid_photo;
    private GridViewAdpater mAdapter = null;
    //放置网格的图标
    private List<Icon> mData = null;

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

        mContext = this;
        //视图层V
        grid_photo = (GridView) findViewById(R.id.gridView);
        //数据源M
        mData = new ArrayList();
        //Icon参数:图片,图片名
        mData.add(new Icon(R.mipmap.iv_icon_1, "图标1"));
        mData.add(new Icon(R.mipmap.iv_icon_2, "图标2"));
        mData.add(new Icon(R.mipmap.iv_icon_3, "图标3"));
        mData.add(new Icon(R.mipmap.iv_icon_4, "图标4"));
        mData.add(new Icon(R.mipmap.iv_icon_5, "图标5"));
        mData.add(new Icon(R.mipmap.iv_icon_6, "图标6"));
        mData.add(new Icon(R.mipmap.iv_icon_7, "图标7"));
        //控制层C
        mAdapter = new GridViewAdpater(mData, mContext);

        grid_photo.setAdapter(mAdapter);
        //点击事件
        grid_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mContext, "你点击了~" + position + "~项", Toast.LENGTH_SHORT).show();
            }
        });

    }
}

适配器,控制层GridViewAdpater

package com.ttit.helloworld.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ttit.helloworld.R;
import com.ttit.helloworld.entity.Icon;

import java.util.List;
//自定义的网格适配器
public class GridViewAdpater extends BaseAdapter {
    //数据
    private List<Icon> mData;
    private Context mContext;

    public GridViewAdpater(List<Icon> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }
    //组件数量
    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }
    //获取一个网格的组件
    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //创建每一个网格组件的布局
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            //创建组件页面(定义的一个网格的组件,租,)
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid_icon
                    , parent
                    , false);
            holder.img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
            holder.name = (TextView) convertView.findViewById(R.id.txt_icon);
            //将这一个网格的信息对象设置进去
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.img_icon.setImageResource(mData.get(position).getIconId());
        holder.name.setText(mData.get(position).getName());
        return convertView;
    }
    //记录这个网格的信息
    static class ViewHolder {
        private ImageView img_icon;
        private TextView name;
    }

}

单个网格页面item_grid_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerInParent="true"
        android:src="@mipmap/iv_icon_1" />

    <TextView
        android:id="@+id/txt_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/img_icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="呵呵"
        android:textSize="18sp" />


</RelativeLayout>

posted @ 2021-03-02 19:15  紫月java  阅读(295)  评论(0编辑  收藏  举报