素材巴巴 > 程序开发 >

Android组件的使用:ListView、GridView

程序开发 2023-09-17 23:03:41

1、ListView的使用

1.1ListView的功能就像之前的Spinner、AutoCompleteTextView中添加额外的资源文件一样,可以把其他资源文件的信息作为一个整体加载到ListView的子列表中。

新建一个listview.xml文件,文件中只有一个ListView组件,组件中的信息需要通过另外一个布局文件加载到ListView中,代码如下:

 1 
  2 
  7     
  8      
 12     
 13 

新建listview_extra_layou.xml,代码如下:

 1 
  2 
  7     
  8     
 13     
 18     
 19 

新建Activity类,ListViewDemoActivity,完整代码如下:

 1 package com.example.basiccomponent2;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 
  8 import android.app.Activity;
  9 import android.os.Bundle;
 10 import android.widget.ListView;
 11 import android.widget.SimpleAdapter;
 12 
 13 public class ListViewDemoActivity extends Activity {
 14     private ListView lv_showlistviewitem = null;
 15     private List> list = new ArrayList>();
 16     @Override
 17     protected void onCreate(Bundle savedInstanceState) {
 18         super.onCreate(savedInstanceState);
 19         super.setContentView(R.layout.listview_layout);
 20         lv_showlistviewitem = (ListView)super.findViewById(R.id.lv_showlistviewitem);
 21         //获得listview_extra_layout.xml各组件id名
 22         String id_name[] ={
 23                 "iv_showimage",
 24                 "tv_showtextviewinfo"
 25         }; 
 26         //获得listview_extra_layout.xml中id名对应的R文件id
 27         int id_rid[] = {
 28                 R.id.iv_showimage,
 29                 R.id.tv_showtextviewinfo
 30         };
 31         //定义图片数组数据
 32         int images[] = {
 33                 R.drawable.d1,
 34                 R.drawable.d2,
 35                 R.drawable.d3
 36         };
 37         //定义数组数据
 38         String info[] = {
 39                 "文本1","文本2"
 40         };
 41         //循环把图片和文本加入到map集合中,再把map集合数据添加到list集合中。
 42         for(int i = 0; i map =new  HashMap();
 44             map.put("iv_showimage", images[i]+"");
 45             map.put("tv_showtextviewinfo", info[i]);
 46             list.add(map);
 47         }
 48         //通过adpter加载数据
 49         SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.listview_extra_layout, id_name, id_rid);
 50         //组件设置适配器
 51         lv_showlistviewitem.setAdapter(adapter);
 52     }
 53 }

运行效果:

