PullToRefresh的簡單使用
來自: http://blog.csdn.net//chenguang79/article/details/43194637
在做android項目的時候,我們經常要用到上拉刷新列表,下拉刷新列表的功能,這里簡單介紹一個PullToRefresh的使用。
一,下載PullToRefresh,地址:https://github.com/chrisbanes/Android-PullToRefresh。這里面,我們只要其中的Library工程。將它引入我們的工程,在android studio1.01中的引用方法,請看http://blog.csdn.net/chenguang79/article/details/43150303
二,應用,這里我使用了一個自定義的Adapter,我感覺這樣能更好的和實戰結和起來。下面是代碼,我在代碼里,都進行了注解
1,布局文件(activity_user_defined_list__pulltore_fresh.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.cg.studiotest.userDefinedList_PulltoreFresh">
<!--下面就是PullToRefresh控件-->
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/userDefinedList_pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</RelativeLayout> 這個控件的屬性基本上和listVIew是一樣的,就不多說了。
2,列表布局文件(pulltorefresh_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_pulltorefresh_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/img_pulltorefresh_item_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout> 3,程序(userDefinedList_PulltoreFresh.java)
package com.example.cg.studiotest;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ListView;
import android.widget.Toast;
import com.example.cg.baseAdapter.pulltorefresh_baseAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class userDefinedList_PulltoreFresh extends Activity {
//定義下拉刷新列表控件
private PullToRefreshListView userDefinedList_pull_refresh_list;
//定義原始數據源
private String[] mainStr = {"宋元通寶","太平通寶","淳化元寶","至道元寶","咸平元寶","景德元寶","祥福元寶",
"祥福通寶","天禧通寶","天圣元寶","明道元寶","景祐元寶","皇寶通寶","慶歷重寶"};
//定義新加的數據源
private String[] strArray = {"至和元寶","至和重寶","嘉祐元寶","喜祐通寶","治平元寶","熙寧元寶","熙寧通寶"};
//定義要從新的數據源中取出哪個值
private int num;
//定義List的Adapter
private pulltorefresh_baseAdapter pullAdapter;
//定義向Adaptger中傳值的數據
private List<String> listDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_defined_list__pulltore_fresh);
//初始化數據
listDate = new ArrayList<String>();
//初始化值
num = 0;
//循環給數據賦值
for(int i=0;i<mainStr.length;i++)
{
listDate.add(mainStr[i]);
}
//初始化下拉列表控件
userDefinedList_pull_refresh_list = (PullToRefreshListView)this.findViewById(R.id.userDefinedList_pull_refresh_list);
userDefinedList_pull_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String twoInfo = DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
//下拉時,顯示的主要文字,一般的情況下,我們不修改它
refreshView.getLoadingLayoutProxy().setReleaseLabel("放手開始加載數據");
//下拉時,第二行顯示的文字
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(twoInfo);
//數據刷新的時候,顯示的文字
refreshView.getLoadingLayoutProxy().setRefreshingLabel("數據加載中......");
//刷新結束后,顯示的文字,沒有什么意義,一間而過
refreshView.getLoadingLayoutProxy().setPullLabel("數據加載完成!");
//調用異步,開始加載
new GetDataTask().execute();
}
});
//設置底部下拉刷新模式
userDefinedList_pull_refresh_list.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
pullAdapter = new pulltorefresh_baseAdapter(this,listDate);
//設置適配器
ListView actualListView = userDefinedList_pull_refresh_list.getRefreshableView();
actualListView.setAdapter(pullAdapter);
}
private class GetDataTask extends AsyncTask<Void,Void,String>{
/**
* 此處進行異步操作
*/
@Override
protected String doInBackground(Void... params) {
//讓程序休眠一秒鐘,以達到延時的效果
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Toast.makeText(userDefinedList_PulltoreFresh.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
//定義一個變量,用來存取要添加的值,當num值大于新加數據的個數時,就不再給值,而是返回空
String str = "";
if(num!=strArray.length)
{
str = strArray[num];
num++;
}
return str;
}
/**
* 這里是異步操作完,返回的結果,用來修改UI
* @param backStr 異步返回的結果
*/
@Override
protected void onPostExecute(String backStr) {
//判斷一下,傳過來的值是否為空,如是不為空則進行添加
if(!backStr.equals("")) {
listDate.add(backStr);
}
pullAdapter.notifyDataSetChanged();
userDefinedList_pull_refresh_list.onRefreshComplete();
super.onPostExecute(backStr);
}
}
} 4,Adapter文件(pulltorefresh_baseAdapter.java)
package com.example.cg.baseAdapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.cg.studiotest.R;
import java.util.List;
/**
* Created by cg on 2015/1/27.
*/
public class pulltorefresh_baseAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<String> testDate;
public pulltorefresh_baseAdapter(Context context,List<String> testDate)
{
this.mInflater = LayoutInflater.from(context);
this.testDate = testDate;
}
@Override
public int getCount() {
return testDate.size();
}
@Override
public Object getItem(int position) {
return testDate.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
pulltoreItem item;
if(convertView==null)
{
item = new pulltoreItem();
convertView = mInflater.inflate(R.layout.pulltorefresh_item, null);
item.txt_pulltorefresh_item_title = (TextView)convertView.findViewById(R.id.txt_pulltorefresh_item_title);
item.img_pulltorefresh_item_img = (ImageView)convertView.findViewById(R.id.img_pulltorefresh_item_img);
convertView.setTag(item);
}
else
{
item = (pulltoreItem)convertView.getTag();
}
item.txt_pulltorefresh_item_title.setText(testDate.get(position));
return convertView;
}
public class pulltoreItem
{
TextView txt_pulltorefresh_item_title;
ImageView img_pulltorefresh_item_img;
}
} 5,效果圖



本文由用戶 杭州壽星 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!