码迷,mamicode.com
首页 > 其他好文 > 详细

下拉刷新和上拉加载

时间:2016-05-24 00:29:52      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

1、视图

1)主视图

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     >
 7 
 8     <com.zhyui.zdylistview.ZyhListView
 9         android:id="@+id/zyh_lv"
10         android:layout_width="match_parent"
11         android:layout_height="0dip"
12         android:layout_weight="1"
13         >
14     </com.zhyui.zdylistview.ZyhListView>
15 
16 </LinearLayout>

2)头部视图

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content" 
 5     android:orientation="horizontal"
 6     >
 7     <FrameLayout 
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content" >
10         <ImageView 
11             android:id="@+id/iv_arr"
12             android:layout_width="wrap_content"
13             android:layout_height="wrap_content"
14             android:layout_gravity="center"
15             android:src="@drawable/ic_launcher"
16             />
17         <ProgressBar 
18             android:id="@+id/pb"
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content"
21             android:layout_gravity="center"
22             style="?android:attr/progressBarStyleLarge"
23             android:visibility="invisible"
24             />
25     
26     </FrameLayout>
27     <LinearLayout 
28         android:layout_width="fill_parent"
29         android:layout_height="fill_parent"
30         android:orientation="vertical"
31         >
32         <TextView 
33             android:id="@+id/tv_title"
34             android:layout_width="fill_parent"
35             android:layout_height="0dip"
36             android:layout_weight="2"
37             android:gravity="center"
38             android:text="下拉刷新......"
39             />
40         <TextView 
41             android:id="@+id/tv_time"
42             android:layout_width="fill_parent"
43             android:layout_height="0dip"
44             android:gravity="center"
45             android:textColor="#808080"
46             android:layout_weight="1"
47             android:text="正在刷新中,时间为 2016-5-19"
48             />
49     </LinearLayout>
50 </LinearLayout>

3)底部视图

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:orientation="vertical" >
 6    <ProgressBar
 7        android:id="@+id/fpb"
 8        android:layout_width="wrap_content"
 9        android:layout_height="wrap_content"
10        android:layout_gravity="center" 
11        /> 
12    <TextView 
13        android:id="@+id/tv_load"
14        android:layout_width="wrap_content"
15        android:layout_height="wrap_content"
16        android:text="加载中......"
17        android:textColor="#ff0000"
18        android:layout_gravity="center"
19        android:textSize="18sp"
20        />
21 
22 </LinearLayout>

2、MainActivity

 1 package com.zhyui.zdylistview;
 2 
 3 import com.zhyui.zdylistview.ZyhListView.onRefreshListener;
 4 
 5 import android.os.Bundle;
 6 import android.os.Handler;
 7 import android.widget.ArrayAdapter;
 8 import android.app.Activity;
 9 
10 public class MainActivity extends Activity {
11     private ZyhListView zyh_lv;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         int length = 50;
17         String[] objects = new String[length];
18         for(int i=0; i<length; i++){
19             objects[i] = "abc";
20         }
21         zyh_lv = (ZyhListView) findViewById(R.id.zyh_lv);
22         
23         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
24                 android.R.id.text1, objects);
25         zyh_lv.setAdapter(adapter);
26         
27         zyh_lv.setOnRefreshListener(new onRefreshListener() {
28             @Override
29             public void refresh() {
30                 //向服务器请求数据
31                 new Handler(){
32                     public void handleMessage(android.os.Message msg) {
33                         zyh_lv.onRefreshComplete();
34                     };
35                 }.sendEmptyMessageDelayed(0, 3000);
36             }
37 
38             @Override
39             public void loadMore() {
40                 //向服务器请求数据
41                 new Handler(){
42                     public void handleMessage(android.os.Message msg) {
43                         zyh_lv.onRefreshComplete();
44                     };
45                 }.sendEmptyMessageDelayed(0, 3000);
46             }
47         });
48     }
49 
50 }

