MainActivity
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private PullToRefreshListView refreshListView;
private MyAdapter adapter;
private ILoadingLayout startLabels;
private ILoadingLayout endLabels;
private int page = 10;
private List<Bean.NewslistBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
refreshListView = (PullToRefreshListView) findViewById(R.id.refresh_list_view);
//获取数据,,,设置适配器
new My().execute(); //设置刷新时显示的刷新状态
/**设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,
PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
refreshListView.setMode(PullToRefreshBase.Mode.BOTH);
/**
* 设置下拉和上拉的时候显示的文字
*/
//通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的文字的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
startLabels = refreshListView.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在刷新...");
startLabels.setReleaseLabel("放开刷新");
endLabels = refreshListView.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
//设置上拉下拉的监听事件
/**
* 当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只
* 调用onPullDownToRefresh()方法,
* Mode.PULL_FROM_END的时候只调用onPullUpToRefresh()方法.
*/
refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
//下拉的时候调用
@Override
public void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) {
//加载数据(异步的操作)....数据完成之后,取消刷新
//执行onRefreshComplete();方法必须在异步下执行,不能和主进程一起执行
page--;
if (page > 0) {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
String string = "";
String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=" + page;
try {
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
int code = con.getResponseCode();
if (code == 200) {
InputStream is = con.getInputStream();
byte[] b = new byte[1024];
int l = 0;
while ((l = is.read(b)) != -1) {
String str = new String(b, 0, l);
string += str;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return string;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
//dataDataBean.getResults();
//数据要添加到集合的最前边
list.addAll(0, bean.getNewslist());
if (adapter == null) {
adapter = new MyAdapter();
refreshListView.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
refreshListView.onRefreshComplete();
}
};
asyncTask.execute();
} else {
Toast.makeText(MainActivity.this, "没有最新数据了", Toast.LENGTH_SHORT).show();
refreshListView.onRefreshComplete();
}
}
//上拉的时候调用
@Override
public void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) {
page++;
new My().execute();
}
});
}
class My extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
String string = "";
String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=" + page;
try {
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int code = con.getResponseCode();
if (code == 200) {
InputStream is = con.getInputStream();
byte[] b = new byte[1024];
int l = 0;
while ((l = is.read(b)) != -1) {
String str = new String(b, 0, l);
string += str;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return string;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
Gson gson = new Gson();
Bean bean = gson.fromJson((String) o, Bean.class);
list = bean.getNewslist();
if (adapter == null) {
adapter = new MyAdapter();
refreshListView.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
refreshListView.onRefreshComplete();
}
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i
;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Hand hand;
if (view == null) {
view = View.inflate(MainActivity.this, R.layout.item, null);
hand = new Hand();
hand.te1 = view.findViewById(R.id.te1);
hand.te2 = view.findViewById(R.id.te2);
hand.te3 = view.findViewById(R.id.te3);
hand.img = view.findViewById(R.id.img);
view.setTag(hand);
} else {
hand = (Hand) view.getTag();
}
hand.te1.setText(list.get(i).getTitle());
hand.te2.setText(list.get(i).getDescription());
hand.te3.setText(list.get(i).getCtime());
String url = list.get(i).getPicUrl();
ImageLoader il = ImageLoader.getInstance();
il.displayImage(url, hand.img);
return view;
}
}
class Hand {
ImageView img;
TextView te1, te2, te3;
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/refresh_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrAnimationStyle="flip"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF" />
</RelativeLayout>