主界面↓
package com.wangzhen.pulltorefresh; import java.util.ArrayList;
import java.util.List; import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.wangzhen.adapter.MessageAdapter;
import com.wangzhen.bean.NewsBean;
import com.wangzhen.listener.HttpDataListener;
import com.wangzhen.tasks.LoadNewsTask; import android.support.v7.app.ActionBarActivity;
import android.text.format.DateUtils;
import android.widget.ListView;
import android.content.Context;
import android.os.Bundle; @ContentView(R.layout.activity_main)
public class MainActivity extends ActionBarActivity { @ViewInject(R.id.ListView_PullToRefresh)
private PullToRefreshListView ListView_PullToRefresh; Context mContext;
MessageAdapter adapter;
private HttpDataListener mDataListener; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewUtils.inject(this);
mContext = this; Setup();
InitListener(); List<NewsBean> list = new ArrayList<NewsBean>();
for (int i = 0; i < 5; i++) {
NewsBean bean = new NewsBean();
bean.setNewsImageURL("");
bean.setNewsTitle("标题" + (i + 1));
bean.setNewsContent("内容" + (i + 1));
list.add(bean);
}
adapter = new MessageAdapter(mContext, list);
ListView_PullToRefresh.setAdapter(adapter); ListView_PullToRefresh
.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
String time = DateUtils.formatDateTime(mContext,
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL); // 下拉刷新
if (refreshView.isShownHeader()) {
// 设置刷新标签
ListView_PullToRefresh.getLoadingLayoutProxy()
.setRefreshingLabel("正在刷新");
// 设置刷新标签
ListView_PullToRefresh.getLoadingLayoutProxy()
.setPullLabel("下拉刷新");
// 设置刷新标签
ListView_PullToRefresh.getLoadingLayoutProxy()
.setReleaseLabel("释放开始刷新");
// 设置上一次刷新的提示标签
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel("最后更新时间:" + time);
}
// 上拉加载
if (refreshView.isShownFooter()) {
// 设置刷新标签
ListView_PullToRefresh.getLoadingLayoutProxy()
.setRefreshingLabel("正在加载");
// 设置刷新标签
ListView_PullToRefresh.getLoadingLayoutProxy()
.setPullLabel("上拉加载更多");
// 设置刷新标签
ListView_PullToRefresh.getLoadingLayoutProxy()
.setReleaseLabel("释放开始加载");
// 设置上一次刷新的提示标签
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel("最后加载时间:" + time);
}
new LoadNewsTask(ListView_PullToRefresh, mDataListener)
.execute();
}
});
} /**
* 初始化监听器
*/
private void InitListener() {
mDataListener = new HttpDataListener() { @Override
public void ParseResponse(List<NewsBean> list) {
if (list == null) {
return;
}
adapter.AddData(list);
adapter.notifyDataSetChanged();
}
};
} /**
* 对下拉刷新和上拉加载进行设置
*/
private void Setup() {
/**
* PullToRefresh刷新模式
* Both:上拉刷新和下拉刷新都支持
* DIABLED:禁用上拉下拉刷新
* PULL_FROM_START:仅支持下拉刷新
* PULL_FROM_END:仅支持上拉加载
* MANUAL_REFRESH_ONLY:只允许手动触发
*/
ListView_PullToRefresh.setMode(Mode.BOTH);
}
}
HTTP接口↓
package com.wangzhen.listener; import java.util.List; import com.wangzhen.bean.NewsBean; /**
* HTTP请求接口
* @author Administrator
*
*/
public interface HttpDataListener { /***
* 解析服务器返回结果
*
* @param list
*/
public abstract void ParseResponse(List<NewsBean> list);
}
异步加载类↓
package com.wangzhen.tasks; import java.util.ArrayList;
import java.util.List; import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wangzhen.bean.NewsBean;
import com.wangzhen.listener.HttpDataListener; import android.os.AsyncTask; /**
* 异步加载新闻
*
* @author Administrator
*
*/
public class LoadNewsTask extends AsyncTask<String, Void, List<NewsBean>> { PullToRefreshListView mListView;
HttpDataListener mListener; public LoadNewsTask(PullToRefreshListView listView,
HttpDataListener listener) {
mListView = listView;
mListener = listener;
} @Override
protected void onPostExecute(List<NewsBean> result) {
super.onPostExecute(result);
mListView.onRefreshComplete();
mListener.ParseResponse(result);
} @Override
protected List<NewsBean> doInBackground(String... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return GenerateData();
} /**
* 生成数据
*
* @return
*/
private List<NewsBean> GenerateData() {
List<NewsBean> list = new ArrayList<NewsBean>();
for (int i = 0; i < 5; i++) {
NewsBean bean = new NewsBean();
bean.setNewsImageURL("");
bean.setNewsTitle("添加标题" + (i + 1));
bean.setNewsContent("添加内容" + (i + 1));
list.add(bean);
}
return list;
} }
消息适配器↓
package com.wangzhen.adapter; import java.util.List; import com.wangzhen.bean.NewsBean;
import com.wangzhen.pulltorefresh.R; 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; /**
* 新闻适配器
*
* @author Administrator
*
*/
public class MessageAdapter extends BaseAdapter { Context mContext;
LayoutInflater mInflater; List<NewsBean> mData = null; public MessageAdapter(Context context, List<NewsBean> list) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
mData = list;
} /**
* 增加数据源
*
* @param data
*/
public void AddData(List<NewsBean> list) {
for (NewsBean bean : list) {
this.mData.add(bean);
}
} @Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
} @Override
public NewsBean getItem(int position) {
// TODO Auto-generated method stub
return mData.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) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_layout, null);
holder.mImageView_Icon = (ImageView) convertView
.findViewById(R.id.ImageView_Icon);
holder.mTextView_Title = (TextView) convertView
.findViewById(R.id.TextView_Title);
holder.mTextView_Content = (TextView) convertView
.findViewById(R.id.TextView_Content);
convertView.setTag(holder); } else {
holder = (ViewHolder) convertView.getTag();
} holder.mImageView_Icon.setImageResource(R.drawable.ic_launcher);
holder.mTextView_Title.setText(mData.get(position).getNewsTitle());
holder.mTextView_Content.setText(mData.get(position).getNewsContent()); return convertView;
} class ViewHolder {
ImageView mImageView_Icon;
TextView mTextView_Title;
TextView mTextView_Content;
} }
实体类↓
package com.wangzhen.bean; /**
* 实体类
*
* @author Administrator
*
*/
public class NewsBean { private String NewsImageURL;
private String NewsTitle;
private String NewsContent; public String getNewsImageURL() {
return NewsImageURL;
} public void setNewsImageURL(String newsImageURL) {
NewsImageURL = newsImageURL;
} public String getNewsTitle() {
return NewsTitle;
} public void setNewsTitle(String newsTitle) {
NewsTitle = newsTitle;
} public String getNewsContent() {
return NewsContent;
} public void setNewsContent(String newsContent) {
NewsContent = newsContent;
} }
在PullToRefreshBase文件里要添加以下代码:
// 判别头部是否展示出来,如果展示出来代表下拉使得头部展示。true为下拉
public boolean isShownHeader() {
return getHeaderLayout().isShown();
} // 判别低部是否展示出来,如果展示出来代表上拉使得低部展示。true为上拉
public boolean isShownFooter() {
return getFooterLayout().isShown();
}