博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[安卓] 16、ListView和GridView结合显示单元实现自定义列表显示效果
阅读量:6203 次
发布时间:2019-06-21

本文共 5991 字,大约阅读时间需要 19 分钟。

 

List在各种手机应用中都有体现,是安卓UI设计的必修课。

本文将介绍在开发中如何利用ListView和GridView设计自定义列表。

下面分别是用ListView和GridView做的效果:

    

上面两个看似相差很大,但是其代码非常类似,主要有:

   

① 在页面中嵌入ListView或GridView:

ListView的activity_main.xml

1 
10 11
16 17 18 19

GridView的activity_main.xml

1 
11 12
18 19 20

 

② 设计每个单元样式:

两个工程的item.xml一样,如果想定制不同的效果:如朋友圈列表那样的就要精心设计这个item了(不排除有其他方法)!

该item.xml布局显示效果就是上面一个图片,下面一个文字

1 
2
9 10
14 15
22

 

③ 设计Adapter和监听事件:

两个代码很相似,下面红色的部分是将数据和item.xml中的元素绑定~class ItemClickListener implements OnItemClickListener是item点击监听事件类,在其内部调用.get(key)获得点击item中相应的元素值(是一种map的存储方式)。

ListView的MainActivity.java

1 public class MainActivity extends Activity { 2  3     private ListView lview; 4     private List
> data_list; 5 private SimpleAdapter sim_adapter; 6 // ICON 7 private int[] icon = { R.drawable.icon_01, R.drawable.icon_02, 8 R.drawable.icon_03, R.drawable.icon_04, R.drawable.icon_05, 9 R.drawable.icon_06, R.drawable.icon_07, R.drawable.icon_08,10 R.drawable.icon_09, R.drawable.icon_10, R.drawable.icon_11,11 R.drawable.icon_12, R.drawable.icon_13, R.drawable.icon_14 };12 private String[] iconName = { "ͨ茶叶", "汉堡", "肉", "香肠", "披萨", "虾", "水果", "鱼",13 "面包", "蟹", "鸡腿", "根茎蔬菜", "蛋糕", "酒" };14 15 public void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 this.requestWindowFeature(Window.FEATURE_NO_TITLE);18 // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,19 // WindowManager.LayoutParams.FLAG_FULLSCREEN);20 setContentView(R.layout.activity_main);21 22 lview = (ListView) findViewById(R.id.listView1);23 data_list = new ArrayList
>();24 25 getData();26 27 String[] from = { "image", "text" };28 int[] to = { R.id.image, R.id.text };29 sim_adapter = new SimpleAdapter(this, data_list, R.layout.item, from,30 to);31 32 lview.setAdapter(sim_adapter);33 lview.setOnItemClickListener(new ItemClickListener());34 }35 36 public List
> getData() {37 for (int i = 0; i < icon.length; i++) {38 Map
map = new HashMap
();39 map.put("image", icon[i]);40 map.put("text", iconName[i]);41 data_list.add(map);42 }43 44 return data_list;45 }46 47 // 当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件48 class ItemClickListener implements OnItemClickListener {49 public void onItemClick(AdapterView
arg0,// The AdapterView where the50 // click happened51 View arg1,// The view within the AdapterView that was clicked52 int arg2,// The position of the view in the adapter53 long arg3// The row id of the item that was clicked54 ) {55 // 在本例中arg2=arg356 HashMap
item = (HashMap
) arg057 .getItemAtPosition(arg2);58 // 显示所选Item的ItemText59 setTitle((String) item.get("text"));// the item is map,you can60 // seethe function getData,if61 // want get the value, just use62 // .get(key) to get the value63 }64 }65 }

GridView的MainActivity.java

