Android初试--自定义ListView的使用
来源:互联网 发布:家用万兆网络 编辑:程序博客网 时间:2024/05/03 02:36
Android初试--自定义ListView的使用
上一篇文章中我们介绍了,关于ListView的简单使用,现在我们就来看看如何自定义ListView的使用。
1.将使用到的图添加到res/drawable-hdpi的文件夹中。
2.在res/layout中定义listview中每一项的布局文件“listview_item.xml”。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/touxiang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/touxiang"
android:layout_toRightOf="@+id/touxiang"
android:textColor="#3333ff"
android:textSize="25sp"
/>
<TextView
android:id="@+id/fabiaotime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/username"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:textColor="#bfbfbf"
android:textSize="15sp"
/>
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/touxiang"
android:layout_alignLeft="@+id/username"
android:layout_marginRight="10dip"
android:textColor="#000000"
android:textSize="20sp"
/>
<ImageView
android:id="@+id/contenttupiao"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_below="@+id/info"
android:layout_alignLeft="@+id/username"
android:layout_marginTop="10dip"
/>
<TextView
android:id="@+id/laiyuan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/username"
android:layout_below="@+id/contenttupiao"
android:layout_marginTop="10dip"
android:textColor="#bfbfbf"
android:textSize="15sp"
/>
</RelativeLayout>
3.在res/layout中修改主布局文件“activity_main.xml”,这个布局中将会包含一个listview组件。
<LinearLayout 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:orientation="vertical"
>
<ListView
android:id="@+id/mainlistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
4.编写主Activity文件。
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView listview=null;
private ArrayList<HashMap<String, Object>> listItem =null;
public String getTime(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowtime= sdf.format(new Date());
return nowtime;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到listview组件
listview=(ListView)findViewById(R.id.mainlistview);
//组织显示在listview中组件的数据内容
//添加第一个数据
HashMap<String,Object> data1=new HashMap<String,Object>();
data1.put("touxiang",R.drawable.tx1);
data1.put("username", "司马懿");
data1.put("nowtime", getTime());
data1.put("contentinfo", "难道真的是天命难违?");
data1.put("contenttupian",R.drawable.contenttupian1);
data1.put("datafrom","来自 我的微博");
//添加第二个数据
HashMap<String,Object> data2=new HashMap<String,Object>();
data2.put("touxiang",R.drawable.tx2);
data2.put("username", "诸葛亮");
data2.put("nowtime", getTime());
data2.put("contentinfo", "观今夜之天象,知天下之大事");
data2.put("contenttupian",R.drawable.contenttupian2);
data2.put("datafrom","来自 我的微博");
//将准备好的数据添加进ArrayList中
listItem = new ArrayList<HashMap<String, Object>>();
listItem.add(data1);
listItem.add(data2);
//创建ListView的适配器
SimpleAdapter listItemAdapter=new SimpleAdapter(
this,
listItem,
R.layout.list_item,
new String[]{"touxiang","username","nowtime","contentinfo","contenttupian","datafrom"},
new int[]{R.id.touxiang,R.id.username,R.id.fabiaotime,R.id.info,R.id.contenttupiao,R.id.laiyuan}
);
//将适配器添加到listview中
listview.setAdapter(listItemAdapter);
//添加点击
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String name=(String)listItem.get(arg2).get("username");
Toast.makeText(MainActivity.this, "大家好,我是"+name, 1000).show();
}
});
}
}
测试效果:
下拉后可以看见下一条,如图:
当点击某一条后,会弹出信息:
- Android初试--自定义ListView的使用
- Android初试--ListView的简单使用
- Android ListView控件的使用(自定义适配器 + ListView + 解决OOM )
- Android初试--Android模拟器的使用
- 自定义Android的ListView
- 自定义Listview的使用
- 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用
- Android笔记—Listview控件的自定义使用
- Android 在slidingMenu中的ListFrament中使用自定义的ListView
- Android中ListView的分页--自定义ListView
- Android 自定义Adapter的Listview
- Android 自定义Adapter的ListView
- android 自定义弹性的ListView
- Android 自定义Adapter的ListView
- Android自定义adapter的listview
- EventBus的使用初试
- 自定义ListView使用的Adapter
- listview自定义adapter的使用
- Activity不容忽视的回调方法
- 【数据结构】 链表的19种基本操作(完整版)
- 【TimerTask】android TimerTask 的简单应用
- 《黑马程序员》 空中网面试第二题分析
- JSONObject和JSONArray的使用方法
- Android初试--自定义ListView的使用
- 分布式计算、并行计算及集群、网格、云计算的区别
- 经典导航栏04,一个精彩的JS+CSS精彩导航实例
- 英雄联盟之谁与争锋
- php || 与 or
- 李京:中国科技大学移动平台——掌上科大
- android多任务下载的状态更新
- LCM在开机动画完成后瞬间灭掉,无法唤醒问题
- MyBatis的动态SQL详解