GridView的一种实现方式

GridView常用于以网格形式展示数据,九宫格布局就是一种典型的应用

 

 

activity_main.xml文件

​
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
   <GridView
       android:layout_marginTop="10dp"
       android:numColumns="3"
       android:listSelector="@android:color/transparent"
       android:id="@+id/gridView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:columnWidth="60dp"
       android:stretchMode="columnWidth"
       android:fastScrollEnabled="false"
       >
   </GridView>
</LinearLayout>

item文件

<?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"
android:gravity="center"
    >
<!--建立两个空的控件用来载入具体内容-->
<ImageView
    android:id="@+id/image"
    android:layout_width="60dp"
    android:layout_height="60dp" />
<TextView
    android:layout_marginTop="5dp"
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
</LinearLayout>

 

MainActivity.java文件

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

  public class MainActivity extends Activity implements AdapterView.OnItemClickListener {

      //定义以及初始化数据
      private int[] icon = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                            R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                            R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
                            R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
      private String[] iconName = new String[]{"通讯录", "日历", "照相机", "时钟", "游戏"
                                 , "短信", "铃声", "设置", "语音", "天气", "浏览器", "视频","dee","fff","eee","eee","qqq","fff","eee","fff","ddd"};

      List<Map<String, Object>> datalist = new ArrayList<>();
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          GridView gridView = (GridView) findViewById(R.id.gridView);  //实例化gridview组件
          //新建适配器
          SimpleAdapter adapter = new SimpleAdapter(this,getData(), R.layout.item, new String[]{"image", "text"}, new int[]{R.id.image, R.id.text});
          gridView.setAdapter(adapter);  //gridview加载适配器
          gridView.setOnItemClickListener(this);   //gridview配置事件监听器
      }

      private List<Map<String, Object>> getData() {
          for (int i = 0; i < icon.length; i++) {
              Map<String, Object> map = new HashMap<>();
              map.put("image", icon[i]);
              map.put("text", iconName[i]);
              datalist.add(map);
          }
          return datalist;
      }

     /*
       点击后打印信息
      */
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
          Toast.makeText(this,"点击了"+iconName[position],Toast.LENGTH_SHORT).show();
          Log.i("tag","点击了"+iconName[position]);
      }
  }

相关属性

android:columnWidth:设置列的宽度

android:numColumns:设置需要展示的列数。 可选值[数字|auto_fit]

android:horizontalSpacing:设置列与列的水平间距。

android:verticalSpacing:设置行与行的垂直间距。

android:listSelector:设置列表项被选中时的效果 [color或drawable资源]

android:fastScrollEnabled:在快速滑动时是否显示右侧的滑动块。

gridview的android:stretchMode属性:

     columnWidth: 如果列有空闲空间就加宽列 
     spacingWidth :如果列有空闲空间就加宽各列间距 
     none 没有任何动作 
     spacingWidthUniform 平均分配空间 

注意:如果要使用stretchMode属性,就需要设置columnWidth属性,而且不能设置android:horizontalSpacing属性,否则GridView的内容可能不会显示。

设置分割线:

1,在drawable文件夹下创建一个shape资源,本例命名为item_divider.xml。代码如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:color="#aaa" android:width="1dp"/>
</shape>

 

2,将该item_divider.xml资源设为根布局(本例为item)的background,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/item_divider"
    android:gravity="center"
    >
    <!--建立两个空的控件用来载入具体内容-->
    <ImageView
        android:id="@+id/image"
        android:layout_width="60dp"
        android:layout_height="60dp" />
    <TextView
        android:layout_marginTop="5dp"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

 

注意:这种方法有一个不足之处,那就是内部的网格线会比外部的网格线要粗(因为这是两个Item间的网格线叠加形成的)。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值