1 package cn.bitlove.waterfalllistview.widget;
2
3 import cn.bitlove.waterfalllistview.R;
4 import android.content.Context;
5 import android.util.AttributeSet;
6 import android.view.LayoutInflater;
7 import android.view.View;
8 import android.view.ViewGroup;
9 import android.widget.AbsListView;
10 import android.widget.FrameLayout;
11 import android.widget.AbsListView.OnScrollListener;
12 import android.widget.ListView;
13
14 /**
15 * 瀑布流Listview
16 * */
17 public class WaterfallListview extends ListView implements OnScrollListener {
18
19 final private String tag="WaterfallListView";
20 private Context mContext;
21 private LayoutInflater mInflater;
22 final int REFRESH_IDEL=0; //空闲中
23 final int REFRESH_PREPARE=1; //准备刷新
24 final int REFRESH_ING=2; //刷新中
25 final int REFRESH_End=3; //刷新完成
26 private int mRefreshState = REFRESH_IDEL; //刷新状态
27 private int mScrollState; //滚动状态
28
29 private FrameLayout mFooterLayout; //ListView 的footer
30 private View mListFoot; //底部刷新区域
31
32 private IOnRefresh mRefreshListener = null; //刷新监听器
33 public WaterfallListview(Context context) {
34 super(context);
35 }
36 public WaterfallListview(Context context, AttributeSet attrs) {
37 super(context,attrs);
38 init();
39 }
40 @Override
41 public void onScrollStateChanged(AbsListView view, int scrollState) {
42 mScrollState = scrollState;
43 }
44
45 @Override
46 public void onScroll(AbsListView view, int firstVisibleItem,
47 int visibleItemCount, int totalItemCount) {
48 if(totalItemCount==0 || mScrollState==SCROLL_STATE_IDLE){
49 return;
50 }
51 //最后一个item
52 int lastItemCount = firstVisibleItem+visibleItemCount;
53 if(lastItemCount==totalItemCount && mRefreshState==REFRESH_IDEL){
54 prepareRefresh();
55 doRefresh();
56 }
57 }
58
59 private void init(){
60 mCOntext= getContext();
61 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
62 initFooter();
63 setOnScrollListener(this);
64 }
65 /**
66 * 设置刷新器
67 * */
68 public void setRefreshListener(IOnRefresh refresher){
69 mRefreshListener = refresher;
70 }
71
72 /**
73 * 初始化底部刷新内容
74 * */
75 private void initFooter(){
76 mListFoot = mInflater.inflate(R.layout.foot_waterfall_listview, null);
77 mFooterLayout = new FrameLayout(mContext);
78 addFooterView(mFooterLayout);
79 }
80 /**
81 * 准备刷新
82 * */
83 private void prepareRefresh(){
84 //Log.i(tag,"prepareRefresh");
85 mRefreshState = REFRESH_PREPARE;
86 ViewGroup vg = (ViewGroup) mListFoot.getParent();
87 if(vg!=null){
88 vg.removeView(mListFoot);
89 }
90
91 mFooterLayout.addView(mListFoot);
92
93 if(mRefreshListener!=null){
94 mRefreshListener.beforeRefresh();
95 }
96 }
97 /**
98 * 执行刷新
99 * */
100 private void doRefresh(){
101 //Log.i(tag,"doRefresh");
102 mRefreshState=REFRESH_ING;
103
104 if(mRefreshListener!=null){
105 mRefreshListener.doRefresh();
106 }
107
108 }
109 /**
110 * 完成刷新
111 * */
112 public void completeRefresh(){
113 mRefreshState = REFRESH_IDEL;
114 mFooterLayout.removeAllViews();
115 }
116
117 public interface IOnRefresh{
118 public void beforeRefresh();
119 /**
120 * 执行完doRefresh操作后,必须调用completeRefresh方法来清理刷新状态
121 * */
122 public void doRefresh();
123 }
124 }