西狂杨过 2014-02-25
Android之-----GridView使用的方法总结
GridView和ListView是Android里面比较常用的多控件布局,其使用方法上篇博客写过的ListViewf非常相似都要用到适配器来帮助创建界面。这里比较经常用适配器,那么下一篇博客就有必要来探究一下适配器的作用了。
这里我通过两种方式实现GridView布局,一种是比较简单的SimpleAdapter适配器来完成。另一种是继承BaseAdapter来自定义适配器。个人感觉第一中方法更加方便,但是第二种方法我们却能够更深入的了解到它进行数据页向面传输的机制。 SimpleAdapter方式实现:
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/mineMainview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp" android:columnWidth="90dp" android:gravity="center" android:horizontalSpacing="5dp" android:numColumns="2" android:stretchMode="columnWidth" android:verticalSpacing="20dp" > </GridView> </RelativeLayout>
上面GridView的方法注解说明:
<!-- GridView的宽度和高度 //每一列的宽度 android:columnWidth="90dp" //每一行能显示出几列,auto_fit自动设置,也可以指定列数 android:numColumns="auto_fit" //水平和竖直方向上的间距 android:verticalSpacing="10dp" android:horizontalSpacing="10dp" //图片的模式 android:stretchMode="columnWidth" //对齐方式 android:gravity="center"/>-->
picture_item.xml文件:
<?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="120dp" android:layout_height="120dp" android:layout_gravity="center" android:padding="4dp" android:scaleType="fitXY" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center_horizontal" /> </LinearLayout>
MainActivity.java文件
package com.example.gridview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.GridView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private GridView gridView; private String[] from = { "image", "title" }; private int[] to = { R.id.image, R.id.title }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.mineMainview); SimpleAdapter pictureAdapter = new SimpleAdapter(this, getList(), R.layout.picture_item, from, to); gridView.setAdapter(pictureAdapter); } public List<Map<String, Object>> getList() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = null; String[] titles = new String[] { "本地音乐", "我的最爱", "我的下载", "我的歌单", "最近播放" }; Integer[] images = { R.drawable.local, R.drawable.love, R.drawable.down, R.drawable.build, R.drawable.cloudly }; for (int i = 0; i < images.length; i++) { map = new HashMap<String, Object>(); map.put("image", images[i]); map.put("title", titles[i]); list.add(map); } return list; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
以上方法是使用SimpleAdapter适配器的方法的代码示例。
下面是自定义适配器的代码,虽然稍微麻烦了一点但是还是比较值得学习的。
activity_main.xml文件和picture_item.xml文件和上面的一样下面是java的代码。
MainActivity.java文件
package com.example.gridview; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.GridView; public class MainActivity extends Activity { private GridView gridView; private String[] titles=new String[]{ "本地音乐","我的最爱","我的下载","我的歌单","最近播放" }; private Integer[] images={ R.drawable.local,R.drawable.love, R.drawable.down,R.drawable.build, R.drawable.cloudly }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView=(GridView) findViewById(R.id.mineMainview); PictureAdapter pictureAdapter=new PictureAdapter(titles, images, this); gridView.setAdapter(pictureAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
自定义适配器PictureAdapter.java文件
package com.example.gridview; import java.util.ArrayList; import java.util.List; 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 PictureAdapter extends BaseAdapter { private Context context; private List<Picture> pictures=new ArrayList<Picture>(); public PictureAdapter(String[] titles, Integer[] images, Context context) { super(); this.context = context; for (int i = 0; i < images.length; i++) { Picture picture = new Picture(titles[i], images[i]); pictures.add(picture); } } @Override public int getCount() { if (null != pictures) { return pictures.size(); } else { return 0; } } @Override public Object getItem(int position) { return pictures.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); // 获得容器 convertView = LayoutInflater.from(this.context).inflate(R.layout.picture_item, null); // 初始化组件 viewHolder.image = (ImageView) convertView.findViewById(R.id.image); viewHolder.title = (TextView) convertView.findViewById(R.id.title); // 给converHolder附加一个对象 convertView.setTag(viewHolder); } else { // 取得converHolder附加的对象 viewHolder = (ViewHolder) convertView.getTag(); } // 给组件设置资源 Picture picture = pictures.get(position); viewHolder.image.setImageResource(picture.getImageId()); viewHolder.title.setText(picture.getTitle()); return convertView; } class ViewHolder { public TextView title; public ImageView image; } class Picture { private String title; private int imageId; public Picture(String title, Integer imageId) { this.imageId = imageId; this.title = title; } public String getTitle() { return title; } public int getImageId() { return imageId; } } }
下面是两种方式程序运行的效果图,是一样的: