最新自己写了一个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