通过接口实现自定义的监听,自己定义一个接口OnRefreshListener,在接口定义的函数中mListener.onRefresh();函数中调用setOnRefreshListener()再在使用监听的函数中实现接口的方法即可
自定义监听RefreshListView.java
在函数重视使用监听
/*
* 刷新控件的布局
*/
private void refreshState() {
switch(mCurrentState){
//下拉刷新状态
case STATE_PULL_REFRESH:
tv_title.setText("下拉刷新");
iv_arr.setVisibility(View.VISIBLE);
pb_progress.setVisibility(View.INVISIBLE);
iv_arr.startAnimation(animDown);
break;
//松开刷新
case STATE_RELEASH_REFRESH:
tv_title.setText("松开刷新");
iv_arr.setVisibility(View.VISIBLE);
pb_progress.setVisibility(View.INVISIBLE);
iv_arr.startAnimation(animUp);
break;
//正在刷新
case STATE_REFRESHING:
tv_title.setText("正在刷新...");
iv_arr.clearAnimation();// 必须先清除动画,才能隐藏
iv_arr.setVisibility(View.INVISIBLE);
pb_progress.setVisibility(View.VISIBLE);
if(mListener != null){
mListener.onRefresh();
}
break;
default:
break;
}
}
定义
//创建接口,监听下拉刷新。刷新的时候调用接口方法
OnRefreshListener mListener;
public void setOnRefreshListener(OnRefreshListener listener) {
mListener = listener;
}
public interface OnRefreshListener {
public void onRefresh();
public void onLoadMore();// 加载下一页数据
}
自定义监听的使用
//监听下拉刷新,调用自定义布局中的接口
lv_list.setOnRefreshListener(new OnRefreshListener(){
@Override
public void onRefresh() {
//读取数据
getDataFromServer();
//结束刷新
lv_list.onRefreshComplete(true);
}
@Override
public void onLoadMore() {
if(mMoreUrl != null){
getMoreDataFromServer();
}else{
Toast.makeText(mActivity, "最后一条了", Toast.LENGTH_SHORT).show();
//隐藏脚部局
lv_list.onRefreshComplete(false);
}
}
});
listener的使用
package com.ldw.news.base;
import java.util.ArrayList;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.ldw.news.R;
import com.ldw.news.domain.NewsData.NewsTabData;
import com.ldw.news.domain.TabData;
import com.ldw.news.domain.TabData.TabNewsData;
import com.ldw.news.domain.TabData.TopNewsData;
import com.ldw.news.global.ClobalContants;
import com.ldw.news.view.RefreshListView;
import com.ldw.news.view.RefreshListView.OnRefreshListener;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.viewpagerindicator.CirclePageIndicator;
/*
* 左边菜单栏右边详情页,最上面对应的tab,页签
*/
public class TabDetailPager extends BaseMenuDetailPager implements OnPageChangeListener{
//携带数据详情的数据
private NewsTabData mTabData;
private TextView text;
private String mUrl;
private TabData mTabDetailData;
@ViewInject(R.id.vp_news)
private ViewPager mViewPager;
@ViewInject(R.id.tv_title)
private TextView tv_title;//banner的标题
@ViewInject(R.id.indicator)
private CirclePageIndicator indicator;//banner下面的小圆点
@ViewInject(R.id.lv_list)
private RefreshListView lv_list;//新闻列表
private ArrayList<TopNewsData> mTopNewsList;//banner的数据
private ArrayList<TabNewsData> mNewsList;
private String mMoreUrl;//加载更多的链接地址
private NewsAdapter mNewsAdapter;
public TabDetailPager(Activity activity, NewsTabData newsTabData) {
super(activity);
mTabData = newsTabData;
//图片的地址
mUrl = ClobalContants.SERVER_URL + mTabData.url;
}
@Override
public View initView() {
View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);
//把banner添加到ListView的头部,实现二者的组合
View headView = View.inflate(mActivity, R.layout.list_header_topnews, null);
ViewUtils.inject(this, view);
ViewUtils.inject(this, headView);
//添加到ListView的头部
lv_list.addHeaderView(headView);
//监听下拉刷新,调用自定义布局中的接口
lv_list.setOnRefreshListener(new OnRefreshListener(){
@Override
public void onRefresh() {
//读取数据
getDataFromServer();
//结束刷新
lv_list.onRefreshComplete(true);
}
@Override
public void onLoadMore() {
if(mMoreUrl != null){
getMoreDataFromServer();
}else{
Toast.makeText(mActivity, "最后一条了", Toast.LENGTH_SHORT).show();
//隐藏脚部局
lv_list.onRefreshComplete(false);
}
}
});
//监听Viewager的滑动
//mViewPager.setOnPageChangeListener(this);
// 向FrameLayout中动态添加布局
return view;
}
@Override
public void initData(){
getDataFromServer();
}
/*
* 获取网络数据
*/
private void getDataFromServer() {
//从服务器中获取数据
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, mUrl, new RequestCallBack<String>(){
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//获取到返回的结果
String result = (String) responseInfo.result;
System.out.println("tab详情返回结果:" + result);
parseData(result, false);
}
@Override
public void onFailure(HttpException error, String msg) {
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
.show();
//打印错误信息
error.printStackTrace();
}
});
}
/*
* 获取更多网络数据
*/
private void getMoreDataFromServer() {
//从服务器中获取数据
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, mMoreUrl, new RequestCallBack<String>(){
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//获取到返回的结果
String result = (String) responseInfo.result;
System.out.println("tab详情返回结果:" + result);
parseData(result, true);
}
@Override
public void onFailure(HttpException error, String msg) {
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
.show();
//打印错误信息
error.printStackTrace();
}
});
}
/*
* 解析网络数据,第二个参数表示是否加载下一页
*/
protected void parseData(String result,boolean isMore) {
//使用Gson解析
Gson gson = new Gson();
mTabDetailData = gson.fromJson(result, TabData.class);
System.out.println("解析结果:" + mTabDetailData);
//服务器读取加载更多数据
String more = mTabDetailData.data.more;
if(!TextUtils.isEmpty(more)){
mMoreUrl = ClobalContants.SERVER_URL + more;
}else{
mMoreUrl = null;
}
//不加载更多
if(!isMore){
//新闻列表的数据ListView
mNewsList = mTabDetailData.data.news;
//取到banner新闻top的数据
mTopNewsList = mTabDetailData.data.topnews;
//top新闻填充
if(mTopNewsList != null){
//填充top新闻的pagre
mViewPager.setAdapter(new TopNewsAdapter());
//banner下面的小圆点,需要在adapter初始化以后才能显示
indicator.setViewPager(mViewPager);
indicator.setSnap(true);//支持快照显示
indicator.setOnPageChangeListener(this);//添加监听,修改文本内容
indicator.onPageSelected(0);//默认选择第一个,防止图片和远点不对应
//解析数据的时候初始化标题
tv_title.setText(mTopNewsList.get(0).title);
}
//填充新闻列表
if(mNewsList != null){
mNewsAdapter = new NewsAdapter();
lv_list.setAdapter(mNewsAdapter);
}
}else{
//是加载下一页,需要吧数据追加到原来的集合
ArrayList<TabNewsData> news = mTabDetailData.data.news;
mNewsList.addAll(news);
//数据发生改变
mNewsAdapter.notifyDataSetChanged();
}
}
/*
* 头条新闻,banner
*/
class TopNewsAdapter extends PagerAdapter{
private BitmapUtils utils;
//构造函数
public TopNewsAdapter(){
utils = new BitmapUtils(mActivity);
//加载数据过程中,设置默认加载的图片
utils.configDefaultLoadingImage(R.drawable.topnews_item_default);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mTabDetailData.data.topnews.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//初始化banner图
ImageView image = new ImageView(mActivity);
//填充布局对象
//image.setImageResource(R.drawable.topnews_item_default);
//填充平铺布局,基于控件的大小
image.setScaleType(ScaleType.FIT_XY);
//获取到图片资源的数据的地址
TopNewsData topNewsData = mTabDetailData.data.topnews.get(position);
utils.display(image, topNewsData.topimage);
//添加image数据
container.addView(image);
return image;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
//新闻列表的适配器
class NewsAdapter extends BaseAdapter{
private BitmapUtils utils;
//构造函数初始化图片
public NewsAdapter(){
utils = new BitmapUtils(mActivity);
//加载的时候显示默认图片
utils.configDefaultLoadingImage(R.drawable.pic_item_list_default);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mNewsList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mNewsList.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){
convertView = View.inflate(mActivity, R.layout.list_news_item, null);
holder = new ViewHolder();
holder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
TabNewsData item = (TabNewsData) getItem(position);
holder.tv_title.setText(item.title);
holder.tv_date.setText(item.pupdate);
utils.display(holder.iv_pic, item.listimage);
return convertView;
}
}
static class ViewHolder{
public TextView tv_title;
public TextView tv_date;
public ImageView iv_pic;
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub
}
//滑动的监听,当页面被选中的时候更新标题
@Override
public void onPageSelected(int position) {
//TopNewsData topNewsData= mTabDetailData.data.topnews.get(position);
tv_title.setText(mTopNewsList.get(position).title);
}
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
}
自定义监听
package com.ldw.news.view;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.ldw.news.R;
/*
* 详情页新闻列表的下拉刷新,使用自定义的布局来实现
*/
public class RefreshListView extends ListView implements OnScrollListener{
private static final int STATE_PULL_REFRESH = 0;//下拉刷新
private static final int STATE_RELEASH_REFRESH = 1; //松开刷新
private static final int STATE_REFRESHING = 2; //刷新
//记录当前的状态,默认下拉刷新
private int mCurrentState = STATE_PULL_REFRESH;
//起点的Y坐标
private int startY = -1;
private int mHeaderViewHeight;
private View mHeaderView;
private TextView tv_title;
private TextView tv_time;
private ImageView iv_arr;
private ProgressBar pb_progress;
private RotateAnimation animUp;
private RotateAnimation animDown;
public RefreshListView(Context context) {
super(context);
initHeaderView();
initFooterView();
}
public RefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
initHeaderView();
initFooterView();
}
public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initHeaderView();
initFooterView();
}
/*
* 下拉刷新布局
*/
public void initHeaderView(){
//填充布局
mHeaderView = View.inflate(getContext(), R.layout.refresh_header, null);
//添加头部,先加谁谁在最上面
this.addHeaderView(mHeaderView);
tv_title = (TextView) mHeaderView.findViewById(R.id.tv_title);
tv_time = (TextView) mHeaderView.findViewById(R.id.tv_time);
iv_arr = (ImageView) mHeaderView.findViewById(R.id.iv_arr);
pb_progress = (ProgressBar) mHeaderView.findViewById(R.id.pb_progress);
mHeaderView.measure(0, 0);
//获取到下拉条的高度
mHeaderViewHeight = mHeaderView.getMeasuredHeight();
//初始化的时候,隐藏头布局
mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);
//初始化箭头上下动画
initArrowAnim();
//初始化时间
tv_time.setText("最后刷新时间:" + getCurrentTime());
}
/*
* 初始化脚布局
*/
private void initFooterView() {
mFooterView = View.inflate(getContext(),
R.layout.refresh_listview_footer, null);
this.addFooterView(mFooterView);
mFooterView.measure(0, 0);
mFooterViewHeight = mFooterView.getMeasuredHeight();
// 隐藏脚布局
mFooterView.setPadding(0, -mFooterViewHeight, 0, 0);
//设置滑动监听,根据滑动的状态设置脚标
this.setOnScrollListener(this);
}
//触摸事件,触摸可以拉出下拉条
@Override
public boolean onTouchEvent(MotionEvent ev){
switch(ev.getAction()){
case MotionEvent.ACTION_DOWN:
//起始点的Y的坐标
startY = (int) ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
//如果没有获取到Y的初始值就重新获取
if(startY == -1){
startY = (int) ev.getRawY();
}
// 正在刷新时不做处理
if (mCurrentState == STATE_REFRESHING) {
break;
}
int endY =(int) ev.getRawY();
int dy = endY - startY;//移动偏移量
//下拉的时候dy>0,同时是第一个元素,允许下拉
if(dy > 0 && getFirstVisiblePosition() == 0){
//计算布局应该显示多少
int padding = dy - mHeaderViewHeight;
mHeaderView.setPadding(0, -padding, 0, 0);
//根据padding的正负判断是否完全显示
if(padding > 0 && mCurrentState != STATE_RELEASH_REFRESH){
//完全显示,松开刷新
mCurrentState = STATE_RELEASH_REFRESH;
refreshState();
}else if(padding < 0 && mCurrentState != STATE_PULL_REFRESH){
//没有完全显示padding<0,状态是下拉刷新
mCurrentState = STATE_PULL_REFRESH;
refreshState();
}
return true;
}
break;
case MotionEvent.ACTION_UP:
//没有触摸以后重置起始点的坐标
startY = -1;
//松开以后,判断当前的状态是不是松开刷新,松开刷新就改变状态,如果是下拉刷新就隐藏
if (mCurrentState == STATE_RELEASH_REFRESH) {
mCurrentState = STATE_REFRESHING;// 正在刷新
mHeaderView.setPadding(0, 0, 0, 0);// 显示
refreshState();
} else if (mCurrentState == STATE_PULL_REFRESH) {
mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);// 隐藏
}
break;
}
return super.onTouchEvent(ev);
}
/*
* 刷新控件的布局
*/
private void refreshState() {
switch(mCurrentState){
//下拉刷新状态
case STATE_PULL_REFRESH:
tv_title.setText("下拉刷新");
iv_arr.setVisibility(View.VISIBLE);
pb_progress.setVisibility(View.INVISIBLE);
iv_arr.startAnimation(animDown);
break;
//松开刷新
case STATE_RELEASH_REFRESH:
tv_title.setText("松开刷新");
iv_arr.setVisibility(View.VISIBLE);
pb_progress.setVisibility(View.INVISIBLE);
iv_arr.startAnimation(animUp);
break;
//正在刷新
case STATE_REFRESHING:
tv_title.setText("正在刷新...");
iv_arr.clearAnimation();// 必须先清除动画,才能隐藏
iv_arr.setVisibility(View.INVISIBLE);
pb_progress.setVisibility(View.VISIBLE);
if(mListener != null){
mListener.onRefresh();
}
break;
default:
break;
}
}
/**
* 初始化箭头动画
*/
private void initArrowAnim() {
// 箭头向上动画
animUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animUp.setDuration(200);
animUp.setFillAfter(true);
// 箭头向下动画
animDown = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animDown.setDuration(200);
animDown.setFillAfter(true);
}
//创建接口,监听下拉刷新。刷新的时候调用接口方法
OnRefreshListener mListener;
private View mFooterView;
private int mFooterViewHeight;
public void setOnRefreshListener(OnRefreshListener listener) {
mListener = listener;
}
public interface OnRefreshListener {
public void onRefresh();
public void onLoadMore();// 加载下一页数据
}
/*
* 收起下拉刷新的控件
*/
public void onRefreshComplete(boolean success) {
if (isLoadingMore) {// 正在加载更多...
mFooterView.setPadding(0, -mFooterViewHeight, 0, 0);
// 隐藏脚布局
isLoadingMore = false;
} else {
//不加载更多的时候收起控件
mCurrentState = STATE_PULL_REFRESH;
tv_title.setText("下拉刷新");
iv_arr.setVisibility(View.VISIBLE);
pb_progress.setVisibility(View.INVISIBLE);
mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);// 隐藏
if (success) {
tv_time.setText("最后刷新时间:" + getCurrentTime());
}
}
}
/*
* 获取当前时间
*/
public String getCurrentTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date());
}
//加载更多
private boolean isLoadingMore;
//滑动状态的变化
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//状态是空闲或者飞速的滑动
if (scrollState == SCROLL_STATE_IDLE
|| scrollState == SCROLL_STATE_FLING) {
// 滑动到最后
if (getLastVisiblePosition() == getCount() - 1 && !isLoadingMore) {
System.out.println("到底了.....");
mFooterView.setPadding(0, 0, 0, 0);// 显示
setSelection(getCount() - 1);// 改变listview显示位置
//到最后了显示脚步布局
isLoadingMore = true;
if (mListener != null) {
mListener.onLoadMore();
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
}