文/bywinkey 整理时间:2014年12月23日21:41:51
一、概述
使用自定义ListView 编写一个 url快捷方式结果如图所示:
点击一个item 转到对应的网站去
自定义ListView要使用到适配器 SimpleAdapter 加载一个自定义的item 用于显示上图中的 图片和 文字,ListView只负责将这些Item 显示出来即可
期间:使用到HashMap 存放 item里面的键值对,使用SimpleAdapter将 item 与 ListView 组件绑定
这里说道这个item 其实就是一个layout 布局文件,当然也可以使用代码生成,本文不在讲述。
二、步骤
1创建model
2.在默认布局文件上添加一个ListView组件 代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".NavTools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="2px"
android:layout_marginLeft="4px"
android:id="@+id/myList"
></ListView>
<!-- 距离左边 4px 间隔2px -->
</LinearLayout>
</RelativeLayout>
2.在res/layout下创建自定义item 名为navassist.xml 添加ListView用到的item 与默认布局是一样的 这里添加一个ImageView用于显示图片,添加一个TextView显示文字 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3px"
android:layout_marginTop="3px"
android:src="@drawable/baidu"
/>
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50px"
android:textSize="24sp"
android:text="测试数据"
/>
</LinearLayout>
3.在activity中添加如下代码
private ListView listView;//定义一个ListView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_tools);
listView = (ListView) findViewById(R.id.myList);//得到节点
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();//这里前后都是hashMap 不知道为什么?
//定义List 也就是要显示的内容
HashMap<String,Object> map1 =new HashMap<String, Object>();
HashMap<String,Object> map2 =new HashMap<String, Object>();
HashMap<String,Object> map3 =new HashMap<String, Object>();
HashMap<String,Object> map4 =new HashMap<String, Object>();
HashMap<String,Object> map5 =new HashMap<String, Object>();
//添加内容
map1.put("picture",R.drawable.baidu);
map1.put("name","百度");
map2.put("picture",R.drawable.csdn);
map2.put("name","CSDN");
map3.put("picture",R.drawable.sougou);
map3.put("name","搜狗");
map4.put("picture",R.drawable.sousou);
map4.put("name","搜搜");
map5.put("picture",R.drawable.taobao);
map5.put("name","淘宝");
//添加到list中
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
//使用SimpleAdapter适配器将 navassist 与Listview组合
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.navassist,new String[]{"picture","name"},new int[]{R.id.myImageView,R.id.nameTextView});
//将适配器绑定到listview
listView.setAdapter(adapter);
//添加监听事件
listView.setOnItemClickListener(new myListener());
}
//内部类,
class myListener implements AdapterView.OnItemClickListener{
private Intent intent;//使用意图进行转向
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0: // 百度
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));//定义目标
startActivity(intent);//转走
break;
case 2: // csdn
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.csdn.net/"));//定义目标
startActivity(intent);//转走
break;
case 3: // 搜狗
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sogou.com/"));//定义目标
startActivity(intent);//转走
break;
case 4: // sousou
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.soso.com/"));//定义目标
startActivity(intent);//转走
break;
case 5: // 淘宝
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.taobao.com/"));//定义目标
startActivity(intent);//转走
break;
}
}
}
这里用到了android中的跳转意图Intent多用于跳转其具体使用方法会在后面章节中说明
SImpleAdapter 的构造方法有5个参数 每个参数的用途:
-
第一个参数 Context 上下文
-
第二个参数数据源,也就是要给里面填充的数据可以来自数据库 也可以临时写进去
-
第三个参数就是item (定义的布局文件)
-
第四个参数 HashMap中的key 例如我们要显示
map1.put("picture",R.drawable.baidu);
map1.put("name","百度");这里的 picture对应的内容和 name对应的内容,那么就传入一个String类型的数组将key写入: newString{“picture”,”name”};
-
第五个参数:与布局文件navassist 映射, 第一个是图片,传图片id 第二个是字符串就传TextView的id ex: new int[]{R.id.myImageView,R.id.nameTextView}