2、GridView的使用

 1 
  2 
  7     
  8     
 13 

 

 

 1 
  2 
  8     
  9     
 14     
 18 

 

 1 package com.example.entity;
  2 
  3 public class GridViewStudent {
  4     private int photo; // 存放学生图片id
  5     private String name; // 存放学生姓名
  6 
  7     /*
  8      * 无参构造方法
  9      */
 10     public GridViewStudent(){}
 11     /*
 12      * 全参构造方法
 13      */
 14     public GridViewStudent(int photo,String name){
 15         this.photo = photo;
 16         this.name = name;
 17     }
 18     /*
 19      * gettter方法
 20      */
 21     public int getPhoto() {
 22         return photo;
 23     }
 24     /*
 25      * settter方法
 26      */
 27     public void setPhoto(int photo) {
 28         this.photo = photo;
 29     }
 30     /*
 31      * gettter方法
 32      */
 33     public String getName() {
 34         return name;
 35     }
 36     /*
 37      * settter方法
 38      */
 39     public void setName(String name) {
 40         this.name = name;
 41     }
 42 
 43 }

 

 1 package com.example.adapter;
  2 
  3 import java.util.List;
  4 
  5 import android.content.Context;
  6 import android.view.LayoutInflater;
  7 import android.view.View;
  8 import android.view.ViewGroup;
  9 import android.widget.BaseAdapter;
 10 import android.widget.ImageView;
 11 import android.widget.TextView;
 12 
 13 import com.example.activity.R;
 14 import com.example.entity.GridViewStudent;
 15 
 16 public class GridViewDemoAdapter extends BaseAdapter {
 17 
 18     private Context context;            //定义上下文属性(即传过来的GridViewDemoActivity类)
 19     private int resource;               //定义资源文件(即传过来的gridviewdemo_item_layout.xml文件)
 20     private List list; //定义list集合,类型为GridViewStudent
 21     private LayoutInflater inflater;    //定义装载布局
 22     /*
 23      * 全参构造方法,为类中的属性初始化
 24      */
 25     public GridViewDemoAdapter(Context context,int resource,List list){
 26         this.context = context;
 27         this.resource = resource;
 28         this.list = list;
 29         inflater = LayoutInflater.from(context);//初始化LayoutInflater
 30     }
 31     /*
 32      * 获取item个数
 33      */
 34     @Override
 35     public int getCount() {
 36         //如果list集合不为空,则返回集合大小,否则返回0
 37         if(list != null){
 38             return list.size();
 39         }
 40         return 0;
 41     }
 42     /*
 43      * 获取item
 44      */
 45     @Override
 46     public Object getItem(int position) {
 47         return list.get(position);
 48     }
 49     /*
 50      * 获取itemid
 51      */
 52     @Override
 53     public long getItemId(int position) {
 54         return 0;
 55     }
 56     /*
 57      *返回一个布局
 58      */
 59     @Override
 60     public View getView(int position, View convertView, ViewGroup parent) {
 61         if(convertView==null){
 62             convertView = inflater.inflate(resource, null);
 63         }
 64         TextView tv_showgvitemtxt = (TextView)convertView.findViewById(R.id.tv_showgvitemtxt);
 65         ImageView iv_showgvitemimg = (ImageView)convertView.findViewById(R.id.iv_showgvitemimg);
 66         tv_showgvitemtxt.setText(list.get(position).getName());
 67         iv_showgvitemimg.setImageResource(list.get(position).getPhoto());
 68         return convertView;
 69     }
 70 
 71 }

 

 1 package com.example.activity;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import android.app.Activity;
  7 import android.os.Bundle;
  8 import android.widget.GridView;
  9 
 10 import com.example.adapter.GridViewDemoAdapter;
 11 import com.example.entity.GridViewStudent;
 12 
 13 public class GridViewDemoActivtiy extends Activity {
 14     private GridView gv_showinfo = null;    //声明对象
 15 
 16     private List list = new ArrayList();// 声明list集合,用于存储数据
 17 
 18     @Override
 19     protected void onCreate(Bundle savedInstanceState) {
 20         super.onCreate(savedInstanceState);
 21         super.setContentView(R.layout.gridviewdemo_layout);// 设置容器,
 22         gv_showinfo = (GridView) super.findViewById(R.id.gv_showinfo);// 初始化gridview
 23         
 24         //往list中添加值
 25         GridViewStudent gvs1 = new GridViewStudent(R.drawable.ic_launcher, "张三");
 26         list.add(gvs1);
 27         GridViewStudent gvs2 = new GridViewStudent(R.drawable.ic_launcher, "李四");
 28         list.add(gvs2);
 29         GridViewStudent gvs3 = new GridViewStudent(R.drawable.ic_launcher, "王五");
 30         list.add(gvs3);
 31         GridViewStudent gvs4 = new GridViewStudent(R.drawable.ic_launcher, "赵六");
 32         list.add(gvs4);
 33         GridViewStudent gvs5 = new GridViewStudent(R.drawable.ic_launcher, "王二");
 34         list.add(gvs5);
 35         GridViewStudent gvs6 = new GridViewStudent(R.drawable.ic_launcher, "钱七");
 36         list.add(gvs6);
 37         //设置Adapter
 38         GridViewDemoAdapter gridViewAdapter = new GridViewDemoAdapter(
 39                 GridViewDemoActivtiy.this,
 40                 R.layout.gridviewdemo_item_layout,
 41                 list);
 42         gv_showinfo.setAdapter(gridViewAdapter);
 43     }
 44 }

运行效果:

转载于:https://www.cnblogs.com/zhengweicong/p/3213483.html


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。