【1】重寫listView
10年積累的網(wǎng)站制作、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有金山免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
import java.text.SimpleDateFormat; import java.util.Date; import com.example.testdddleapk.R; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; public class RefreshListView extends ListView implements OnScrollListener { private static final int DONE = 0; private static final int PULL_TO_REFRESH = 1; private static final int RELEASE_TO_REFRESH = 2; private static final int REFRESHING = 3; private static final float RATIO = 3;// 用來設(shè)置實(shí)際間距和上邊距之間的比例 private int state;// 當(dāng)前下拉刷新的狀態(tài) private int firstVisibleIndex;// 在listview中第一個(gè)可以看見的item private View headView; private ImageView headArrow; private ProgressBar progressBar; private TextView headTitle; private TextView headLastUpdate; private int headContentWidth; private int headContentHeight; private Animation animation; private Animation reverseAnimation; private OnRefreshListner refreshListner;// 刷新監(jiān)聽器 private boolean isRefreshable; private boolean isRecored = false;// 用來記錄第一次按下坐標(biāo)點(diǎn),在整個(gè)滑動(dòng)的過程中 只記錄一次 private float startY; private boolean isBack = false;// 是從 松開刷新狀態(tài) 來到的 下拉刷新狀態(tài) public RefreshListView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { // listview 設(shè)置滑動(dòng)時(shí)緩沖背景色 setCacheColorHint(0xcc000000); headView = View.inflate(context, R.layout.listview_header, null); headArrow = (ImageView) headView.findViewById(R.id.iv_listview_header_arrow); progressBar = (ProgressBar) headView.findViewById(R.id.pb_listview_header); headTitle = (TextView) headView.findViewById(R.id.tv_listview_header_last_update_time); headLastUpdate = (TextView) headView .findViewById(R.id.tv_listview_header_state); headArrow.setMinimumWidth(50); headArrow.setMinimumHeight(70); MeasureView(headView); headContentWidth = headView.getMeasuredWidth(); headContentHeight = headView.getMeasuredHeight(); headView.setPadding(0, -1*headContentHeight, 0, 0); // 為listView加入頂部View addHeaderView(headView); setOnScrollListener(this); animation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(250); animation.setFillAfter(true);// 設(shè)定動(dòng)畫結(jié)束時(shí),停留在動(dòng)畫結(jié)束位置 (保留動(dòng)畫效果) animation.setInterpolator(new LinearInterpolator());// 勻速變化 reverseAnimation = new RotateAnimation(0, -180,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f); reverseAnimation.setDuration(200); reverseAnimation.setFillAfter(true);// 設(shè)定動(dòng)畫結(jié)束時(shí),停留在動(dòng)畫結(jié)束位置 (保留動(dòng)畫效果) reverseAnimation.setInterpolator(new LinearInterpolator());// 勻速變化 // 設(shè)置當(dāng)前headView的狀態(tài) state = DONE; // 設(shè)置當(dāng)前下拉刷新是否可用 isRefreshable = false; } /** * 測量headView的 寬高 */ private void MeasureView(View child) { ViewGroup.LayoutParams lp = child.getLayoutParams(); if (null == lp) { lp = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); } int measureChildWidth = ViewGroup.getChildMeasureSpec(0, 0, lp.width); int measureChildHeight; if (lp.height > 0) { measureChildHeight = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } else { measureChildHeight = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } child.measure(measureChildWidth, measureChildHeight); } private float tempY=0; private float downY=0; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downY=event.getY(); System.out.println("downY:"+downY); if (firstVisibleIndex == 0 && !isRecored) { startY = event.getY(); isRecored = true; } break; case MotionEvent.ACTION_MOVE: // float tempY = event.getY(); tempY = event.getY(); System.out.println("tempy:"+tempY); if (firstVisibleIndex == 0 && !isRecored) { startY = tempY; isRecored = true; } if (state != REFRESHING) { if (state == PULL_TO_REFRESH) { // 向下拉了 從下拉刷新的狀態(tài) 來到 松開刷新的狀態(tài) if ((tempY - startY) / RATIO >= headContentHeight && (tempY - startY) > 0) { state = RELEASE_TO_REFRESH; changeHeadViewOfState(); } // 向上推了 從下拉刷新的狀態(tài) 來到 刷新完成的狀態(tài) else if ((tempY - startY) <= 0) { state = DONE; changeHeadViewOfState(); } } else if (state == RELEASE_TO_REFRESH) { // 向上推了 還沒有完全將HEADVIEW 隱藏掉(可以看到一部分) // 從松開刷新的狀態(tài) 來到 下拉刷新的狀態(tài) if ((tempY - startY) / RATIO < headContentHeight && (tempY - startY) > 0) { state = PULL_TO_REFRESH; changeHeadViewOfState(); isBack = true; } // 向上推了 一下子推到了最上面 從松開刷新的狀態(tài) 來到 刷新完成的狀態(tài) (數(shù)據(jù)不刷新的) else if ((tempY - startY) <= 0) { state = DONE; changeHeadViewOfState(); } } else if (state == DONE) { // 刷新完成的狀態(tài) 來到 下拉刷新的狀態(tài) if ((tempY - startY) > 0) { state = PULL_TO_REFRESH; changeHeadViewOfState(); } } if (state == PULL_TO_REFRESH) { headView.setPadding(0,(int) ((tempY - startY) / RATIO - headContentHeight),0, 0); } if (state == RELEASE_TO_REFRESH) { headView.setPadding(0, (int) ((tempY - startY) / RATIO - headContentHeight),0, 0); } } break; case MotionEvent.ACTION_UP: if (state != REFRESHING) { if (state == PULL_TO_REFRESH) { // 松手 state = DONE; changeHeadViewOfState(); } else if (state == RELEASE_TO_REFRESH) { // 松手 state = REFRESHING; changeHeadViewOfState(); // 執(zhí)行數(shù)據(jù)刷新方法 onRefresh(); } } isRecored = false; isBack = false; break; } return super.onTouchEvent(event); } /** * 執(zhí)行下拉刷新 */ private void onRefresh() { if (refreshListner != null) { refreshListner.onRefresh(); } } /** * HeadView的狀態(tài)變化效果 */ private void changeHeadViewOfState() { switch (state) { case PULL_TO_REFRESH: headArrow.setVisibility(View.VISIBLE); progressBar.setVisibility(View.GONE); headTitle.setVisibility(View.VISIBLE); headLastUpdate.setVisibility(View.VISIBLE); headArrow.clearAnimation(); headTitle.setText("下拉可以刷新"); //由 松開刷新 到 下拉刷新 if(isBack){ headArrow.startAnimation(animation); isBack = false; } break; case RELEASE_TO_REFRESH: headArrow.setVisibility(View.VISIBLE); progressBar.setVisibility(View.GONE); headTitle.setVisibility(View.VISIBLE); headLastUpdate.setVisibility(View.VISIBLE); headArrow.clearAnimation(); headArrow.startAnimation(reverseAnimation); headTitle.setText("松開可以刷新"); break; case REFRESHING: headArrow.setVisibility(View.GONE); progressBar.setVisibility(View.VISIBLE); headTitle.setVisibility(View.VISIBLE); headLastUpdate.setVisibility(View.VISIBLE); headArrow.clearAnimation(); headTitle.setText("正在刷新..."); headView.setPadding(0, 0, 0, 0); break; case DONE: headArrow.setVisibility(View.VISIBLE); progressBar.setVisibility(View.GONE); headTitle.setVisibility(View.VISIBLE); headLastUpdate.setVisibility(View.VISIBLE); headArrow.clearAnimation(); headTitle.setText("下拉可以刷新"); headView.setPadding(0, -1 * headContentHeight, 0, 0); break; } } private int lastPos;//最后一個(gè)可見的item的位置 private int count;//item總數(shù),注意不是當(dāng)前可見的item總數(shù) private boolean hasFoot = false;//是否有了Foot public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) { firstVisibleIndex = firstVisibleItem; lastPos = getLastVisiblePosition(); count = totalItemCount; //因?yàn)閯傔M(jìn)入的時(shí)候,lastPos=-1,count=0,這個(gè)時(shí)候不能讓它執(zhí)行onAddFoot方法 if(lastPos==count-1 && !hasFoot && lastPos != -1&&((tempY-startY)<0)){ hasFoot = true; onAddFoot(); } } public void onScrollStateChanged(AbsListView view, int scrollState) { if(isFootLoading) return; if(lastPos==count-1 && lastPos != -1&&hasFoot&&footer!=null&&((tempY-downY)>0)){ this.removeFooterView(footer); } if(hasFoot && scrollState==SCROLL_STATE_IDLE&&((tempY-downY)<0)){ isFootLoading = true; onFootLoading(); } } /** * 設(shè)置下拉刷新監(jiān)聽 */ public void setOnRefreshListner(OnRefreshListner listener) { // 設(shè)置下拉刷新可用 isRefreshable = true; refreshListner = listener; } //執(zhí)行底部加載 public void onFootLoading(){ if(footLoadingListener!=null && isFootLoading &&hasFoot&&(tempY-downY)<0) { footLoadingListener.onFootLoading(); } } public void setOnAddFootListener(OnAddFootListener addFootListener){ onAddFootListener = addFootListener; } //執(zhí)行添加foot public void onAddFoot(){ if(onAddFootListener!=null && hasFoot) onAddFootListener.addFoot(); } //是否添加Foot的監(jiān)聽器,如果寫在OnFootLoadingListener中會(huì)有延遲,效果不好 //應(yīng)該是先進(jìn)入添加Foot的狀態(tài),再進(jìn)入FootLoading的狀態(tài) public OnAddFootListener onAddFootListener; //是否進(jìn)入從底部加載數(shù)據(jù)的狀態(tài)的監(jiān)聽器 public OnFootLoadingListener footLoadingListener; //正在加載底部數(shù)據(jù) private boolean isFootLoading = false; public void setOnFootLoadingListener(OnFootLoadingListener footLoading){ footLoadingListener = footLoading; } /** * 下拉刷新監(jiān)聽器 */ public interface OnRefreshListner { // 下拉刷新的時(shí)候,在這里執(zhí)行獲取數(shù)據(jù)的過程 void onRefresh(); } private View footer=null; public void setFooter(View footer){ this.footer=footer; } /** * 上拉刷新監(jiān)聽器 */ public interface OnFootLoadingListener{ // 這里是執(zhí)行后臺獲取數(shù)據(jù)的過程 void onFootLoading(); } /** * 添加Foot的監(jiān)聽器 */ public interface OnAddFootListener{ // 這里是用戶addFootView的操作 void addFoot(); } /** * 底部數(shù)據(jù)加載完成,用戶需要加入一個(gè)removeFootView的操作 */ public void onFootLoadingComplete(){ hasFoot = false; isFootLoading = false; } /** * 上拉刷新完成時(shí) 所執(zhí)行的操作,更改狀態(tài),隱藏head */ public void onRefreshComplete() { state = DONE; changeHeadViewOfState(); headLastUpdate.setText("最后刷新時(shí)間: " + new Date().toLocaleString()); } @Override public void setAdapter(ListAdapter adapter) { headLastUpdate.setText("最后刷新時(shí)間: " + new Date().toLocaleString()); super.setAdapter(adapter); } }
【2】下拉刷新頭部布局文件listview_header.xml
【3】上拉加載更多布局文件listview_footer.xml
【4】progressBar--common_progressbar.xml
【5】使用方法
adapter=new myListAdapter(items,xxx.this); listView.setAdapter(adapter); /** * 下拉刷新回調(diào) */ listView.setOnRefreshListner(new OnRefreshListner() { public void onRefresh() { new AsyncTask>(){ @Override protected ArrayList doInBackground(Void... params) { try { //模擬從服務(wù)器獲取數(shù)據(jù)的過程 Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } return null; } //更新UI的方法,系統(tǒng)自動(dòng)實(shí)現(xiàn) @Override protected void onPostExecute(ArrayList result) { data.addAll(0,result);//注意是往前添加數(shù)據(jù) adapter.notifyDataSetChanged(); listView.onRefreshComplete();//完成下拉刷新,這個(gè)方法要調(diào)用 super.onPostExecute(result); } }.execute(); } }); //上拉加載更多功能 final View footer = View.inflate(getActivity(), R.layout.listview_footer, null); listView.setOnAddFootListener(new OnAddFootListener() { public void addFoot() { listView.setFooter(footer); listView.addFooterView(footer); } }); listView.setOnFootLoadingListener(new OnFootLoadingListener() { public void onFootLoading() { new AsyncTask >(){ @Override protected ArrayList doInBackground(Void... params) { try { //模擬從服務(wù)器獲取數(shù)據(jù)的過程 Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } //在doInBackground后面執(zhí)行 protected void onPostExecute(ArrayList result) { data.addAll(result);//這個(gè)是往后添加數(shù)據(jù) adapter.notifyDataSetChanged(); listView.onFootLoadingComplete();//完成上拉刷新,就是底部加載完畢,這個(gè)方法要調(diào)用 //移除footer,這個(gè)動(dòng)作不能少 listView.removeFooterView(footer); super.onPostExecute(result); } }.execute(); } });
【6】注意事項(xiàng)
listview設(shè)置項(xiàng)目點(diǎn)擊事件
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view,int position, long id) {
if(position==0||position==items.length()+1) return;
//獲取每項(xiàng)的數(shù)據(jù),position需減1,第0項(xiàng)為listview_head
}