Android组件的使用:ListView、GridView
程序开发
2023-09-17 23:03:41
1、ListView的使用
1.1ListView的功能就像之前的Spinner、AutoCompleteTextView中添加额外的资源文件一样,可以把其他资源文件的信息作为一个整体加载到ListView的子列表中。
新建一个listview.xml文件,文件中只有一个ListView组件,组件中的信息需要通过另外一个布局文件加载到ListView中,代码如下:
1 27 8 12 13
新建listview_extra_layou.xml,代码如下:
1 27 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
运行效果:
2、GridView的使用
1 27 8 13
1 28 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 Listlist; //定义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 Listlist = 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
标签:
上一篇:
前端新人关注的Web前端饱和性分析?前端面试必知必会的十点!
下一篇:
相关文章
-
无相关信息