一:下载
1.联网搜索www.github.com(最优秀的开源网站)
2.在网站里搜索pull to refresh
接下来如下图所示:
二:在Android Studio中导入你下载的第三方
至此,在项目中导入第三方已经完成。
需要注意的是,第三方里面有的东西可能过时,像第三方的java文件PullToRefreshWebView.java可能报错,就需要改,如下图所示
然后
三:编写测试代码
layout布局文件
activity_pulltorefresh.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ptrlv_refresh"
>
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
Activity的java代码
PullToRefreshActivity.java
package com.zking.android08_widgetplus;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
/**
* @author Zking-Snail
* @time 2017/6/9 16:35
* @Version ${REV}
*/
public class PullToRefreshActivity extends AppCompatActivity {
private PullToRefreshListView ptrlv_refresh;
private List<String> list=new ArrayList<>();
private ArrayAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pulltorefresh);
ptrlv_refresh = (PullToRefreshListView) findViewById(R.id.ptrlv_refresh);
for (int i = 0; i <10 ; i++) {
list.add("admin爱小小和喵喵"+i+"次");
}
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
ptrlv_refresh.setAdapter(adapter);
//给ListView设置下拉刷新的监听
ptrlv_refresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
new MyTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
}
});
}
class MyTask extends AsyncTask{
//在后台 获取数据
//内部有线程机制
@Override
protected Object doInBackground(Object[] objects) {
for (int i = 0; i <3 ; i++) {
list.add(0,"小小和喵喵也爱admin"+i+"次");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
//更新UI
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
ptrlv_refresh.onRefreshComplete();
//通知适配器数据 发生改变
adapter.notifyDataSetChanged();
}
}
}
配置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zking.android08_widgetplus">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".PullToRefreshActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
四:效果图
最后,一通百通,像XListView这样的下拉刷新控件也差不多可以这样写。
欢迎大家来指出我的不足哦O(∩_∩)O~