http://my.oschina.net/cuitongliang/blog/369632
https://github.com/chrisbanes/Android-PullToRefresh
本部分内容主要为了实现ListView下拉刷新,上拉加载更多以及下拉刷新和上拉加载更多而定义的。其中包含两部分, 一部分是自定义的ListView,它可以作为Library用在实际项目中,第二部分是一个例子,它将自定义的ListView用在这 个例子中,展示了程序员在使用中的注意事项和使用方法。android ListView 上拉刷新 下拉加载更多项目的目录和架构
自定义的ListView的目录结构如下,其必须要设置为Library才可被其它项目使用,使用者只需要将此工程导入到eclipse中即可使用,具体结构如图所示:
例子的目录结构如下,其必须要将自定义的ListView作为Library引用进来,如图所示:
使用方法
接下来我们讲一下例子中相关的知识点。
首先看一下布局文件中的内容 refreshmore.xml中的内容。
?12345678910111213141516 | <? 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 = "vertical" > < com.xiaocui.refreshlistview.PullToRefreshListView xmlns:mylistview = "http://schemas.android.com/apk/res/com.xiaocui.pulltorefresh.sample" android:id = "@+id/pull_to_refreshmore_listview" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:background = "@android:color/white" android:cacheColorHint = "@android:color/transparent" mylistview:mode = "both" /> </ LinearLayout > |
注意:
1.
?1 | xmlns:mylistview="http://schemas.android.com/apk/res/com.xiaocui.pulltorefresh.sample" |
mylistview是自定义的一个命名空间,通过mylistview可以设置对应的属性。在这里设置的mode="both"
2. 后面的 com.xiaocui.pulltorefresh.sample是应用的包名。
接下来,我们再看一下java代码中的写法:
?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 | package com.xiaocui.pulltorefresh.sample; import java.util.ArrayList; import com.xiaocui.pulltorefresh.sample.R; import com.xiaocui.refreshlistview.PullToRefreshBase.OnRefreshListener; import com.xiaocui.refreshlistview.PullToRefreshListView; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; public class RefreshListViewActivity extends Activity { private PullToRefreshListView listView; private ListView listShow; private ListViewSampleAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.refreshmore); listView = (PullToRefreshListView) findViewById(R.id.pull_to_refreshmore_listview); listShow = listView.getRefreshableView(); listView.setOnRefreshListener(refreshListener); adapter = new ListViewSampleAdapter(RefreshListViewActivity. this ); listShow.setAdapter(adapter); //这里是模拟数据,在实际项目中,可以根据业务需求展示不同的数据。 adapter.loadData(); } /** * 上拉刷新,下拉获取更多 */ OnRefreshListener refreshListener = new OnRefreshListener() { // 获取更多操作 @Override public void onMore() { moreList(); } @Override public void onRefresh() { refreshList(); } }; private void refreshList() { //为了便于演示,这里延长了1s的等待时间。 listView.postDelayed( new Runnable() { @Override public void run() { //完成刷新的功能,将头部分隐藏。 listView.onRefreshComplete(); } }, 1000 ); } private void moreList() { //为了便于演示,这里延长了1sd的等待时间。 listView.postDelayed( new Runnable() { @Override public void run() { //完成加载更多的功能,将头部分隐藏。 listView.onRefreshComplete(); } }, 1000 ); } class ListViewSampleAdapter extends android.widget.BaseAdapter { private ArrayList<String> items = new ArrayList<String>(); LayoutInflater inflater; public ListViewSampleAdapter(Context context){ inflater = LayoutInflater.from(context); } public class ViewHolder { public String id; public TextView name; } /** * Loads the data. */ public void loadData() { items = new ArrayList<String>(); items.add( "Ajax Amsterdam" ); items.add( "Barcelona" ); items.add( "Manchester United" ); items.add( "Chelsea" ); items.add( "Real Madrid" ); items.add( "Bayern Munchen" ); items.add( "Internazionale" ); items.add( "Valencia" ); items.add( "Arsenal" ); items.add( "AS Roma" ); items.add( "Tottenham Hotspur" ); items.add( "PSV" ); items.add( "Olympique Lyon" ); items.add( "AC Milan" ); items.add( "Dortmund" ); items.add( "Schalke 04" ); items.add( "Twente" ); items.add( "Porto" ); items.add( "Juventus" ); // MANDATORY: Notify that the data has changed notifyDataSetChanged(); } @Override public int getCount() { return items.size(); } @Override public Object getItem( int position) { return items.get(position); } @Override public long getItemId( int position) { return position; } @Override public View getView( int position, View convertView, ViewGroup parent) { View rowView = convertView; String record = (String) getItem(position); ViewHolder viewHolder = new ViewHolder(); if (convertView == null ) { rowView = inflater.inflate(R.layout.list_item, null ); viewHolder.name = (TextView) rowView.findViewById(R.id.textView1); rowView.setTag(viewHolder); } final ViewHolder holder = (ViewHolder) rowView.getTag(); holder.name.setText(record); return rowView; } } } |
至此本部分就可以运行了。