public class MainActivity extends AppCompatActivity {
private ListView lv;
int p = 1;
private MyAdapter adapter;
private List<Bean.ResultBean.DataBean> lists = new ArrayList<>();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what % 2 == 1) {
String a = (String) msg.obj;
Gson gson = new Gson();
Bean bean = gson.fromJson(a, Bean.class);
List<Bean.ResultBean.DataBean> list = bean.getResult().getData();
adapter = new MyAdapter(MainActivity.this, list);
lv.setAdapter(adapter);
} else {
String a = (String) msg.obj;
Gson gson = new Gson();
Bean bean = gson.fromJson(a, Bean.class);
List<Bean.ResultBean.DataBean> list = bean.getResult().getData();
for (Bean.ResultBean.DataBean li : list) {
lists.add(li);
}
adapter = new MyAdapter(MainActivity.this, lists);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
if(swi.isRefreshing())
swi.setRefreshing(false);
}
}
};
boolean bo;
private SwipeRefreshLayout swi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (bo == true && scrollState == SCROLL_STATE_IDLE) {
++p;
initData();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
bo = true;
}
}
});
swi.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
lists.clear();
initData();
}
});
}
private void initData() {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mCache-net";
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
Cache cache = new Cache(file, 8L* 1024L * 1024L);
OkHttpClient client = new OkHttpClient.Builder().cache(cache).addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response proceed = chain.proceed(request);
proceed = proceed.newBuilder().removeHeader("Pragma").removeHeader("Cache-Control")
.addHeader("Cache-Control", "max-age=" + 20000).build();
return proceed;
}
}).build();
CacheControl cacheControl = new CacheControl.Builder().maxAge(200, TimeUnit.MINUTES).build();
Request request = new Request.Builder().url("http://v.juhe.cn/toutiao/index?type=yule&key=097060266650f67b2cebd2a06aded587").cacheControl(cacheControl).get().build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string();
Message message = handler.obtainMessage(p, s.toString());
handler.sendMessage(message);
}
});
}
private void initView() {
lv = (ListView) findViewById(R.id.lv);
swi = (SwipeRefreshLayout) findViewById(R.id.swi);
}
}
android_上拉下拉
最新推荐文章于 2022-04-08 15:15:45 发布
本文介绍了一个基于Android平台的新闻应用开发实例,应用中使用了ListView展示新闻列表,并通过SwipeRefreshLayout实现下拉刷新功能。该应用还实现了上拉加载更多功能,并使用OkHttp进行网络请求,同时具备缓存机制。
摘要由CSDN通过智能技术生成