android 31 GridView
GridView:网格列表,也支持适配器。

package com.sxt.day05_01; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import com.sxt.day05_01.entity.GeneralBean; public class MainActivity extends Activity {
GridView mgvGeneral;//MVC的V层,
List<GeneralBean> mGenerals;//MVC的M层,代表十个军事家的集合
GeneralAdapter mAdapter;//MVC的C层,
int[] resid={
R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
R.drawable.zhuyuanzhang
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();//初始化数据
initView();
setListener();
} private void setListener() {
setOnItemClickListener();
setOnItemLongClickListener(); } private void setOnItemLongClickListener() {
mgvGeneral.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被长按", 2000).show();
return true;
}
});
} private void setOnItemClickListener() {
mgvGeneral.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被短按", 2000).show();
}
});
} private void initView() {
mgvGeneral=(GridView) findViewById(R.id.gvGeneral);//获取gvGeneral
mAdapter=new GeneralAdapter();//创建适配器
mgvGeneral.setAdapter(mAdapter);//给gvGeneral设置适配器
} private void initData() {
//将资源中的字符串组数转换为Java数组
String[] names=getResources().getStringArray(R.array.generals);
mGenerals=new ArrayList<GeneralBean>();
for (int i = 0; i < names.length; i++) {
GeneralBean bean=new GeneralBean(resid[i], names[i]);
mGenerals.add(bean);
}
} //适配器
class GeneralAdapter extends BaseAdapter{ @Override
public int getCount() {
return mGenerals.size();
} @Override
public GeneralBean getItem(int position) {
return mGenerals.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
//得到V层的一行
public View getView(int position, View convertView, ViewGroup parent) {
//Inflate可用于将一个xml中定义的布局控件找出来,获取一行的布局item_generals.xml并要转换为View类型的对象
/*
<?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="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivThumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="fitXY"
android:src="@drawable/baiqi"/>
<TextView
android:id="@+id/tvName"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="白起"
android:textSize="20sp"
android:gravity="center_horizontal"/>
</LinearLayout>*/
//
View layout=View.inflate(MainActivity.this, R.layout.item_generals, null);
//设置该一行
ImageView ivThumb=(ImageView) layout.findViewById(R.id.ivThumb);
TextView tvName=(TextView) layout.findViewById(R.id.tvName);
GeneralBean bean=mGenerals.get(position);
ivThumb.setImageResource(bean.getResid());
tvName.setText(bean.getName());
return layout;//返回一行的View即item_generals.xml的LinearLayout
}
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <GridView
android:id="@+id/gvGeneral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="60dp"
android:horizontalSpacing="2dp"
android:verticalSpacing="5dp"
android:numColumns="auto_fit"/> </RelativeLayout>
item_generals.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="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivThumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="fitXY"
android:src="@drawable/baiqi"/>
<TextView
android:id="@+id/tvName"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="白起"
android:textSize="20sp"
android:gravity="center_horizontal"/>
</LinearLayout>
GeneralBean.java
public class GeneralBean {
private int resid;//图片的id值
private String name;//军事家的姓名
public int getResid() {
return resid;
}
android 31 GridView的更多相关文章
- Android中GridView通过自定义适配器(未优化)实现图文视图排列
Android中GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要显示时,可以使用该组件.下面我们通过代码实现如下图例(为了方便截图,将事件处理(土 ...
- Android中GridView拖拽的效果【android进化三十六】
最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的 ...
- Android中GridView的实现实例
实现效果: activity文件代码: package com.tmacsky; import android.app.Activity; import android.os.Bundle; impo ...
- Android中GridView拖拽的效果
最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的那么 ...
- Android的GridView和Gallery结合Demo
Android的GridView和Gallery结合Demo Demo介绍:首页是一个GridView加载图片,竖屏时显示3列图片,横屏时显示4列图片;并且对图片进行大小限制和加灰色边框处理. 点击某 ...
- Android中GridView的使用——使用自带的SimpleAdapter(简单适配器)
GridView一直是一个系统登录后以九宫格方式展现功能子模块的最佳选择,经过试验和网上资料的查阅,现把实现方式总结一下: 一直是通过自定义Adapter方式,在getView()方法中设置图片的显示 ...
- 从头学起android<GridView网格视图.二十八.>
GridView基于组件的网络格所有的形式的组分的,例如:当制作专辑,所有的图片将在相同的尺寸在不同的显示格在孩子,是否能够依靠这个组件完成.此组件的继承结构参见例如下面: java.lang.Obj ...
- Android中用GridView实现九宫格的两种方法(转)
Android中用GridView实现九宫格的两种方法http://blog.csdn.net/shakespeare001/article/details/7768455 1.传统办法:实现一个继承 ...
- Android 使用GridView以表格的形式显示多张图片
GridView用于在界面上按行.列分布的方式来显示多个组件(而ListView只是以按行的方式) 课程目标 学会使用GridView制作二维布局界面(行.列分布) 数据源(集合) --> 适配 ...
随机推荐
- 【python】只执行普通除法:添加 from __future__ import division
from __future__ import division 注意future前后是两个下划线
- [译]36 Days of Web Testing(一)
[前言]最近负责的一次迭代发布中,一个小需求涉及前端JS改动,在测试这个需求的过程中忽略了浏览器兼容性测试,导致了一个线上bug.恶补下web测试,<36Days of web testing& ...
- fineuploader 上传jquery 控件
fineuploader 昨天用的一个jquery插件. 可参考这篇文章以前写的 file-uploader 跟 这个跟里面介绍的2个jquery 插件相比.觉得更强大写..版本号都3.9 了….. ...
- Phonegap 3.0 获取当前地址位置
新版本的cordova 3.0 中,使用官方的示例可直接获取当前手机的地理位置,前提是手机开启了gps,或可联网. 获取到的是经纬度坐标值等信息,可通过google api 实现通过经纬度获取当前地理 ...
- unity Character Controller 点滴
unity Character Controller 点滴 1.今天在做角色的时候,发现人物跳不起来,原来设置这个属性即可,Step Offset, 这个是台阶的高度,这个值设置的越大,人物爬的越高 ...
- Java语言基础(三) Java注释
Java注释 Java提供了三种注释的方式: ①单行注释:// ②多行注释:/* ... */ ③归档(JavaDoc)注释: /** * 作者:heyiyong * 时间:2013年11月27日 ...
- 【poj1087/uva753】A Plug for UNIX(最大流)
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeti ...
- 《STL源码剖析》chapter2空间配置器allocator
为什么不说allocator是内存配置器而说是空间配置器,因为空间不一定是内存,也可以是磁盘或其他辅助介质.是的,你可以写一个allocator,直接向硬盘取空间.sgi stl提供的配置器,配置的对 ...
- 查看SGA和PGA使用率
select name,total,round(total-free,2) used, round(free,2) free,round((total-free)/total*100,2) pctus ...
- 【转】Adnroid4.0 签名混淆打包(conversion to dalvik format failed with error 1)
原文网址:http://jojol-zhou.iteye.com/blog/1220541 自己的解决方法:关闭Eclipse,再开启Eclipse就可以. 最新Eclipse3.7+android ...