欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android中GridView插件的使用方法

时间:2021-02-09 14:31:11|栏目:Android代码|点击:

布局文件activity_main.xml

<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"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="16dp"
    android:numColumns="4"
    android:stretchMode="columnWidth" >
  </GridView>
</RelativeLayout>

gridview_item.xml

这个是一个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" >
  <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_launcher" />
  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="TextView" />
</LinearLayout>

MainActivity.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends Activity {
  private GridView gridView;
  private GridViewAdapter adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 设置适配器的图片资源
    int[] imageId = new int[] { R.drawable.chat_tool_camera,
        R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
        R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
        R.drawable.chat_tool_camera, R.drawable.chat_tool_location,
        R.drawable.chat_tool_paint, R.drawable.chat_tool_video,
        R.drawable.chat_tool_voice, R.drawable.chat_tool_camera,
        R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
        R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
        R.drawable.chat_tool_camera, R.drawable.chat_tool_location,
        R.drawable.chat_tool_paint, R.drawable.chat_tool_video,
        R.drawable.chat_tool_voice };
    // 设置标题
    String[] title = new String[] { "相机", "定位", "画笔", "视频", "声音", "相机",
        "定位", "画笔", "视频", "声音", "相机", "定位", "画笔", "视频", "声音", "相机",
        "定位", "画笔", "视频", "声音" };
    List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
    // 将上述资源转化为list集合
    for (int i = 0; i < title.length; i++) {
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("image", imageId[i]);
      map.put("title", title[i]);
      listitem.add(map);
    }
    adapter = new GridViewAdapter(MainActivity.this, listitem);
    gridView = (GridView) this.findViewById(R.id.gridView);
    gridView.setAdapter(adapter);
  }
}

GridViewAdapter.java

这个是适配器

import java.util.List;
import java.util.Map;
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;
public class GridViewAdapter extends BaseAdapter {
  private Context context;
  private List<Map<String, Object>> listitem;
  public GridViewAdapter(Context context,List<Map<String, Object>> listitem) {
    this.context = context;
    this.listitem = listitem;
  }
  @Override
  public int getCount() {
    return listitem.size();
  }
  @Override
  public Object getItem(int position) {
    return listitem.get(position);
  }
  @Override
  public long getItemId(int position) {
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);
    }
    ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
    TextView textView = (TextView) convertView.findViewById(R.id.textView);
    Map<String, Object> map = listitem.get(position);
    imageView.setImageResource((Integer) map.get("image"));
    textView.setText(map.get("title") + "");
    return convertView;
  }
}

效果图如下:

总结

上一篇:Android PickerView实现三级联动效果

栏    目:Android代码

下一篇:Android实现GPS定位代码实例

本文标题:Android中GridView插件的使用方法

本文地址:http://www.codeinn.net/misctech/59618.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有