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

6.下拉刷新

时间:2015-12-12 23:14:39      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

下拉刷新
下拉刷新-------
1.addHeaderView必须在setAdapter之前调用
2.将paddingTop设置一个headerView高度的负值去隐藏它
getHeight()和getMeasuredHeight()的区别:
getMeasuredHeight():获取测量完的高度,只要在onMeasure方法执行完,就可以用
                   它获取到宽高,在自定义控件内部多使用这个
使用view.measure(0,0)方法可以主动通知系统去测量,然后就
可以直接使用它获取宽高
getHeight():必须在onLayout方法执行完后,才能获得宽高
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
headerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int headerViewHeight = headerView.getHeight();
//直接可以获取宽高
}
});
3.setSelection(position);将对应位置的item放置到屏幕顶端

RefreshListView
  1. public class RefreshListView extends ListView implements OnScrollListener{
  2. private View headerView;//headerView
  3. private ImageView iv_arrow;
  4. private ProgressBar pb_rotate;
  5. private TextView tv_state,tv_time;
  6. private View footerView;
  7. private int footerViewHeight;
  8. private int headerViewHeight;//headerView高
  9. private int downY;//按下时y坐标
  10. private final int PULL_REFRESH = 0;//下拉刷新的状态
  11. private final int RELEASE_REFRESH = 1;//松开刷新的状态
  12. private final int REFRESHING = 2;//正在刷新的状态
  13. private int currentState = PULL_REFRESH;
  14. private RotateAnimation upAnimation,downAnimation;
  15. private boolean isLoadingMore = false;//当前是否正在处于加载更多
  16. public RefreshListView(Context context) {
  17. super(context);
  18. init();
  19. }
  20. public RefreshListView(Context context, AttributeSet attrs) {
  21. super(context, attrs);
  22. init();
  23. }
  24. private void init(){
  25. setOnScrollListener(this);
  26. initHeaderView();
  27. initRotateAnimation();
  28. initFooterView();
  29. }
  30. /**
  31. * 初始化headerView
  32. */
  33. private void initHeaderView() {
  34. headerView = View.inflate(getContext(), R.layout.layout_header, null);
  35. iv_arrow = (ImageView) headerView.findViewById(R.id.iv_arrow);
  36. pb_rotate = (ProgressBar) headerView.findViewById(R.id.pb_rotate);
  37. tv_state = (TextView) headerView.findViewById(R.id.tv_state);
  38. tv_time = (TextView) headerView.findViewById(R.id.tv_time);
  39. headerView.measure(0, 0);//主动通知系统去测量该view;
  40. headerViewHeight = headerView.getMeasuredHeight();
  41. headerView.setPadding(0, -headerViewHeight, 0, 0);
  42. addHeaderView(headerView);
  43. }
  44. /**
  45. * 初始化旋转动画
  46. */
  47. private void initRotateAnimation() {
  48. upAnimation = new RotateAnimation(0, -180,
  49. RotateAnimation.RELATIVE_TO_SELF, 0.5f,
  50. RotateAnimation.RELATIVE_TO_SELF, 0.5f);
  51. upAnimation.setDuration(300);
  52. upAnimation.setFillAfter(true);
  53. downAnimation = new RotateAnimation(-180, -360,
  54. RotateAnimation.RELATIVE_TO_SELF, 0.5f,
  55. RotateAnimation.RELATIVE_TO_SELF, 0.5f);
  56. downAnimation.setDuration(300);
  57. downAnimation.setFillAfter(true);
  58. }
  59. private void initFooterView() {
  60. footerView = View.inflate(getContext(), R.layout.layout_footer, null);
  61. footerView.measure(0, 0);//主动通知系统去测量该view;
  62. footerViewHeight = footerView.getMeasuredHeight();
  63. footerView.setPadding(0, -footerViewHeight, 0, 0);
  64. addFooterView(footerView);
  65. }
  66. @Override
  67. public boolean onTouchEvent(MotionEvent ev) {
  68. switch (ev.getAction()) {
  69. case MotionEvent.ACTION_DOWN:
  70. downY = (int) ev.getY();
  71. break;
  72. case MotionEvent.ACTION_MOVE:
  73. if(currentState==REFRESHING){
  74. break;
  75. }
  76. int deltaY = (int) (ev.getY() - downY);
  77. int paddingTop = -headerViewHeight + deltaY;
  78. if(paddingTop>-headerViewHeight && getFirstVisiblePosition()==0){
  79. headerView.setPadding(0, paddingTop, 0, 0);
  80. // Log.e("RefreshListView", "paddingTop: "+paddingTop);
  81. if(paddingTop>=0 && currentState==PULL_REFRESH){
  82. //从下拉刷新进入松开刷新状态
  83. currentState = RELEASE_REFRESH;
  84. refreshHeaderView();
  85. }else if (paddingTop<0 && currentState==RELEASE_REFRESH) {
  86. //进入下拉刷新状态
  87. currentState = PULL_REFRESH;
  88. refreshHeaderView();
  89. }
  90. return true;//拦截TouchMove,不让listview处理该次move事件,会造成listview无法滑动
  91. }
  92. break;
  93. case MotionEvent.ACTION_UP:
  94. if(currentState==PULL_REFRESH){
  95. //隐藏headerView
  96. headerView.setPadding(0, -headerViewHeight, 0, 0);
  97. }else if (currentState==RELEASE_REFRESH) {
  98. headerView.setPadding(0, 0, 0, 0);
  99. currentState = REFRESHING;
  100. refreshHeaderView();
  101. if(listener!=null){
  102. listener.onPullRefresh();
  103. }
  104. }
  105. break;
  106. }
  107. return super.onTouchEvent(ev);
  108. }
  109. /**
  110. * 根据currentState来更新headerView
  111. */
  112. private void refreshHeaderView(){
  113. switch (currentState) {
  114. case PULL_REFRESH:
  115. tv_state.setText("下拉刷新");
  116. iv_arrow.startAnimation(downAnimation);
  117. break;
  118. case RELEASE_REFRESH:
  119. tv_state.setText("松开刷新");
  120. iv_arrow.startAnimation(upAnimation);
  121. break;
  122. case REFRESHING:
  123. iv_arrow.clearAnimation();//因为向上的旋转动画有可能没有执行完
  124. iv_arrow.setVisibility(View.INVISIBLE);
  125. pb_rotate.setVisibility(View.VISIBLE);
  126. tv_state.setText("正在刷新...");
  127. break;
  128. }
  129. }
  130. /**
  131. * 完成刷新操作,重置状态,在你获取完数据并更新完adater之后,去在UI线程中调用该方法
  132. */
  133. public void completeRefresh(){
  134. if(isLoadingMore){
  135. //重置footerView状态
  136. footerView.setPadding(0, -footerViewHeight, 0, 0);
  137. isLoadingMore = false;
  138. }else {
  139. //重置headerView状态
  140. headerView.setPadding(0, -headerViewHeight, 0, 0);
  141. currentState = PULL_REFRESH;
  142. pb_rotate.setVisibility(View.INVISIBLE);
  143. iv_arrow.setVisibility(View.VISIBLE);
  144. tv_state.setText("下拉刷新");
  145. tv_time.setText("最后刷新:"+getCurrentTime());
  146. }
  147. }
  148. /**
  149. * 获取当前系统时间,并格式化
  150. * @return
  151. */
  152. private String getCurrentTime(){
  153. SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
  154. return format.format(new Date());
  155. }
  156. private OnRefreshListener listener;
  157. public void setOnRefreshListener(OnRefreshListener listener){
  158. this.listener = listener;
  159. }
  160. public interface OnRefreshListener{
  161. void onPullRefresh();
  162. void onLoadingMore();
  163. }
  164. /**
  165. * SCROLL_STATE_IDLE:闲置状态,就是手指松开
  166. * SCROLL_STATE_TOUCH_SCROLL:手指触摸滑动,就是按着来滑动
  167. * SCROLL_STATE_FLING:快速滑动后松开
  168. */
  169. @Override
  170. public void onScrollStateChanged(AbsListView view, int scrollState) {
  171. if(scrollState==OnScrollListener.SCROLL_STATE_IDLE
  172. && getLastVisiblePosition()==(getCount()-1) &&!isLoadingMore){
  173. isLoadingMore = true;
  174. footerView.setPadding(0, 0, 0, 0);//显示出footerView
  175. setSelection(getCount());//让listview最后一条显示出来
  176. if(listener!=null){
  177. listener.onLoadingMore();
  178. }
  179. }
  180. }
  181. @Override
  182. public void onScroll(AbsListView view, int firstVisibleItem,
  183. int visibleItemCount, int totalItemCount) {
  184. }
  185. }
