Android Studio-PullToRefresh框架是一个实现View下拉刷新的开源框架。下拉刷新这个功能我们都比较常见了,今天介绍的就是这个功能的实现。
开源下载地址:https://github.com/chrisbanes/Android-PullToRefresh
下载项目包,将library包导入,查看源码。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PullToRefresh">
<!-- A drawable to use as the background of the Refreshable View -->
<!-- 设置刷新view的背景 -->
<attr name="ptrRefreshableViewBackground" format="reference|color" />
<!-- A drawable to use as the background of the Header and Footer Loading Views -->
<!-- 设置头部view的背景 -->
<attr name="ptrHeaderBackground" format="reference|color" />
<!-- Text Color of the Header and Footer Loading Views -->
<!-- 设置头部/底部文字的颜色 -->
<attr name="ptrHeaderTextColor" format="reference|color" />
<!-- Text Color of the Header and Footer Loading Views Sub Header -->
<!-- 设置头部/底部副标题的文字颜色 -->
<attr name="ptrHeaderSubTextColor" format="reference|color" />
<!-- Mode of Pull-to-Refresh that should be used -->
<!-- 设置下拉刷新的模式,有多重方式可选。无刷新功能,从顶部刷新,从底部刷新,二者都有,只允许手动刷新 -->
<attr name="ptrMode">
<flag name="disabled" value="0x0" />
<flag name="pullFromStart" value="0x1" />
<flag name="pullFromEnd" value="0x2" />
<flag name="both" value="0x3" />
<flag name="manualOnly" value="0x4" />
<!-- These last two are depreacted -->
<flag name="pullDownFromTop" value="0x1" />
<flag name="pullUpFromBottom" value="0x2" />
</attr>
<!-- Whether the Indicator overlay(s) should be used -->
<!-- 是否显示指示箭头 -->
<attr name="ptrShowIndicator" format="reference|boolean" />
<!-- Drawable to use as Loading Indicator. Changes both Header and Footer. -->
<!-- 指示箭头的图片 -->
<attr name="ptrDrawable" format="reference" />
<!-- Drawable to use as Loading Indicator in the Header View. Overrides value set in ptrDrawable. -->
<!-- 顶部指示箭头的图片,设置后会覆盖ptrDrawable中顶部的设置 -->
<attr name="ptrDrawableStart" format="reference" />
<!-- Drawable to use as Loading Indicator in the Fooer View. Overrides value set in ptrDrawable. -->
<attr name="ptrDrawableEnd" format="reference" />
<!-- Whether Android's built-in Over Scroll should be utilised for Pull-to-Refresh. -->
<attr name="ptrOverScroll" format="reference|boolean" />
<!-- Base text color, typeface, size, and style for Header and Footer Loading Views -->
<!-- 设置文字的基本字体 -->
<attr name="ptrHeaderTextAppearance" format="reference" />
<!-- Base text color, typeface, size, and style for Header and Footer Loading Views Sub Header -->
<!-- 设置副标题的基本字体 -->
<attr name="ptrSubHeaderTextAppearance" format="reference" />
<!-- Style of Animation should be used displayed when pulling. -->
<!-- 设置下拉时标识图的动画,默认为rotate -->
<attr name="ptrAnimationStyle">
<flag name="rotate" value="0x0" />
<flag name="flip" value="0x1" />
</attr>
<!-- Whether the user can scroll while the View is Refreshing -->
<!-- 设置刷新时是否允许滚动,一般为true -->
<attr name="ptrScrollingWhileRefreshingEnabled" format="reference|boolean" />
<!--
Whether PullToRefreshListView has it's extras enabled. This allows the user to be
able to scroll while refreshing, and behaves better. It acheives this by adding
Header and/or Footer Views to the ListView.
-->
<!-- 允许在listview中添加头/尾视图 -->
<attr name="ptrListViewExtrasEnabled" format="reference|boolean" />
<!--
Whether the Drawable should be continually rotated as you pull. This only
takes effect when using the 'Rotate' Animation Style.
-->
<!-- 当设置rotate时,可以用这个来设置刷新时旋转的图片 -->
<attr name="ptrRotateDrawableWhilePulling" format="reference|boolean" />
<!-- BELOW HERE ARE DEPRECEATED. DO NOT USE. -->
<attr name="ptrAdapterViewBackground" format="reference|color" />
<attr name="ptrDrawableTop" format="reference" />
<attr name="ptrDrawableBottom" format="reference" />
</declare-styleable>
</resources>
插入PullToRefreshListView
<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"
tools:context="${relativePackage}.${activityClass}"
android:background="#000000">
<!-- The PullToRefreshListView replaces a standard ListView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrAnimationStyle="rotate"
ptr:ptrHeaderTextColor="#ffffff"
ptr:ptrHeaderSubTextColor="#00ffff"
ptr:ptrHeaderBackground="@null"
ptr:ptrDrawable="@drawable/ic_launcher"/>
</RelativeLayout>
Activity 中设置下拉刷新监听
1.使用 setOnRefreshListener设置刷新监听,
2.使用OnRefreshListener回调在 onRefresh方法中触发加载数据操作.实现下拉监听.
3.使用 AsyncTask 实现加载数据 .
public class MainActivity extends AppCompatActivity {
PullToRefreshListView id_lv_test;
private List<String> mList = new ArrayList<String>(){
{
add("Chinese");
add("Math");
add("English");
}
};
private ArrayAdapter<String> mLvAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取控件
id_lv_test = (PullToRefreshListView) findViewById(R.id.id_lv_test);
// 设置适配器
mLvAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mList);
id_lv_test.setAdapter(mLvAdapter);
// 设置下拉刷新监听.
id_lv_test.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
SystemClock.sleep(3 * 1000);
mList.add("新数据 - " + mLvAdapter.getCount());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
id_lv_test.onRefreshComplete();
}
}.execute();
}
}
);
}
}
id_lv_test.setMode(PullToRefreshBase.Mode.BOTH);
// 设置上下刷新
id_lv_test.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
Log.d(TAG,"下拉动作 : " + Thread.currentThread().getId());
id_lv_test.postDelayed(new Runnable() {
@Override
public void run() {
id_lv_test.onRefreshComplete();
}
},50);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
Log.d(TAG,"上拉动作");
id_lv_test.postDelayed(new Runnable() {
@Override
public void run() {
id_lv_test.onRefreshComplete();
}
},50);
}
});
id_lv_test.getLoadingLayoutProxy(false,true).setPullLabel("上拉加载更多.");
id_lv_test.getLoadingLayoutProxy(false,true).setRefreshingLabel("正在加载数据...");
id_lv_test.getLoadingLayoutProxy(false,true).setReleaseLabel("松开加载更多...");
id_lv_test.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
id_lv_test.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新...");
id_lv_test.getLoadingLayoutProxy(true,false).setReleaseLabel("松开刷新...");