ListView其实就是一个用列表形式来展示数据的View
根据适配器adapter的不同可以实现不同的效果:
比如 丑得要命的:
聊天撩妹的:
还有这种
嘻嘻
让我们来看一下最简单的Listview+Arrayadapter
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#d8d8d8"
android:dividerHeight="1dp">
</ListView>
</LinearLayout>
ListView.class:
public class ListView extends Activity{
private ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView)findViewById(R.id.listview);
listView.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,getData()));
setContentView(listView);
}
private List<String> getData(){
List<String> data=new ArrayList<String>();
data.add("我是爷爷");
data.add("我是爸爸");
data.add("我是哥哥");
data.add("那我是谁");
return data;
}
}
效果:
ListView+BaseAdapter
这个稍微复杂一点,需要自定义布局文件
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:layout_margin="8dp"
android:id="@+id/user_face"
android:layout_width="46dp"
android:layout_height="46dp" />
<TextView
android:text="234555566"
android:textColor="#111111"
android:textSize="18dp"
android:id="@+id/user_text"
android:layout_marginTop="12dp"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@id/user_face"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="123456"
android:textColor="#a1a1a1"
android:id="@+id/user_message"
android:layout_marginLeft="2dp"
android:layout_below="@id/user_text"
android:layout_toRightOf="@id/user_face"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="11:30"
android:textColor="#a1a1a1"
android:padding="12dp"
android:id="@+id/user_time"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#d8d8d8"
android:dividerHeight="1dp">
</ListView>
</LinearLayout>
MyAdapter.class:
package com.example.sm1n9.smingshop.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.sm1n9.smingshop.R;
import java.util.HashMap;
import java.util.List;
/**
* Created by user on 2016/6/23.
*/
public class MyAdpterTest extends BaseAdapter {
private Context context;//上下文对象
private List<HashMap<String,Object>> list;//集合类 存放数据
private LayoutInflater inflater;//布局容器
public MyAdpterTest(Context context, List<HashMap<String, Object>> list) {//构造方法
this.context = context;
this.list = list;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//ViewHolder通常出现在适配器里,为的是listview滚动的时候快速设置值,而不必每次都重新创建很多对象,从而提升性能
ViewHolder holder=null;
if (convertView == null) {
holder=new ViewHolder();
convertView=inflater.inflate(R.layout.item,null);
holder.img= (ImageView) convertView.findViewById(R.id.user_face);
holder.title= (TextView) convertView.findViewById(R.id.user_text);
holder.info= (TextView) convertView.findViewById(R.id.user_message);
holder.time= (TextView) convertView.findViewById(R.id.user_time);
convertView.setTag(holder);
}
else{
holder= (ViewHolder) convertView.getTag();
}
holder.img.setBackgroundResource((Integer) list.get(position).get("image"));
holder.title.setText((CharSequence) list.get(position).get("title"));
holder.info.setText((CharSequence) list.get(position).get("info"));
holder.time.setText((CharSequence) list.get(position).get("time"));
return convertView;
}
class ViewHolder{
ImageView img;
TextView title;
TextView info;
TextView time;
}
}
ListView.class:
public class ListView extends Activity{
private ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView)findViewById(R.id.listview);
List<HashMap<String,Object>> maps=new ArrayList<HashMap<String, Object>>();
HashMap<String,Object> map= new HashMap<String,Object>();
map.put("image", R.mipmap.h1);
map.put("title", "系统消息");
map.put("info", "OA办公体验站全新上线 哪里不会点哪里");
map.put("time", "刚刚");
maps.add(map);
map=new HashMap<String,Object>();
map.put("image", R.mipmap.h5);
map.put("title","abe");
map.put("info", "在干嘛?");
map.put("time","15:30");
map.put("ban",R.mipmap.icon_banned);
maps.add(map);
map=new HashMap<String,Object>();
map.put("image", R.mipmap.h2);
map.put("title", "Jack");
map.put("info", "o(^▽^)o");
map.put("time","12:30");
map.put("ban",R.mipmap.icon_banned);
maps.add(map);
map=new HashMap<String,Object>();
map.put("image", R.mipmap.h4);
map.put("title", "比卡丘");
map.put("info", "嗨");
map.put("time","昨天");
map.put("ban",R.mipmap.icon_banned);
maps.add(map);
listView.setAdapter(new MyAdpter(getActivity(), maps));
}
}
效果:
补充说明:
listView在开始绘制的时,系统首先调用getCount()函数,根据他的返回值得到listView的长度,然后根据这个长度,调用getView()逐一绘制每一行。如果你的getCount()返回值是0的话,列表将不显示,## 标题 ##同样return 1,就只显示一行。