1.自定義一個類RefreshListView繼承ListView,必須重寫帶有兩個參數的構造方法java
2.在構造方法中實例化下拉刷新空間,佈局文件refresh_header.xml 自定義ProgressBar,實現紅色的圈圈效果android
3.實現下拉刷新的效果的原理app
view.setPadding(0,-控件的高,0,0);//下拉刷新控件徹底隱藏 view.setPadding(0,0,0,0);//下拉刷新控件徹底顯示 view.setPadding(0,控件的高,0,0);//下拉刷新控件2倍徹底顯示ide
4.重寫onTouchEvent()當手指在屏幕滑動的時候,下拉刷新空間動態隱藏和顯示佈局
int padintTop = -控件的高 + (endY - start)動畫
view.setPadding(0,padintTop,0,0);//下拉刷新控件動態的隱藏和顯示ui
1.記錄y軸上的起始座標 startYthis
2.重寫onTouchEvent();在按下的時候記錄startYspa
3.在移動的時候記錄 endYcode
4.計算偏移量 int distanceY = endY - startY
if(distanceY >0){
int padintTop = -控件的高 + distanceY view.setPadding(0,padintTop,0,0);//下拉刷新控件動態的隱藏和顯示
}
5.判斷頂部輪播圖部分是否徹底顯示-加載更多的bug 怎麼判斷是否徹底顯示呢? 當ListView在屏幕上的Y軸座標小於或者等於頂部輪播圖在Y軸的座標的時候,頂部輪播圖徹底顯示了
在屏幕上移動的時候
//頂部輪播都沒有徹底顯示,你下拉刷新控件就不須要顯示 boolean isDisplayTopNews = isDisplayTopNews();
if(!isDisplayTopNews){
break;
}
addView 添加一個子View
addHeaderView 在ListView中添加一個頭部
6.刷新狀態切換的實現
/** 下拉刷新狀態 */ public static final int PULLDOWNREFRESH = 0;
/** 手鬆刷新狀態 */ public static final int RELEASE_REFRESH = 1; /** 正在刷新狀態 */ public static final int REFRESHING = 2; private int currentStatus = PULL_DOWN_REFRESH;
設置旋轉動畫
/** * 初始化動畫 */ private void initAnimation() { upAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); upAnimation.setDuration(500); upAnimation.setFillAfter(true); donwnAnimation = new RotateAnimation(-180, -360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); donwnAnimation.setDuration(500); donwnAnimation.setFillAfter(true); }
更新狀態
private void refreshHeanderViewStatu() { switch (currentStatus) { case PULL_DOWN_REFRESH://下拉刷新 iv_header_refresh.startAnimation(donwnAnimation); tv_header_status.setText("下拉刷新..."); break; case RELEASE_REFRESH:// 手鬆刷新 iv_header_refresh.startAnimation(upAnimation); tv_header_status.setText("手鬆刷新..."); break; case REFRESHING: //正在刷新 iv_header_refresh.clearAnimation(); iv_header_refresh.setVisibility(View.GONE);//圖片隱藏 tv_header_status.setText("正在刷新..."); pb_header_refresh.setVisibility(View.VISIBLE); break; } }
7.手指離開屏幕的處理,回調接口,聯網請求
public interface OnRefreshListener{
/** 當下拉刷新的時候回調這個方法 */ public void onPullDownRefresh();
}
private OnRefreshListener mOnRefreshListener;
public void setOnRefreshListener(OnRefreshListener l){ mOnRefreshListener = l; }
定義接口 1.在哪裏定義接口,哪一個地方須要回調就在哪裏定義 public interface OnClickListener { /** * Called when a view has been clicked. * * @param v The view that was clicked. */ void onClick(View v); } 2.若是調用結構-在View中
public boolean callOnClick() { ListenerInfo li = mListenerInfo; if (li != null && li.mOnClickListener != null) { li.mOnClickListener.onClick(this); return true; } return false; }
其實就是 void onClick(View v);被回調
3.如何使用 -- 用戶
ivheaderrefresh.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //當點擊的時候會被回調 } });
8.當聯網成功或者失敗的時候,下拉刷新狀態還原
1.在構造方法裏面初始化佈局footerview.xml
view.setPadding(0,-控件的高,0,0);//徹底隱藏
2.監聽ListView的滾動,當滾動到最後一條的時候,加載更多
3.新增長加載更多的方法
public interface OnRefreshListener{
/** 當下拉刷新的時候回調這個方法 */ public void onPullDownRefresh();
/**
當上拉,而且滑動的最後一條數據的時候,回調這個方法 */ public void onLoadMore();
}
4.調用接口 //回調接口 if (onRefreshListener != null) { onRefreshListener.onLoadMore(); }
5.聯網請求更多的數據,加載更多的請求的鏈接是一個新的了
6.把新獲得的數據,加載到原來的ArrayList 集合中
isLoadMore = false; //加載更多 news.addAll(detailPagerBean.getData().getNews()); adapter.notifyDataSetChanged();//適配器刷新
7.刷新,而且恢復初始狀態
isLoadMore = false; footView.setPadding(0,-footViewHeight,0,0);
本庫介紹:做者
1.項目關聯庫
2.佈局中使用
3.設置適配器
案例代碼
package com.atguigu.refreshlistviewdemo; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.atguigu.refreshlistview.RefreshListView; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; public class MainActivity extends Activity { private static final int PULL_DONW_REFRESH = 1; private static final int LOADMORE = 2; private RefreshListView refreshListView; private ArrayList<String> strings; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); refreshListView = (RefreshListView) findViewById(R.id.refreshListView); //準備數據 strings = new ArrayList<>(); for (int i = 0; i < 100; i++) { strings.add(getsystemTime() + "-------" + i); } myAdapter = new MyAdapter(); //設置適配器 refreshListView.setAdapter(myAdapter); refreshListView.setOnRefreshListener(new RefreshListView.OnRefreshListener() { @Override public void onPullDownRefresh() { getDataFromNet(); } @Override public void onLoadMore() { getMoreData(); } }); } private void getMoreData() { new Thread() { @Override public void run() { super.run(); //真正聯網 SystemClock.sleep(2000); for (int i = 0; i < 20; i++) { strings.add(getsystemTime() + "---m----" + i); } handler.sendEmptyMessage(LOADMORE); } }.start(); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case PULL_DONW_REFRESH: myAdapter.notifyDataSetChanged(); refreshListView.onFinishRefresh(true); break; case LOADMORE: myAdapter.notifyDataSetChanged(); refreshListView.onFinishRefresh(true); break; } super.handleMessage(msg); } }; private void getDataFromNet() { new Thread() { @Override public void run() { super.run(); SystemClock.sleep(2000); strings = new ArrayList<>(); for (int i = 0; i < 100; i++) { strings.add(getsystemTime() + "---xxxxx----" + i); } handler.sendEmptyMessage(PULL_DONW_REFRESH); } }.start(); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return strings.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = new TextView(MainActivity.this); textView.setText(strings.get(position)); textView.setTextSize(30); textView.setTextColor(Color.BLACK); return textView; } } private String getsystemTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()); } }