1 概述
绑定ListView数据的适配器有:ArrayAdapter、BaseAdapter、CursorAdapter、SimpleAdapter、ResourceCursorAdapter、SimpleCursorAdapter。在实际应用中,得根据实际情况选择合适的适配器。
一般来说,ArrayAdapter、BaseAdapter、SimpleAdapter、CursorAdapter是比较常用的,区别在于数据源的表现形式不同:
SimpleAdapter和BaseAdapter的数据源一般是List<String>或List<Map<String,?>>;
ArrayAdapter的数据源一般是String[];
CursorAdapter一般就是SQLite数据库了。
2 创建ListView布局文件ListView.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#F0F0F0" android:orientation="vertical" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="#0072E3" android:dividerHeight="0.5px"> </ListView> </LinearLayout>
3 创建listView中Item的布局文件Item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/honeycomb" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:textSize="20sp" android:text="title"/> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:textSize="20sp" android:text="content"/> </LinearLayout> </LinearLayout>
4 创建页脚布局,可以是加载按钮、加载进度条、“上一页”“下一页”之类的布局,看自己需求而定,如下是load_more.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ProgressBar android:id="@+id/load" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ProgressBar> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/loadMoreButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="loadMore" android:text="加载更多" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/btnPrevious" android:layout_width="160dip" android:layout_height="wrap_content" android:text="上一页" /> <Button android:id="@+id/btnNext" android:layout_width="160dip" android:layout_height="wrap_content" android:text="下一页" /> </LinearLayout>
5 定义自己的数据源Adapter
根据自己的需求继承相应的Adapter
必须覆盖的方法:
getCount()
getItem(int position)
getItemId(int position)
getView(int position, ViewcontentView, ViewGroup arg2)
在getView中会根据数据源来渲染界面。下面是其中一个例子:
import java.util.List; import java.util.Map; import com.aaron.util.R; import android.content.Context; import android.text.Html; import android.text.Spanned; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; /** * @author aaron * */ public class TextSearchAdapter extends BaseAdapter { private LayoutInflater inflater; private String keyword; private PageModel mModel; private Context mContext; List<Map<String, Object>> ItemInfoList; public TextSearchAdapter(Context context, List<Map<String, Object>> ItemInfoList, PageModel pageModel, String keyword) { this.ItemInfoList = ItemInfoList; this.mContext = context; this.mModel = pageModel; this.keyword = keyword; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } static class ViewHolder { TextView name; TextView department; TextView content; } /** * [突出显示] * * @param key * @param s * @param color * @return */ private Spanned setKeyColor(String key, String s, String color) { String colorKey = "<font color=\"" + color + "\">" + key + "</font>"; return Html.fromHtml(s.replace(key, colorKey)); } @Override public int getCount() { // TODO Auto-generated method stub return ItemInfoList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return ItemInfoList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (holder == null) { convertView = inflater.inflate(R.layout.listview_item_sql, null); holder = new ViewHolder(); holder.name = (TextView) convertView .findViewById(R.id.list_item_name); holder.department = (TextView) convertView .findViewById(R.id.list_item_department); holder.content = (TextView) convertView .findViewById(R.id.list_item_content); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } String item = (String) ItemInfoList.get(position).get("content"); holder.content.setText(setKeyColor(keyword, item, "red")); holder.name.setText((CharSequence) ItemInfoList.get(position).get( "name")); holder.department.setText((CharSequence) ItemInfoList.get(position) .get("department")); return convertView; } }
6 Activity中初始化
6.1 定义布局
listView = (ListView) this.findViewById(R.id.listView);
6.2 添加页脚
listView.addFooterView(loadMoreView);
当然根据需求也可以设置页头的:
listView.addHeaderView(loadHeadView);
6.3 绑定适配器
adapter = new TextSearchAdapter(this,ItemInfoList,mModel,keyWord); listView.setAdapter(adapter);
6.4 实现OnScrollListener接口,覆盖的方法:
onScroll(AbsListView view,int firstVisibleItem,
int visibleItemCount,int totalItemCount)
onScrollStateChanged(AbsListViewview, int scrollState)
onscroll方法中对数据源进行逻辑处理。
数据更新:adapter.notifyDataSetChanged();
如果是绑定的是CursorAdapter,则adapter.changeCursor(mCursor);
adapter.notifyDataSetChanged();
6.5 实现OnItemClickListener接口,覆盖的方法
onItemClick(AdapterView<?> parent,View view,int position,
long id),实现Item点击事件
7 效果截图
分页的: