Android 网格视图(GirdView)简易适配器的使用

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tdl081071tdy/article/details/83150205

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.first.myapplication.MainActivity">

    <GridView
        android:id="@+id/grid"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </GridView>

</android.support.constraint.ConstraintLayout>

a2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:gravity="center_vertical|center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/img"
            android:src="@mipmap/a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/txt"
            android:textSize="20dp"
            android:text="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.first.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    GridView gridView;
    SimpleAdapter simpleAdapter;
    /**图片地址**/
    private int[] icon = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d,R.mipmap.e,R.mipmap.f,R.mipmap.g};
    private String[] name = {"1","2","1","1","1","1"};

    private List<Map<String,Object>> mapList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapList = new ArrayList<Map<String,Object>>();
        gridView = this.findViewById(R.id.grid);

        /**第一个参数为上下文 第二个参数为数据源 第三个参数为布局文件**/
        /**第四个参数为键值 第五个参数为布局文件中控件的id**/
        simpleAdapter = new SimpleAdapter(MainActivity.this,getlist(),R.layout.a2,new String[]{"imgview","textview"},
                new int[]{R.id.img,R.id.txt});
        /**加载适配器**/
        gridView.setAdapter(simpleAdapter);
    }

    /**添加数据源**/
    private List<Map<String,Object>> getlist(){
        for(int i = 0;i<name.length;i++){
            Map<String,Object>map = new HashMap<String,Object>();
            map.put("imgview",icon[i]);
            map.put("textview",name[i]);
            mapList.add(map);
        }
        return mapList;
    }
}

 

展开阅读全文

没有更多推荐了,返回首页