关于androidpulltorefresh

来源:互联网 发布:co网络用语是什么意思 编辑:程序博客网 时间:2024/06/05 09:04

android使用PullToRefresh实现上拉加载和下拉刷新效果

其实很早前就在博客园中也写过官方的下拉刷新控件SwipeRefreshLayout,但是这个控件仅仅支持下拉刷新,用起来还算可以。然而在我们实际开发应用中,很多地方都不止有下拉刷新,而且还有上拉加载的功能。当然,你完全可以自己写layout通过add的方式实现,但是既然有开源的力量让我们有幸能用到PullToRefresh这个资源,那为何不用呢?当然网上不止这个第三方包可实现,我这里就先用这个版本了。

先上一波运行图:

 

项目已同步至:https://github.com/nanchen2251/pullToRefreshDemo

 

简单使用详情:

1)studio可以直接在app的module设置中直接进行搜索,但是有-的必须添上,而不能用空格代替,为了更加了解这个东西,我还是推荐大家去这里看看,奉上网址:

https://github.com/chrisbanes/Android-PullToRefresh

 

所以去git上下载了后通过studio的导入Module功能,导入library,修改library的gradle文件和自己的项目一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apply plugin: 'com.android.library'
 
android {
    compileSdkVersion23
    buildToolsVersion"24.0.0"
 
    defaultConfig {
        minSdkVersion18
        targetSdkVersion23
    }
 
    buildTypes {
        release {
            minifyEnabledfalse
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.txt'
        }
    }
}

  

2)然后添加依赖,将app与其进行关联。

 

3)下面打开library可以看到很多的东西,这个不仅可以支持ListView,还可以支持GridView和ScollView等等,可谓相当全面,不过是否能和当前火热的RecyclerView一起使用楼主还没试过。

4)下面在我们的xml布局中布局,这里我用了自定义控件的属性,所以添加了一个xmlns:app="http://schemas.android.com/apk/res-auto"

复制代码
 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3     xmlns:android="http://schemas.android.com/apk/res/android" 4     xmlns:tools="http://schemas.android.com/tools" 5     xmlns:app="http://schemas.android.com/apk/res-auto" 6     android:layout_width="match_parent" 7     android:layout_height="match_parent" 8     tools:context="com.example.nanchen.pulltorefreshdemo.MainActivity"> 9 10     <com.handmark.pulltorefresh.library.PullToRefreshListView11         android:layout_width="match_parent"12         android:layout_height="match_parent"13         android:id="@+id/main_pull_refresh_lv"14         app:ptrAnimationStyle="flip"15         app:ptrHeaderBackground="@android:color/transparent"16         app:ptrHeaderTextColor="#919191"/>17 18 </RelativeLayout>
复制代码

 

5)由于我们这里使用的是它的ListView,所以我们需要一个java  Bean  和一个Item的layout进行自定义布局。

复制代码
 1 package com.example.nanchen.pulltorefreshdemo; 2  3 /** 4  * Created by 南尘 on 16-7-20. 5  */ 6 public class Music { 7  8     private String title; 9     private String singer;10 11     public Music() {12     }13 14     public Music(String title, String singer) {15         this.title = title;16         this.singer = singer;17     }18 19     public String getTitle() {20         return title;21     }22 23     public void setTitle(String title) {24         this.title = title;25     }26 27     public String getSinger() {28         return singer;29     }30 31     public void setSinger(String singer) {32         this.singer = singer;33     }34 }
复制代码

 

还有list_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_title"
        android:text="歌曲1"
        android:textSize="20sp"
        android:layout_alignParentLeft="true"/>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_singer"
        android:textSize="20sp"
        android:text="歌手1"
        android:layout_alignParentRight="true"/>
 
 
</RelativeLayout>

  

6)使用非常简单,在Activity中,代码注释已经比较全面了,这里模拟了异步下载数据任务,并且重写了Adaper,至于其中为什么用静态内部类,这个好处很多,大家可以百度科普。