3、自定义ListView

  1 package com.zhyui.zdylistview;
  2 
  3 import java.text.SimpleDateFormat;
  4 import java.util.Date;
  5 
  6 import android.content.Context;
  7 import android.util.AttributeSet;
  8 import android.util.Log;
  9 import android.view.MotionEvent;
 10 import android.view.View;
 11 import android.view.animation.Animation;
 12 import android.view.animation.RotateAnimation;
 13 import android.widget.AbsListView;
 14 import android.widget.ImageView;
 15 import android.widget.ListView;
 16 import android.widget.ProgressBar;
 17 import android.widget.TextView;
 18 import android.widget.AbsListView.OnScrollListener;
 19 
 20 //==================================================
 21 //1、典型应用:onRefreshListener这个接口的使用,注意它的客户端和定义端
 22 //2、下拉刷新:注意它定义的3个状态值和使用
 23 //==================================================
 24 
 25 public class ZyhListView extends ListView implements OnScrollListener{
 26     private static final int STATE_PULL_REFRESH = 0;//下拉刷新状态
 27     private static final int STATE_RELESE_REFRESH = 1;//松开刷新状态
 28     private static final int STATE_REFRESHING = 2;//正在刷新中...
 29     private int mCurrentState = STATE_PULL_REFRESH;
 30     private int startY;
 31     private int endY;
 32     private int headViewHeight;
 33     private View headView;
 34     private ImageView iv_arr;
 35     private ProgressBar pb;
 36     private TextView tv_title;
 37     private TextView tv_time;
 38     private RotateAnimation rotate_up;
 39     private RotateAnimation rotate_down;
 40     public ZyhListView(Context context) {
 41         super(context);
 42         initHeadView();
 43         initFootView();
 44     }
 45     
 46     public ZyhListView(Context context, AttributeSet attrs) {
 47         super(context, attrs);
 48         initHeadView();
 49         initFootView();
 50     }
 51     
 52     public ZyhListView(Context context, AttributeSet attrs, int defStyle) {
 53         super(context, attrs, defStyle);
 54         initHeadView();
 55         initFootView();
 56     }
 57     
 58     private void initHeadView() {
 59         headView = View.inflate(getContext(), R.layout.head, null);
 60         this.addHeaderView(headView);
 61         
 62         iv_arr = (ImageView) headView.findViewById(R.id.iv_arr);
 63         pb = (ProgressBar) headView.findViewById(R.id.pb);
 64         tv_title = (TextView) headView.findViewById(R.id.tv_title);
 65         tv_time = (TextView) headView.findViewById(R.id.tv_time);
 66         tv_time.setText("最后刷新时间:" + getCurrentTime());
 67         //获取头部的高度
 68         //因为还没有对头部进行测量,所以如果不调用measure方法是得不到高度的
 69         headView.measure(0, 0);
 70         headViewHeight = headView.getMeasuredHeight();
 71         headView.setPadding(0, -headViewHeight, 0, 0);
 72         
 73         //初始化动画
 74         initAnim();
 75     }
 76     
 77     @Override
 78     public boolean onTouchEvent(MotionEvent ev) {
 79         switch(ev.getAction()){
 80             case MotionEvent.ACTION_DOWN:
 81                 startY = (int) ev.getRawY();
 82                 break;
 83             case MotionEvent.ACTION_MOVE:
 84                 if(startY == -1){
 85                     startY = (int) ev.getRawY();
 86                 }
 87                 endY = (int) ev.getRawY();
 88                 int des = endY - startY;
 89                 if(des > 0 && this.getFirstVisiblePosition() == 0){
 90                     Log.i("zyh", this.getVerticalScrollbarPosition()+"");
 91                     int padding = des - headViewHeight;
 92                     headView.setPadding(0, padding, 0, 0);
 93                     if(padding > 0 && mCurrentState == STATE_PULL_REFRESH){
 94                         mCurrentState = STATE_RELESE_REFRESH;//改变状态
 95                         refreshState();
 96                     }else if(padding < 0 && mCurrentState != STATE_PULL_REFRESH){
 97                         mCurrentState = STATE_PULL_REFRESH;
 98                         refreshState();
 99                     }
100                 }
101                 break;
102             case MotionEvent.ACTION_UP:
103                 startY = -1;
104                 if(mCurrentState == STATE_RELESE_REFRESH){
105                     mCurrentState = STATE_REFRESHING;
106                     refreshState();
107                     headView.setPadding(0, 0, 0, 0);
108                 }else if(mCurrentState == STATE_PULL_REFRESH){
109                     headView.setPadding(0, -headViewHeight, 0, 0);
110                 }
111                 break;
112         }
113         return super.onTouchEvent(ev);
114     }
115 
116     private void refreshState() {
117         switch(mCurrentState){
118             case STATE_PULL_REFRESH:
119                 tv_title.setText("下拉刷新");
120                 iv_arr.setVisibility(View.VISIBLE);
121                 pb.setVisibility(View.INVISIBLE);
122                 iv_arr.startAnimation(rotate_down);
123                 break;
124             case STATE_RELESE_REFRESH:
125                 tv_title.setText("松开刷新");
126                 iv_arr.setVisibility(View.VISIBLE);
127                 pb.setVisibility(View.INVISIBLE);
128                 iv_arr.startAnimation(rotate_up);
129                 break;
130             case STATE_REFRESHING:
131                 tv_title.setText("正在刷新");
132                 //清楚动画,否则iv_arr是不隐藏的
133                 iv_arr.clearAnimation();
134                 
135                 iv_arr.setVisibility(View.INVISIBLE);
136                 pb.setVisibility(View.VISIBLE);
137                 if(mOnRefreshListener != null){
138                     mOnRefreshListener.refresh();
139                 }
140                 break;
141         }
142     }
143     
144     private void initAnim() {
145         //下拉
146         rotate_up = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, 
147                 Animation.RELATIVE_TO_SELF, 0.5f);
148         rotate_up.setDuration(500);
149         rotate_up.setFillAfter(true);
150         
151         //松开时
152         rotate_down = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, 
153                 Animation.RELATIVE_TO_SELF, 0.5f);
154         rotate_down.setDuration(500);
155         rotate_down.setFillAfter(true);
156         
157     }
158     
159     public void onRefreshComplete(){
160         if(isLoadingMore){
161             footView.setPadding(0, -footViewHeight, 0, 0);
162             isLoadingMore = false;
163         }else{
164             mCurrentState = STATE_PULL_REFRESH;
165             tv_title.setText("下拉刷新");
166             iv_arr.setVisibility(View.VISIBLE);
167             pb.setVisibility(View.INVISIBLE);
168             headView.setPadding(0, -headViewHeight, 0, 0);
169             //设置时间
170             tv_time.setText("最后刷新时间:" + getCurrentTime());
171         }
172         
173     }
174     private String getCurrentTime() {
175         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
176         return sdf.format(new Date());
177     }
178     private onRefreshListener mOnRefreshListener;
179     private View footView;
180     private int footViewHeight;
181     public void setOnRefreshListener(onRefreshListener listener){
182         this.mOnRefreshListener = listener;
183     }
184     public interface onRefreshListener{
185         void refresh();
186         void loadMore();
187     }
188     
189     
190     private void initFootView() {
191         footView = View.inflate(getContext(), R.layout.foot, null);
192         this.addFooterView(footView);
193         footView.measure(0, 0);
194         footViewHeight = footView.getMeasuredHeight();
195         footView.setPadding(0, -footViewHeight, 0, 0);
196         //footView.setPadding(0, 0, 0, -footViewHeight);
197         this.setOnScrollListener(this);
198         
199     }
200 
201     private boolean isLoadingMore = false;
202     @Override
203     public void onScrollStateChanged(AbsListView view, int scrollState) {
204         //滑动后停下来,第二个是手指松开还在滑动
205         if(scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_FLING){
206             //滑到最后一个
207             if(getLastVisiblePosition() == getCount()-1 && !isLoadingMore){
208                 Log.i("zyh", "已经到最后了....");
209                 footView.setPadding(0, 0, 0, 0);
210                 setSelection(getCount()-1);
211                 isLoadingMore = true;
212                 
213                 if(mOnRefreshListener != null){
214                     mOnRefreshListener.loadMore();
215                 }
216             }
217         }
218     }
219 
220     @Override
221     public void onScroll(AbsListView view, int firstVisibleItem,
222             int visibleItemCount, int totalItemCount) {
223         // TODO Auto-generated method stub
224         
225     }
226 }

 

下拉刷新和上拉加载

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5521894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!