注:本文只是一个学习笔记 用以记录自己学到哪了
本文来自 http://blog.csdn.net/manymore13
① 先上个简单点的ListView
public class ListViewActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,getData());
this.setListAdapter(adapter);
}
private String[] getData()
{
String[] data = {"中国","美国","俄罗斯","澳大利亚","加拿大"};
return data;
}
}
下面看一下效果图:
public ArrayAdapter (Context context, int textViewResourceId, T[] objects)
ArrayAdapter 的构造函数的三个参数依次是this ,布局文件,数据源(在这里是String[])另外说一下那个布局文件:描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字
②下面利用SimpleAdapter 来说一下Listview 效仿了QQ的布局 先看一下效果:
本程序有ListViewActivity.java 和 my_list_item.xml主要文件组成
my_list_item.xml 如下
<?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/Image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_margin="5dip"
android:src="@drawable/j1"
android:scaleType="centerInside"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:textSize="22sp"
android:textStyle="bold"
android:singleLine="true"/>
<TextView
android:id="@+id/sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="20sp"
android:singleLine="true"/>
</LinearLayout>
</LinearLayout>
ListViewActivity.java 文件如下:
下面是onCreate()方法
List<Map<String, Object>> listData = this.getData();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[]{"nickname","sign","image"};
int[] to = new int[]{R.id.nickname,R.id.sign,R.id.Image};
//为ListView设置适配器
SimpleAdapter adapter = new SimpleAdapter(this,listData,
R.layout.my_list_item, from,to);
this.setListAdapter(adapter);
}
PS:from,to要对应
获取数据即listview中文字,图片
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("nickname", "岁月神偷");
map.put("sign", "一切都会变好的!");
map.put("image", R.drawable.j1);
list.add(map);
map = new HashMap<String,Object>();
map.put("nickname", "被水淹死的鱼");
map.put("sign", "等待春天的到来");
map.put("image", R.drawable.j2);
list.add(map);
map = new HashMap<String,Object>();
map.put("nickname", "我相信");
map.put("sign", "只希望能手牵手在太阳下散步”....“最绝望的念想、最悲恸的守望”");
map.put("image", R.drawable.j3);
list.add(map);
map = new HashMap<String,Object>();
map.put("nickname", "龟龟");
map.put("sign", "当一切 ...已成惘然...");
map.put("image", R.drawable.j4);
list.add(map);
return list;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
this.displayMessage(position);
super.onListItemClick(l, v, position, id);
}
public void displayMessage(int position)
{
String nickname = (String)listData.get(position).get("nickname");
String sign = (String)listData.get(position).get("sign");
Toast.makeText(this, nickname+": "+sign,Toast.LENGTH_SHORT).show();
}
③ 上面的布局稍微难一些,接着我们来改变Listview的一些特征
下面是selector.xml放在drawable-hdpi图片文件夹中 这个selector.xml主要是更改ListView的item项,被选中,被按下整体颜色还有item的宽度,高以及边角弧度
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<gradient android:angle="270" android:endColor="#CD2990"
android:startColor="#A5D245" />
<size android:height="60dp" android:width="320dp" />
<corners android:radius="15dp" />
</shape>
</item>
<item android:state_pressed="true">
<shape>
<gradient android:angle="270" android:endColor="#99BD4C"
android:startColor="#A5D245"/>
<size android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
<item>
<shape>
<gradient android:angle="270" android:endColor="#A8C3B0"
android:startColor="#C6CFCE" />
<size android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
</selector>
<?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="wrap_content"
android:background="@drawable/selector" 这里引用到上面selector.xml文件
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
/>
<TextView
android:text="data"
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="@color/black"
>
</TextView>
</LinearLayout>
接着是主要布局main.xml 这里面更改了item之间的分割边距(15px) 选中颜色 关于ListView可见Api文档
ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#253853"
>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="15px"
android:listSelector="#264365"
android:drawSelectorOnTop="false"
>
</ListView>
</LinearLayout>
package xb.customlist;
import java.util.ArrayList;
import java.util.HashMap;
import android.R.array;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class mainActivity extends Activity {
ListView list;
String data[] = new String[]{
"中国","美国","俄罗斯","澳大利亚","加拿大"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list =(ListView) findViewById(R.id.list);
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item,
new String[]{"title","img"}, new int[]{R.id.title,R.id.img});
list.setAdapter(adapter);
}
private ArrayList<HashMap<String, Object>> getData() {
ArrayList<HashMap<String, Object>> dlist = new ArrayList<HashMap<String,Object>>();
for(int i =0;i<data.length;i++){
HashMap<String, Object>map = new HashMap<String, Object>();
map.put("title", data[i]);
map.put("img", R.drawable.item_icon);
dlist.add(map);
}
return dlist;
}
}
好了,来看看效果是怎样的
这次关于学习ListVIew的笔记就这么多,以后随着深入Listview会扩充这次笔记的。