一起学Android之GridView

移动开发 作者: 2024-08-24 14:55:02
本文以一个简单的小例子,简述Android开发中GridView的常见应用,仅供学习分享使用。
  • android:numColumns="auto_fit"  设置GridView中的列数,可以是数字,auto_fit表示系统自动根据屏幕的大小进行适应性显示。
  • android:verticalSpacing="10dp” 用于设置行与行之间的垂直间隔
  • android:horizontalSpacing="10dp" 用于设置列与列之间的水平间隔
  • android:stretchMode="columnWidth" 设置列中元素的拉伸模式
  • setOnItemClickListener 设置列元素的监听事件
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height 7     tools:context="com.hex.demogridview.MainActivity">
 8     GridView
 9         android:id="@+id/gv_info"
10         android:columnWidth="90dp"
11         android:verticalSpacing="10dp"
12         android:horizontalSpacing13         android:numColumns="auto_fit"
14         android:stretchMode="columnWidth"
15         android:layout_width16         android:layout_height="match_parent"></GridView17 </RelativeLayout>
LinearLayout  3               android:layout_width              android:layout_height              android:orientation="vertical" 6     ImageView
 7         ="@+id/img_info"
 8         android:src="@drawable/a011"
 9 ="wrap_content"/>
11     TextView
12         ="@+id/tv_info"
        android:text="abc"
        android:textSize="20sp"
        android:textAlignment="center"
        android:textColor="@color/colorAccent"
17 18 19 LinearLayout>
class  MyAdapter extends BaseAdapter {
 2         private Context mContext;
 3 
 4         public MyAdapter(Context context) {
 5             this.mContext = context;
 6         }
 7 
 8         @Override
 9         public int getCount() {
10             return mList != null ? mList.size() : 0;
11 12 
13 14         public Object getItem( position) {
15             return mList.get(position);
16 17 
18 19         long getItemId(20              position;
21 22 
23 24         public View getView( position,View convertView,ViewGroup parent) {
25             ViewHolder viewHolder;
26             if (convertView == null) {
27                 // if it's not recycled,initialize some attributes
28                 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); getLayoutInflater();
29                 convertView = inflater.inflate(R.layout.grid_item,);
30                 viewHolder=new ViewHolder();
31                 viewHolder.img = (ImageView) convertView.findViewById(R.id.img_info);
32                 viewHolder.tv = (TextView) convertView.findViewById(R.id.tv_info);
33 
34                 convertView.setTag(viewHolder);
35             } else {
36                 viewHolder = (ViewHolder) convertView.getTag();
37             }
38             viewHolder.img.setImageResource((int)mList.get(position).get("img"));
39             viewHolder.tv.setText((String) mList.get(position).get("txt"40              convertView;
41 42     }
class MainActivity  AppCompatActivity {
 2     private  [] imgs;
 3     private List<Map<String,Object>> mList;
 4      GridView gvInfo;
 5     @Override
protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 9         gvInfo= (GridView) this.findViewById(R.id.gv_info);
10 
11         imgs=new []{R.drawable.a011,R.drawable.a012,R.drawable.a013,R.drawable.a014,R.drawable.a015,R.drawable.a016,
R.drawable.a017,R.drawable.a018,R.drawable.a019,R.drawable.a0110,R.drawable.a0111};
12 mList=new ArrayList<Map<String,1)">(); 13 for(int i=0;i<11;i++14 Map<String,Object> map = new HashMap<String,Object>15 map.put("img",imgs[i]); 16 map.put("txt","第" + i + "个" mList.add(map); 19 MyAdapter adapter =new MyAdapter(20 gvInfo.setAdapter(adapter); 21 gvInfo.setOnItemClickListener( AdapterView.OnItemClickListener() { 22 @Override 23 void onItemClick(AdapterView<?> parent,View view,1)">int position,1)">long id) { 24 TextView textView= (TextView) view.findViewById(R.id.tv_info); 25 String msg = textView.getText().toString(); 26 Toast.makeText(MainActivity.27 28 }); 29 30 }
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_67875.html
一起学Android之GridView