1 public class MainActivity extends Activity { 2  3     private GridView gview; 4     private List
> data_list; 5 private SimpleAdapter sim_adapter; 6 // ICON 7 private int[] icon = { R.drawable.icon_01, R.drawable.icon_02, 8 R.drawable.icon_03, R.drawable.icon_04, R.drawable.icon_05, 9 R.drawable.icon_06, R.drawable.icon_07, R.drawable.icon_08,10 R.drawable.icon_09, R.drawable.icon_10, R.drawable.icon_11,11 R.drawable.icon_12, R.drawable.icon_13, R.drawable.icon_14 };12 private String[] iconName = { "ͨ茶叶", "汉堡", "肉", "香肠", "披萨", "虾", "水果", "鱼",13 "面包", "蟹", "鸡腿", "根茎蔬菜", "蛋糕", "酒" };14 15 public void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 this.requestWindowFeature(Window.FEATURE_NO_TITLE);18 // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,19 // WindowManager.LayoutParams.FLAG_FULLSCREEN);20 setContentView(R.layout.activity_main);21 22 gview = (GridView) findViewById(R.id.gridView1);23 data_list = new ArrayList
>();24 25 getData();26 27 String[] from = { "image", "text" };28 int[] to = { R.id.image, R.id.text };29 sim_adapter = new SimpleAdapter(this, data_list, R.layout.item, from,30 to);31 32 gview.setAdapter(sim_adapter);33 gview.setOnItemClickListener(new ItemClickListener());34 }35 36 public List
> getData() {37 for (int i = 0; i < icon.length; i++) {38 Map
map = new HashMap
();39 map.put("image", icon[i]);40 map.put("text", iconName[i]);41 data_list.add(map);42 }43 44 return data_list;45 }46 47 // 当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件48 class ItemClickListener implements OnItemClickListener {49 public void onItemClick(AdapterView
arg0,// The AdapterView where the50 // click happened51 View arg1,// The view within the AdapterView that was clicked52 int arg2,// The position of the view in the adapter53 long arg3// The row id of the item that was clicked54 ) {55 // 在本例中arg2=arg356 HashMap
item = (HashMap
) arg057 .getItemAtPosition(arg2);58 // 显示所选Item的ItemText59 setTitle((String) item.get("text"));// the item is map,you can60 // seethe function getData,if61 // want get the value, just use62 // .get(key) to get the value63 }64 }65 }

 

在研究上面问题过程中记录了几个有用的链接:

1、安卓 GridView item均匀分布:

2、Android API 中文(15) —— GridView:

3、

4、

5、android selector(如对TextView点击样式改变) - perfect亮:

MMMMMMMMMMMMM

  • 上面工程中点击item的点击效果被我重新定义过了,具体用了第5点链接中说的selector:
    • 在item.xml的第8行:android:background="@drawable/item_selector"
    • 在drawable文件夹内新建item_selector.xml
    • 1 
      2
      3
      5
      7
      8

MMMMMMMMMMMMM

上述两个工程的源码:

GridView工程:

ListView工程:

 

 

@beautifulzzzz

  2015-11-10 持续更新中~

 

转载地址:http://rgmca.baihongyu.com/

你可能感兴趣的文章
python socket 多人聊天室
查看>>
nodejs中require的路径是一个文件夹时发生了什么
查看>>
P1401 城市(30分,正解网络流)
查看>>
用css实现自定义虚线边框
查看>>
C++简单版BitSet求解大量数据是否存在莫个数
查看>>
linux系统编程之进程(七):system()函数使用【转】
查看>>
(译) JSON-RPC 2.0 规范(中文版)
查看>>
linux用户修改用户shell
查看>>
Git 的 .gitignore 配置
查看>>
今天听说了一个压缩解压整型的方式-group-varint
查看>>
【站点部署】解析二级域名并部署站点
查看>>
iOS常用第三方库大全,史上最全第三方库收集
查看>>
iis下php 500错误
查看>>
蛋清打发奶油状
查看>>
二叉树的基本操作及应用(三)
查看>>
repcached配置与简单測试
查看>>
第五章 MVC之Bundle详解
查看>>
Suricata的初始化脚本
查看>>
Makefile中怎么使用Shell if判断
查看>>
Android RecyclerView 二级列表实现
查看>>