1. 程式人生 > >PullToRefreshView下拉重新整理上來載入更多,支援任何子view!

PullToRefreshView下拉重新整理上來載入更多,支援任何子view!

最新自己寫了一個PullToRefreshView,這是一個自定義view,繼承於LinearLayout,子控制元件可以是任意控制元件!先上一張福利圖:


由於PullToRefreshView繼承於LinearLayout,它有著ViewGroup的特性,子控制元件可以是任意的View。所以用法也和LinearLayout一樣,我們來看一下xml中的佈局:

<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_gray"
    android:orientation="vertical" >

	<com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView 
	    android:id="@+id/pull_to_refresh_view"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:background="@color/white"
	    android:orientation="vertical">
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/str01"
	        android:textSize="14pt"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic01"
	        android:contentDescription="@string/image_description"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic02"
	        android:contentDescription="@string/image_description"/>
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/str02"
	        android:textSize="14pt"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic03"
	        android:contentDescription="@string/image_description"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic04"
	        android:contentDescription="@string/image_description"/>
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/str03"
	        android:textSize="14pt"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic05"
	        android:contentDescription="@string/image_description"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic06"
	        android:contentDescription="@string/image_description"/>
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/str04"
	        android:textSize="14pt"/>
	    
	</com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView>
</LinearLayout>
</span>

其中com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView就是自定一個的PullToRefreshView,用法和LinearLayout一致。
而Activity中的內碼表很簡單,如下:
<span style="font-size:14px;">package com.jimstin.pulltorefreshviewdemo;

import com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView;
import com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView.OnLoadListener;
import com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView.OnRefreshListener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

	private PullToRefreshView mPullToRefreshView;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

	private void initView() {
		mPullToRefreshView = (PullToRefreshView) findViewById(R.id.pull_to_refresh_view);
		//這個方法必須要呼叫initLayoutParams()
		mPullToRefreshView.initLayoutParams();
		mPullToRefreshView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				super.onRefresh();
				//模擬重新整理
				new AsyncTask<Integer, Integer, Integer>() {

					@Override
					protected Integer doInBackground(Integer... params) {
						try {
							//睡眠2秒
							Thread.sleep(2000);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return null;
					}
					
					protected void onPostExecute(Integer result) {
						stopRefresh();
					};
				}.execute();
			}
		});
		
		mPullToRefreshView.setOnLoadListener(new OnLoadListener() {
			@Override
			public void onLoad() {
				//模擬載入更多
				new AsyncTask<Integer, Integer, Integer>() {

					@Override
					protected Integer doInBackground(Integer... params) {
						try {
							//睡眠2秒
							Thread.sleep(2000);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return null;
					}
					
					protected void onPostExecute(Integer result) {
						stopLoad();
					};
				}.execute();
				super.onLoad();
			}
		});
	}

    
}
</span>

用法:

1.要呼叫PullToRefreshView.initLayoutParams()方法,初始化LayoutParams;
2.設定OnRefreshListener重新整理監聽器,當下拉鬆開手的時候,就會呼叫OnRefresh方法,呼叫完OnRefresh方法後,也就是重新整理完畢後,記住呼叫stopRefresh()方法,用於停止重新整理
3.設定OnLoadListener載入監聽器,當上拉鬆開手的時候,就會呼叫OnLoad 方法,呼叫完OnLoad方法後,也就是載入更多完畢支護,記住要呼叫stopLoad()方法,用於停止載入


注意:目前的版本PullToRefreshView的父控制元件只能是LinearLayout



這個版本的PullToRefreshView寫的還有待提高,不過這個版本也可以使用。
如大家在使用過程中遇到問題,歡迎留言!我會和大家一起解決。

原始碼地址:http://download.csdn.net/detail/zhangjm_123/8276943