MainActivity
  1. public class MainActivity extends Activity {
  2. private RefreshListView refreshListView;
  3. private ArrayList<String> list = new ArrayList<String>();
  4. private MyAdapter adapter;
  5. private Handler handler = new Handler(){
  6. public void handleMessage(android.os.Message msg) {
  7. //更新UI
  8. adapter.notifyDataSetChanged();
  9. refreshListView.completeRefresh();
  10. };
  11. };
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. initView();
  16. initData();
  17. }
  18. private void initView() {
  19. requestWindowFeature(Window.FEATURE_NO_TITLE);
  20. setContentView(R.layout.activity_main);
  21. refreshListView = (RefreshListView) findViewById(R.id.refreshListView);
  22. }
  23. private void initData() {
  24. for (int i = 0; i < 15; i++) {
  25. list.add("listview原来的数据 - "+i);
  26. }
  27. // final View headerView = View.inflate(this, R.layout.layout_header, null);
  28. //第一种方法
  29. // headerView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  30. // @Override
  31. // public void onGlobalLayout() {
  32. // headerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  33. // int headerViewHeight = headerView.getHeight();
  34. //
  35. //
  36. // Log.e("MainActivity", "headerViewHeight: "+headerViewHeight);
  37. // headerView.setPadding(0, -headerViewHeight, 0, 0);
  38. // refreshListView.addHeaderView(headerView);//
  39. // }
  40. // });
  41. //第二种方法
  42. // headerView.measure(0, 0);//主动通知系统去测量
  43. // int headerViewHeight = headerView.getMeasuredHeight();
  44. // Log.e("MainActivity", "headerViewHeight: "+headerViewHeight);
  45. // headerView.setPadding(0, -headerViewHeight, 0, 0);
  46. // refreshListView.addHeaderView(headerView);//
  47. adapter = new MyAdapter();
  48. refreshListView.setAdapter(adapter);
  49. refreshListView.setOnRefreshListener(new OnRefreshListener() {
  50. @Override
  51. public void onPullRefresh() {
  52. //需要联网请求服务器的数据,然后更新UI
  53. requestDataFromServer(false);
  54. }
  55. @Override
  56. public void onLoadingMore() {
  57. requestDataFromServer(true);
  58. }
  59. });
  60. }
  61. /**
  62. * 模拟向服务器请求数据
  63. */
  64. private void requestDataFromServer(final boolean isLoadingMore){
  65. new Thread(){
  66. public void run() {
  67. SystemClock.sleep(3000);//模拟请求服务器的一个时间长度
  68. if(isLoadingMore){
  69. list.add("加载更多的数据-1");
  70. list.add("加载更多的数据-2");
  71. list.add("加载更多的数据-3");
  72. }else {
  73. list.add(0, "下拉刷新的数据");
  74. }
  75. //在UI线程更新UI
  76. handler.sendEmptyMessage(0);
  77. };
  78. }.start();
  79. }
  80. class MyAdapter extends BaseAdapter{
  81. @Override
  82. public int getCount() {
  83. return list.size();
  84. }
  85. @Override
  86. public Object getItem(int position) {
  87. return null;
  88. }
  89. @Override
  90. public long getItemId(int position) {
  91. return 0;
  92. }
  93. @Override
  94. public View getView(int position, View convertView, ViewGroup parent) {
  95. TextView textView = new TextView(MainActivity.this);
  96. textView.setPadding(20, 20, 20, 20);
  97. textView.setTextSize(18);
  98. textView.setText(list.get(position));
  99. return textView;
  100. }
  101. }
  102. }










6.下拉刷新

标签:

原文地址:http://www.cnblogs.com/liuyu0529/p/5041934.html

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