1.先写布局
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/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"
/>
2.主页面 public class MainActivity extends AppCompatActivity {
private List<JavaBean.DataBean> list;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 第一步:得到控件
final PullToRefreshListView mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
/**第二步:设置刷新方式
* 设置刷新的模式:常用的有三种
* PullToRefreshBase.Mode.BOTH //上下拉刷新都可以
* PullToRefreshBase.Mode.PULL_FROM_START //只允许下拉刷新
* PullToRefreshBase.Mode.PULL_FROM_END //只允许上拉刷新
*
*/
mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
// 第三步:设置是否允许刷新的时候可以滑动()
mPullRefreshListView.setScrollingWhileRefreshingEnabled(true);
//第四步:数据
final String str = "http://www.xieast.com/api/news/news.php?page=1";
HttpUtils.httpAsynTask(str, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
//解析
Gson gson = new Gson();
JavaBean bean = gson.fromJson(s, JavaBean.class);
list = bean.getData();
//第五步:设置适配器
adapter = new MyAdapter(MainActivity.this, list);
mPullRefreshListView.setAdapter(adapter);
}
});
//第六步:监听事件
mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//下拉刷新
HttpUtils.httpAsynTask(str, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
//解析
Gson gson = new Gson();
JavaBean bean = gson.fromJson(s, JavaBean.class);
list = bean.getData();
//设置适配器
adapter = new MyAdapter(MainActivity.this, list);
mPullRefreshListView.setAdapter(adapter);
mPullRefreshListView.onRefreshComplete();
}
});
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//上拉加载
HttpUtils.httpAsynTask(str, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
//解析
Gson gson = new Gson();
JavaBean bean = gson.fromJson(s, JavaBean.class);
List<JavaBean.DataBean> beans = bean.getData();
///把查询到的新数据添加到集合中
list.addAll(beans);
//刷新适配器
adapter.notifyDataSetChanged();
// 当列表被刷新时,调用onRefreshComplete
mPullRefreshListView.onRefreshComplete();
}
});
}
});
}
}
3.适配器
public class MyAdapter extends BaseAdapter {
private Context context;
private List<JavaBean.DataBean> list;
public MyAdapter(Context context, List<JavaBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= View.inflate(context,android.R.layout.simple_list_item_1,null);
TextView textView= convertView.findViewById(android.R.id.text1);
textView.setText("TTTTT"+position);
return convertView;
}
}
4.HttpUtils工具类
public class HttpUtils {
public static void httpAsynTask(String strUrl, final CallBackString backString) {
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... strings) {
return httpGet(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//接口回调的方法
backString.getData(s);
}
}.execute(strUrl);
}
//接口================================
interface CallBackString{
void getData(String s);
}
//==============================================
public static String httpGet(String strUrl) {
//设置url
try {
URL url = new URL(strUrl);
//获取HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置为get请求
connection.setRequestMethod("GET");
//设置连接主机超时时间
connection.setConnectTimeout(5000);
//设置从主机读取数据超时
connection.setReadTimeout(5000);
//得到数据
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
//拼接数据
StringBuilder builder = new StringBuilder();
String str = "";
while ((str = reader.readLine()) != null) {
builder.append(str);
}
//关闭连接
connection.disconnect();
//返回数据
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
5.还有bean类