复制代码
  1 package com.example.nanchen.pulltorefreshdemo;  2   3 import android.content.Context;  4 import android.os.AsyncTask;  5 import android.os.Bundle;  6 import android.support.v7.app.AppCompatActivity;  7 import android.view.LayoutInflater;  8 import android.view.View;  9 import android.view.ViewGroup; 10 import android.widget.BaseAdapter; 11 import android.widget.ListView; 12 import android.widget.TextView; 13  14 import com.handmark.pulltorefresh.library.ILoadingLayout; 15 import com.handmark.pulltorefresh.library.PullToRefreshBase; 16 import com.handmark.pulltorefresh.library.PullToRefreshListView; 17  18 import java.util.ArrayList; 19 import java.util.List; 20  21 public class MainActivity extends AppCompatActivity { 22  23     private PullToRefreshListView refresh_lv; 24     private List<Music> list; 25     private DataAdapter adapter; 26  27     @Override 28     protected void onCreate(Bundle savedInstanceState) { 29         super.onCreate(savedInstanceState); 30         setContentView(R.layout.activity_main); 31  32         refresh_lv = (PullToRefreshListView) findViewById(R.id.main_pull_refresh_lv); 33         list = new ArrayList<>(); 34  35         //设置可上拉刷新和下拉刷新 36         refresh_lv.setMode(PullToRefreshBase.Mode.BOTH); 37  38         //设置刷新时显示的文本 39         ILoadingLayout startLayout = refresh_lv.getLoadingLayoutProxy(true,false); 40         startLayout.setPullLabel("正在下拉刷新..."); 41         startLayout.setRefreshingLabel("正在玩命加载中..."); 42         startLayout.setReleaseLabel("放开以刷新"); 43  44  45         ILoadingLayout endLayout = refresh_lv.getLoadingLayoutProxy(false,true); 46         endLayout.setPullLabel("正在上拉刷新..."); 47         endLayout.setRefreshingLabel("正在玩命加载中..."); 48         endLayout.setReleaseLabel("放开以刷新"); 49  50  51         refresh_lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { 52             @Override 53             public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { 54                 new LoadDataAsyncTask(MainActivity.this).execute(); 55             } 56  57             @Override 58             public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { 59                 new LoadDataAsyncTask(MainActivity.this).execute(); 60             } 61         }); 62  63  64         loadData(); 65         adapter = new DataAdapter(this,list); 66         refresh_lv.setAdapter(adapter); 67     } 68  69     private int count = 1; 70     private void loadData(){ 71         for (int i = 0; i < 10; i++) { 72             list.add(new Music("歌曲"+count,"歌手"+count)); 73             count++; 74         } 75     } 76  77     /** 78      * 异步下载任务 79      */ 80     private static class LoadDataAsyncTask extends AsyncTask<Void,Void,String>{ 81  82         private MainActivity mainActivity; 83  84         public LoadDataAsyncTask(MainActivity mainActivity) { 85             this.mainActivity = mainActivity; 86         } 87  88         @Override 89         protected String doInBackground(Void... params) { 90             try { 91                 Thread.sleep(2000); 92                 mainActivity.loadData(); 93                 return "seccess"; 94             } catch (InterruptedException e) { 95                 e.printStackTrace(); 96             } 97             return null; 98         } 99 100         /**101          * 完成时的方法102          */103         @Override104         protected void onPostExecute(String s) {105             super.onPostExecute(s);106             if (s.equals("seccess")){107                 mainActivity.adapter.notifyDataSetChanged();108                 mainActivity.refresh_lv.onRefreshComplete();//刷新完成109             }110         }111     }112 113     /**114      * 自定义适配器115      */116     private static class DataAdapter extends BaseAdapter{117 118         private Context context;119         private List<Music> list;120 121         public DataAdapter(Context context, List<Music> list) {122             this.context = context;123             this.list = list;124         }125 126         @Override127         public int getCount() {128             if (list != null){129                 return list.size();130             }131             return 0;132         }133 134         @Override135         public Object getItem(int position) {136             return list.get(position);137         }138 139         @Override140         public long getItemId(int position) {141             return position;142         }143 144         @Override145         public View getView(int position, View convertView, ViewGroup parent) {146             ViewHolder vh;147             if (convertView == null){148                 convertView = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);149                 vh = new ViewHolder();150                 vh.tv_title = (TextView) convertView.findViewById(R.id.item_title);151                 vh.tv_singer = (TextView) convertView.findViewById(R.id.item_singer);152                 convertView.setTag(vh);153             }else{154                 vh = (ViewHolder) convertView.getTag();155             }156             Music music = (Music) getItem(position);157             vh.tv_title.setText(music.getTitle());158             vh.tv_singer.setText(music.getSinger());159             return convertView;160         }161 162         class ViewHolder{163             TextView tv_title;164             TextView tv_singer;165         }166     }167 }
0 0
原创粉丝点击