我的Android之旅(六)---GridView

GridView是显示二维的数据的网格,并类似ListView可滚动
GridView典型应用九宫格


适配器控件,加载数据调用setAdaper()

GridView常用属性:

android:numColumns=”auto_fit” //GridView的列数设置为自动
android:columnWidth=”90dp " //每列的宽度,也就是Item的宽度
android:stretchMode=”columnWidth“//设置缩放模式,与列宽大小同步
android:verticalSpacing=”10dp” //两行之间的边距
android:horizontalSpacing=”10dp” //两列之间的边距
android:cacheColorHint="#00000000" //去除拖动时默认的黑色背景
android:listSelector="#00000000" //去除选中时的黄色底色
android:scrollbars="none" //隐藏GridView的滚动条

下面简单的举两个例子:


在编写主函数之前,要先建立一个测试文件,格式为GridView 中所需要得到的页面效果

该布局中中只包含一个TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingTop="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/rdbt_text"
    android:text="测试"
    android:background="@drawable/anim"
    android:id="@+id/tv"
    android:textSize="20sp"
    android:button="@null"
    android:gravity="center"/>
</LinearLayout>




main函数中代码:

package com.jerehedu.jreduch04;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 Grid_View3Activity extends AppCompatActivity {
    private GridView gv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid__view3);
        gv= (GridView) findViewById(R.id.gv);
        final List list=new ArrayList();
        Map map=new HashMap();
        map.put("name", "推荐");
        list.add(map);
        map=new HashMap();
        map.put("name", "热点");
        list.add(map);
        map=new HashMap();
        map.put("name", "烟台");
        list.add(map);
        map=new HashMap();
        map.put("name", "视频");
        list.add(map);
        map=new HashMap();
        map.put("name", "订阅");
        list.add(map);
        map=new HashMap();
        map.put("name", "社会");
        list.add(map);
        map=new HashMap();
        map.put("name", "娱乐");
        list.add(map);
        map=new HashMap();
        map.put("name", "科技");
        list.add(map);
        map=new HashMap();
        map.put("name", "汽车");
        list.add(map);
        map=new HashMap();
        map.put("name", "体育");
        list.add(map);
        map=new HashMap();
        map.put("name", "财经");
        list.add(map);
        map=new HashMap();
        map.put("name", "军事");
        list.add(map);
        map=new HashMap();
        map.put("name", "国际");
        list.add(map);
        map=new HashMap();
        map.put("name", "段子");
        list.add(map);
        map=new HashMap();
        map.put("name", "趣图");
        list.add(map);
        map=new HashMap();
        map.put("name", "美女");
        list.add(map);
        map=new HashMap();
        map.put("name", "健康");
        list.add(map);
        SimpleAdapter sa=new SimpleAdapter(this,list,R.layout.my_layout3,
                new String[]{"name"},
                new int[]{R.id.tv});
        gv.setAdapter(sa);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map map=(Map)list.get(position);
                Toast.makeText(getBaseContext(), "" + map.get("name"), Toast.LENGTH_SHORT).show();
            }
        });

    }
}

注意适配器SimpleAdap中各个参数的含义:

第一个参数是Context,就是你当前的activity:this
第二个参数是需要展示的列表,每个列表里存的是Map:list
第三个参数是ListView中每一项的布局文件:R.id.my_layout
第四个参数是列表里每一个Map的键:注意这里是String类型的数据
第五个是R.layout.line这个布局文件中每个控件的id

GridView 中的数据传输时需要用Map来传递,然后再传入List中,通过适配器来实现网页要求


布局文件中的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.jerehedu.jreduch04.Grid_View3Activity"
    android:weightSum="1">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我的频道"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"
            android:textColor="#ea0707"
            android:textSize="20sp"
            android:background="#f9f3f3"
            android:layout_marginLeft="200dp"/>
    </LinearLayout>
    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:numColumns="4"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"
        android:cacheColorHint="#00000000"
        android:listSelector="#00000000"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:scrollbars="none"
        android:id="@+id/gv">
    </GridView>
</LinearLayout>

布局文件中的ViewGroup中的属性比较多,,刻意记一下还是好的

运行结果如下:


该例中的ListView中每一个的布局文件包括两个控件,可以实现手机菜单的界面:

该布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingTop="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/iv"
    android:scaleType="centerCrop"
    android:src="@mipmap/ic_launcher"/>
    <!-- scaleType="centerCrop":等比例缩放 -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:gravity="center"
        android:text="测试"/>
</LinearLayout>

主布局文件中的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jerehedu.jreduch04.GridViewActivity">
<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="100dp"
    android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp"
    android:cacheColorHint="#00000000"
    android:listSelector="#00000000"
    android:scrollbars="none"
    android:id="@+id/gv">

</GridView>
</RelativeLayout>
该主布局文件内只需要写一个GridView即可。重要的是其的属性

mian 函数中的代码如下:

package com.jerehedu.jreduch04;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 GridViewActivity extends AppCompatActivity {
private GridView gv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);
        gv= (GridView) findViewById(R.id.gv);
        final List list=new ArrayList();
        Map map=new HashMap();
        map.put("img",R.mipmap.e1);
        map.put("name", "兔子");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.t2);
        map.put("name", "兔子1");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.t3);
        map.put("name", "兔子2");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.t4);
        map.put("name", "兔子3");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.t5);
        map.put("name", "兔子4");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.e1);
        map.put("name", "兔子5");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.e2);
        map.put("name", "兔子6");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.e2);
        map.put("name", "兔子7");
        list.add(map);

        /*
        1.SimpleAdapter 使用的数据源必须继承  Map 接口
        2.from参数的意思是  指向数据源map 中的键
        3.to 参数的意思是 给布局中控件Id赋值
        */
        SimpleAdapter sa=new SimpleAdapter(this,list,
                R.layout.grid_layout,
                new String[]{"img","name"},
                new int[]{R.id.iv,R.id.tv});
        gv.setAdapter(sa);
      gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              Map map=(Map)list.get(position);
              Toast.makeText(getBaseContext(),""+map.get("name"),Toast.LENGTH_SHORT).show();
              Intent intent=new Intent(getBaseContext(),GridView2Activity.class);
              intent.putExtra("img",(int)map.get("img"));
              //要记得传输所需要的数据
              startActivity(intent);
          }

      });

    }

}


该程序还可实现:点击任意图片,将会跳转到另外一个Activity,并且可以实现图片的放大以及旋转


运行结果如下:





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值