androdi 之GridVeiw 使用
来源:互联网 发布:知乎 酒店惊魂 编辑:程序博客网 时间:2024/05/22 02:11
package com.gird;
import java.util.ArrayList;
import java.util.HashMap;
import com.gird.GridAdapter.ViewHolder;
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 android.widget.AdapterView.OnItemClickListener;
public class main extends Activity {
GridView gridview;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridview =(GridView)this.findViewById(R.id.gridView1);
ArrayList<HashMap<String, Object>> list=iniRes();
/*this上下文
* 要显示的数据
* 定义的布局
* ArrayList<HashMap<String 中的keys 一一对应布局中的id
*
*/
// SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.grid_item,
// new String[]{"image","text"}, new int[]{R.id.ItemImage,R.id.ItemText});
GridAdapter adapter =new GridAdapter(this);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new MyOnItemClickListener());
}
private ArrayList<HashMap<String, Object>> iniRes()
{
ArrayList<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
for(int i=0;i<13;i++)
{
HashMap< String, Object> map=new HashMap<String, Object>();
if(i==0)
{
map.put("image", R.drawable.g11);
map.put("text", this.getResources().getString(R.string.gridview1));
}
else if(i==1)
{
map.put("image", R.drawable.g12);
map.put("text", this.getResources().getString(R.string.gridview2));
}
else if(i==2)
{
map.put("image", R.drawable.g13);
map.put("text", this.getResources().getString(R.string.gridview3));
}
else if(i==3)
{
map.put("image", R.drawable.g14);
map.put("text", this.getResources().getString(R.string.gridview4));
}
else if(i==4)
{
map.put("image", R.drawable.g15);
map.put("text", this.getResources().getString(R.string.gridview5));
}
else if(i==5)
{
map.put("image", R.drawable.g16);
map.put("text", this.getResources().getString(R.string.gridview6));
}
else if(i==6)
{
map.put("image", R.drawable.g17);
map.put("text", this.getResources().getString(R.string.gridview7));
}
else if(i==7)
{
map.put("image", R.drawable.g18);
map.put("text", this.getResources().getString(R.string.gridview8));
}
else if(i==8)
{
map.put("image", R.drawable.g19);
map.put("text", this.getResources().getString(R.string.gridview9));
}
else if(i==9)
{
map.put("image", R.drawable.btn_check_off_selected);
map.put("text", "九宫格10");
}
else if(i==10)
{
map.put("image", R.drawable.btn_check_on_pressed);
map.put("text", "九宫格11");
}
else if(i==11)
{
map.put("image", R.drawable.btn_circle_pressed);
map.put("text", "九宫格12");
}
else if(i==12)
{
map.put("image", R.drawable.btn_close_pressed);
map.put("text", "九宫格13");
}
list.add(map);
}
return list;
}
class MyOnItemClickListener implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i("JNIMsg", "position:"+position+" id "+id);
//SimpleAdapter 系统适配器
// HashMap<String, Object> item=(HashMap<String, Object>) parent.getItemAtPosition(position);
// if(item.get("text").equals(main.this.getResources().getString(R.string.gridview1)))
// {
// Toast.makeText(main.this, getResources().getString(R.string.gridview1), 0).show();
// }
//GridAdapter 自己定义适配器
ViewHolder vHollder = (ViewHolder)view.getTag(); //得到自定义Layout
if(vHollder.txtview.getText().toString().equals(main.this.getResources().getString(R.string.gridview1)))
{
Toast.makeText(main.this, getResources().getString(R.string.gridview1), 0).show();
}
}
}
}
自己定的适配器,
package com.gird;
import android.content.Context;
import android.util.Log;
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 GridAdapter extends BaseAdapter {
LayoutInflater Layout;
Context context;
int imgIDS[];
String strIDS[];
public GridAdapter(Context context) {
Layout= LayoutInflater.from(context); //得到布局
this.context=context;
strIDS=new String[]{context.getResources().getString(R.string.gridview1),context.getResources().getString(R.string.gridview2),context.getResources().getString(R.string.gridview3)};
imgIDS=new int[]{R.drawable.g11,R.drawable.g12,R.drawable.g13,R.drawable.g14,R.drawable.g15,R.drawable.g16,R.drawable.g17
,R.drawable.g18,R.drawable.g19};
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return strIDS.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView==null)
{//绑定组件
viewHolder = new ViewHolder();
View V=Layout.inflate(R.layout.grid_item, null); //引用自定义布局
viewHolder.imageView=(ImageView)V.findViewById(R.id.ItemImage);
viewHolder.txtview=(TextView)V.findViewById(R.id.ItemText);
V.setTag(viewHolder);
convertView = V;
}
else
{
viewHolder = (ViewHolder)convertView.getTag();//得到绑定组件
}
if(position<imgIDS.length)
{
viewHolder.imageView.setImageResource(imgIDS[position]);
viewHolder.txtview.setText(strIDS[position]);
}
return convertView;
}
class ViewHolder
{
ImageView imageView;
TextView txtview;
}
}
GridView布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dp"
android:layout_height="wrap_content"
>
<ImageView
android:layout_height="wrap_content"
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_below="@+id/ItemImage"
android:layout_height="wrap_content"
android:text="TextView01"
android:layout_centerHorizontal="true"
android:id="@+id/ItemText">
</TextView>
</RelativeLayout>
主要布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/bg">
<ImageView android:src="@drawable/top" android:layout_width="wrap_content"
android:id="@+id/imageView1" android:layout_height="wrap_content"></ImageView>
<!--
android:numColumns显示的行数 android:verticalSpacing 垂直距离
android:horizontalSpacing 水平距离 android:gravity 对齐方式
android:listSelector 选择中的背景
android:listSelector="@xml/grid_selector_background"
-->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:id="@+id/gridView1"
android:layout_height="wrap_content" android:numColumns="3"
android:verticalSpacing="30dip" android:horizontalSpacing="10dip"
android:gravity="center"
android:listSelector="@xml/grid_selector_background"
>
</GridView>
</LinearLayout>
- androdi 之GridVeiw 使用
- androdi组件之Intent
- Androdi之RecyclerView
- androdi之自定义控件(一)
- Androdi开发之WebView学习总结
- androdi studio之aidl文件生成
- androdi draw9patch 图片使用中的小问题
- Androdi使用GSON解析嵌套JSON
- 71 Androdi WebView 的简单使用(一)
- gridveiw操作大全
- gridveiw操作大全
- gridveiw操作大全
- GridVeiw 直接导出 Excel
- gridveiw导出Excel
- gridveiw消除黄色背景
- Androdi SoftReference
- androdi权限
- Androdi AsyncTask
- DNS域名解析过程
- Android下,屏蔽飞信通讯录助手
- Nginx学习资源
- struts2学习笔记(十)——主题与模板
- Apache二级域名实现
- androdi 之GridVeiw 使用
- JS判断页面上的单选按钮是否选中
- 深入研究java.lang.ProcessBuilder类
- 领导能力
- CSS的Border属性 属性 边框 可以 定义 宽度 颜色 CSS solid 类型 文本
- [iOS]CATransform3D
- 正则表达式语法
- Android测试(基础篇)
- 25个Apache性能优化技巧推荐