android最新上拉加载,下拉刷新

来源:互联网 发布:pic单片机优点 编辑:程序博客网 时间:2024/04/29 04:37

     开发项目过程中基本都会用到listView的下拉刷新和上滑加载更多,之前大家最常用的应该是pull to refresh或它的变种版吧,google官方在最新的Android.support.v4包中增加了一个新类SwipeRefreshLayout,地址 这个类的作用就是提供官方的下拉刷新,并且效果相当不错,而上拉加载更多则用我们自定义的listview,也是相当简单。

下拉刷新


简单的介绍下:

  首先它是一个viewgroup,但是它只允许有一个子控件,子控件能是任何view,使用的时候,所在类实现OnRefreshListener接口,在onRefresh()方法中实现所要完成的任务,

findviewbyid得到SwipeRefreshLayout后,显示刷新动画用SwipeRefreshLayout.setRefreshing(true);取消刷新动画用SwipeRefreshLayout.setRefreshing(false);相当简单。

另外有一些其他的常用方法比如判断时候正在显示刷新动画,用SwipeRefreshLayout.isShown()。

布局:

[html] view plain copy
  1. <android.support.v4.widget.SwipeRefreshLayout  
  2.         android:id="@+id/swip_index"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="match_parent" >  
  5.   
  6.         <com.example.listviewloadmore.LoadMoreListView  
  7.             android:id="@+id/listView"  
  8.             android:layout_width="wrap_content"  
  9.             android:layout_height="wrap_content"  
  10.             android:text="@string/hello_world" >  
  11.         </com.example.listviewloadmore.LoadMoreListView>  
  12.     </android.support.v4.widget.SwipeRefreshLayout>  


上拉加载更多

思路:监听滑动事件并判断是否到达底部,到达底部的时候,回调我们定义好的一个方法,从而达到上拉加载更多的目的。
但是具体怎么监听和回调呢,我们不妨看看Button控件的点击事件的执行流程,Button继承textView,textView继承View
1.实现OnClickListener接口,重写onClick方法
2.设置Button.setOnClickListener(this)或者new一个接口也一样的。

查看View源码,我们可得知

View类中有OnClickOistener接口,setOnClickListener方法


那么我们在我们自己的listview中要实现的步骤狠清楚了
1.myListView继承listView
2.实现onLoadMore接口
3.在类中声明onLoadMore接口对象
4.设置点击方法setOnLoadMore
5.在需要的地方调用onLoadMore.loadMore方法。
下面附上MyListView源码:
[java] view plain copy
  1. package com.example.listviewloadmore;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.util.AttributeSet;  
  6. import android.util.Log;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.widget.AbsListView;  
  10. import android.widget.AbsListView.OnScrollListener;  
  11. import android.widget.ListView;  
  12.   
  13. public class LoadMoreListView extends ListView implements OnScrollListener{  
  14.     private View footer;  
  15.       
  16.     private int totalItem;  
  17.     private int lastItem;  
  18.       
  19.     private boolean isLoading;  
  20.       
  21.     private OnLoadMore onLoadMore;  
  22.       
  23.     private LayoutInflater inflater;  
  24.       
  25.     public LoadMoreListView(Context context) {  
  26.         super(context);  
  27.         init(context);  
  28.     }  
  29.   
  30.     public LoadMoreListView(Context context, AttributeSet attrs) {  
  31.         super(context, attrs);  
  32.         init(context);  
  33.     }  
  34.   
  35.     public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {  
  36.         super(context, attrs, defStyle);  
  37.         init(context);  
  38.     }  
  39.   
  40.     @SuppressLint("InflateParams")  
  41.     private void init(Context context) {  
  42.         inflater = LayoutInflater.from(context);  
  43.         footer = inflater.inflate(R.layout.load_more_footer,null ,false);  
  44.         footer.setVisibility(View.GONE);  
  45.         this.addFooterView(footer);  
  46.         this.setOnScrollListener(this);  
  47.     }  
  48.   
  49.     @Override  
  50.     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
  51.         this.lastItem = firstVisibleItem+visibleItemCount;  
  52.         this.totalItem = totalItemCount;  
  53.     }  
  54.   
  55.     @Override  
  56.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  57.         if(this.totalItem == lastItem&&scrollState == SCROLL_STATE_IDLE){  
  58.             Log.v("isLoading""yes");  
  59.             if(!isLoading){  
  60.                 isLoading=true;  
  61.                 footer.setVisibility(View.VISIBLE);  
  62.                 onLoadMore.loadMore();  
  63.             }  
  64.         }  
  65.     }  
  66.     public void setLoadMoreListen(OnLoadMore onLoadMore){  
  67.         this.onLoadMore = onLoadMore;  
  68.     }  
  69.     /** 
  70.      * 加载完成调用此方法 
  71.      */  
  72.     public void onLoadComplete(){  
  73.         footer.setVisibility(View.GONE);  
  74.         isLoading = false;  
  75.           
  76.     }  
  77.       
  78.     public interface OnLoadMore{  
  79.         public void loadMore();  
  80.     }  
  81.   
  82. }  


