AndroidStudio-PullTorefresh使用

来源:互联网 发布:手机端java编程软件 编辑:程序博客网 时间:2024/06/05 10:41

之前一直在Eclipse上使用,今天尝试了在androidstudio上使用该框架(下载地址:https://github.com/chrisbanes/Android-PullToRefresh)

虽然网上也有很多介绍如何使用pulltorefresh的文章,但我还是写下来记录一下. 
1.下载压缩文件,解压,会看到如下几个文件: 
这里写图片描述 
这里我暂时只使用了library中的文件。

2。导入Androidstudio 
(1)修改根目录下的settings.gradle文件:include ‘:app’ —》include ‘:app’,’:lib:pull’ 
这里写图片描述

(2)修噶app目录下的build.gradle文件: 
dependencies { 
compile fileTree(dir: ‘libs’, include: [‘*.jar’]) 
}

—-> 
dependencies { 
compile fileTree(dir: ‘libs’, include: [‘*.jar’]) 
// Library 
compile project(‘:lib:pull’) 
}

这里写图片描述

3.源码库位置:在根目录下新建一个文件夹lib(名字可自己定义),将解压之后的library文件夹放到lib下文件夹中,然后在library(记住,是源码所在的文件夹)新建文件build.gradle,添加如下内容:

apply plugin: ‘android-library’ 
android { 
compileSdkVersion 19 
buildToolsVersion “20.0.0” 
sourceSets { 
main { 
manifest.srcFile ‘AndroidManifest.xml’ 
java.srcDirs = [‘src’] 
resources.srcDirs = [‘src’] 
aidl.srcDirs = [‘aidl’] 
renderscript.srcDirs = [‘src’] 
res.srcDirs = [‘res’] 
assets.srcDirs = [‘assets’] 


}

这里写图片描述 
此处 compileSdkVersion 19 和 buildToolsVersion “20.0.0”记得和其他的build文件保持一致。

项目结构如下 
这里写图片描述


以上编译即可。

实例:PullToRefreshDemo
运行效果:

视图结构



代码清单:
布局文件:activity_main.xml
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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=".MainActivity" >  
  10.    
  11.     <com.handmark.pulltorefresh.library.PullToRefreshListView  
  12.         android:id="@+id/pull_to_refresh_listview"  
  13.         android:layout_height="fill_parent"  
  14.         android:layout_width="fill_parent" />  
  15.   
  16. </RelativeLayout>  

Java源代码文件:MainActivity.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.rainsong.pulltorefreshdemo;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.LinkedList;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.AsyncTask;  
  8. import android.os.Bundle;  
  9. import android.text.format.DateUtils;  
  10. import android.view.Menu;  
  11. import android.widget.ArrayAdapter;  
  12. import android.widget.ListView;  
  13.   
  14. import com.handmark.pulltorefresh.library.PullToRefreshBase;  
  15. import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;  
  16. import com.handmark.pulltorefresh.library.PullToRefreshListView;  
  17.   
  18. public class MainActivity extends Activity {  
  19.     private PullToRefreshListView mPullToRefreshListView;  
  20.     private LinkedList<String> mListItems;  
  21.     private ArrayAdapter<String> mAdapter;  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.           
  28.         // Set a listener to be invoked when the list should be refreshed.  
  29.         mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);  
  30.         mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {  
  31.             @Override  
  32.             public void onRefresh(PullToRefreshBase<ListView> refreshView) {  
  33.                 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),  
  34.                         DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  
  35.                   
  36.                 // Update the LastUpdatedLabel  
  37.                 refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  
  38.                   
  39.                 // Do work to refresh the list here.  
  40.                 new GetDataTask().execute();  
  41.             }  
  42.         });  
  43.           
  44.         ListView actualListView = mPullToRefreshListView.getRefreshableView();  
  45.           
  46.         mListItems = new LinkedList<String>();  
  47.         mListItems.addAll(Arrays.asList(mStrings));  
  48.           
  49.         mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);  
  50.         actualListView.setAdapter(mAdapter);  
  51.     }  
  52.       
  53.     private class GetDataTask extends AsyncTask<Void, Void, String[]> {  
  54.   
  55.         @Override  
  56.         protected String[] doInBackground(Void... params) {  
  57.             // Simulates a background job.  
  58.             try {  
  59.                 Thread.sleep(4000);  
  60.             } catch (InterruptedException e) {  
  61.             }  
  62.             return mStrings;  
  63.         }  
  64.         @Override  
  65.         protected void onPostExecute(String[] result) {  
  66.             mListItems.addFirst("Added after refresh...");  
  67.             mAdapter.notifyDataSetChanged();  
  68.               
  69.             // Call onRefreshComplete when the list has been refreshed.  
  70.             mPullToRefreshListView.onRefreshComplete();  
  71.             super.onPostExecute(result);  
  72.         }  
  73.     }  
  74.   
  75.     @Override  
  76.     public boolean onCreateOptionsMenu(Menu menu) {  
  77.         // Inflate the menu; this adds items to the action bar if it is present.  
  78.         getMenuInflater().inflate(R.menu.main, menu);  
  79.         return true;  
  80.     }  
  81.   
  82.     private String[] mStrings = { "John""Michelle""Amy""Kim""Mary",  
  83.             "David""Sunny""James""Maria""Michael""Sarah""Robert",  
  84.             "Lily""William""Jessica""Paul""Crystal""Peter",  
  85.             "Jennifer""George""Rachel""Thomas""Lisa""Daniel""Elizabeth",  
  86.             "Kevin" };  
  87. }  


0
0 0