代码不是很复杂,认真看一定能看明白。
底部正在加载布局代码
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/load_more_footer"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true" >  
  11.   
  12.         <ProgressBar  
  13.             android:id="@+id/load_more_progressBar"  
  14.             android:layout_width="36dp"  
  15.             android:layout_height="36dp"  
  16.             android:padding="3dp"  
  17.             android:visibility="visible" />  
  18.   
  19.         <TextView  
  20.             android:id="@+id/no_more_textView"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="36dp"  
  23.             android:gravity="center"  
  24.             android:text="正在加载"  
  25.             android:textColor="#666666"  
  26.             android:textSize="16sp"  
  27.             android:visibility="visible" />  
  28.     </LinearLayout>  
  29.   
  30. </RelativeLayout>  


基本到这上拉加载更多就结束了。
下面就是把下拉上拉结合起来用了
布局文件main.xml
[html] view plain copy
  1. <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context="com.example.listviewloadmore.MainActivity" >  
  10.   
  11.     <android.support.v4.widget.SwipeRefreshLayout  
  12.         android:id="@+id/swip_index"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="match_parent" >  
  15.   
  16.         <com.example.listviewloadmore.LoadMoreListView  
  17.             android:id="@+id/listView"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:text="@string/hello_world" >  
  21.         </com.example.listviewloadmore.LoadMoreListView>  
  22.     </android.support.v4.widget.SwipeRefreshLayout>  
  23.   
  24. </RelativeLayout>  


MainActivity代码

[java] view plain copy
  1. package com.example.listviewloadmore;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.os.Handler;  
  8. import android.support.v4.widget.SwipeRefreshLayout;  
  9. import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.Toast;  
  12.   
  13. import com.example.listviewloadmore.LoadMoreListView.OnLoadMore;  
  14.   
  15. public class MainActivity extends Activity implements OnRefreshListener, OnLoadMore {  
  16.     private LoadMoreListView listView;  
  17.     private SwipeRefreshLayout swip;  
  18.     private int page = 1;  
  19.     ArrayList<String> data1 = new ArrayList<String>();  
  20.     private ArrayAdapter<String> adapter;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.         initView();  
  27.         initData(1);  
  28.     }  
  29.   
  30.   
  31.     private void initView() {  
  32.         listView = (LoadMoreListView) findViewById(R.id.listView);  
  33.         listView.setLoadMoreListen(this);  
  34.         swip = (SwipeRefreshLayout) findViewById(R.id.swip_index);  
  35.         swip.setOnRefreshListener(this);  
  36.         swip.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light,  
  37.                 android.R.color.holo_green_light);  
  38.         adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data1);  
  39.     }  
  40.     /** 
  41.      * 加载数据 
  42.      * @param page2 页数 
  43.      */  
  44.     private void initData(int page2) {  
  45.         // TODO Auto-generated method stub  
  46.         if(page2==1){  
  47.             for (int i = 0; i < 10; i++) {  
  48.                 data1.add((i+1)+"");  
  49.                 listView.setAdapter(adapter);  
  50.             }  
  51.         }else{  
  52.             data1.add("新加的第"+(9+page2)+"个");  
  53.         }  
  54.     }  
  55.     // 下拉刷新的方法  
  56.     @Override  
  57.     public void onRefresh() {  
  58.         Toast.makeText(this"开始刷新啦", Toast.LENGTH_SHORT).show();  
  59.         new Handler().postDelayed(new Runnable() {  
  60.             @Override  
  61.             public void run() {  
  62.                 if(swip.isShown()){  
  63.                     swip.setRefreshing(false);  
  64.                 }  
  65.                 Toast.makeText(MainActivity.this"刷新完成了", Toast.LENGTH_SHORT).show();  
  66.             }  
  67.         }, 3000);  
  68.     }  
  69.   
  70.     // 加载更多的方法  
  71.     @Override  
  72.     public void loadMore() {  
  73.         page++;  
  74.         new Handler().postDelayed(new Runnable() {  
  75.             @Override  
  76.             public void run() {  
  77.                 initData(page);  
  78.                 listView.onLoadComplete();  
  79.                 Toast.makeText(MainActivity.this"加载完成", Toast.LENGTH_SHORT).show();  
  80.                 adapter.notifyDataSetChanged();  
  81.             }  
  82.         }, 3000);  
  83.     }  
  84.   
  85. }  



上一下效果图:



Demo下载地址http://download.csdn.net/detail/wo0123456789wo/9771541
0 0
原创粉丝点击