//上拉加载,下拉刷新
package com.bwie.xlistviewdemo1;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.bwie.adapters.MyBaseAdapter;
import com.bwie.beans.News;
import com.bwie.beans.SuperClass;
import com.google.gson.Gson;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{
private XListView xListView;
private ArrayList<News> newslist;
private MyBaseAdapter myAdapter;
private final static String JSON_URL = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xlv);
xListView.setPullLoadEnable(true);//设置可以刷新和加载
xListView.setXListViewListener(this);
//刚进来,加载第一版数据
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
//线程加载数据
String str = new NetWorkUtils().getJsonByUrlConnection(JSON_URL);
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClass superClass = new Gson().fromJson(s,SuperClass.class);
newslist = superClass.getNewslist();//得到第一页数据
myAdapter = new MyBaseAdapter(newslist, MainActivity.this);
xListView.setAdapter(myAdapter);
}
}.execute();
}
/**
* 停止刷新或加载时
*/
public void onLoad(){
xListView.stopRefresh();
xListView.stopLoadMore();
xListView.setRefreshTime("刚刚");
}
/**
* 下拉会回调此方法,在次方法中写刷新的代码
*/
@Override
public void onRefresh() {
Toast.makeText(this,"刷新",Toast.LENGTH_SHORT).show();
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
//线程加载数据
String str = new NetWorkUtils().getJsonByUrlConnection(JSON_URL);
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClass superClass = new Gson().fromJson(s,SuperClass.class);
newslist = superClass.getNewslist();//得到第一页数据
myAdapter = new MyBaseAdapter(newslist, MainActivity.this);
xListView.setAdapter(myAdapter);
onLoad();
}
}.execute();
}
/**
* 上拉到listview的最后一条显示出来,然后会调用此方法,在此方法中加载下一页数据
*/
@Override
public void onLoadMore() {
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
//线程加载数据
String str = new NetWorkUtils().getJsonByUrlConnection(JSON_URL);
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClass superClass = new Gson().fromJson(s,SuperClass.class);
ArrayList<News> newData = superClass.getNewslist();//得到下一页数据(比如2,3,4.。。)
newslist.addAll(newData);//把新的一页的数据集合,加到总的数据集合
myAdapter.notifyDataSetChanged();//刷新适配器,展示全部数据
onLoad();
}
}.execute();
}
}
//请求网络加载图片
ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(config
//网络图片请求代码//通过imageloader加载图片,显示在次行
ImageLoader.getInstance().displayImage(newsObj.getPicUrl(),holder2.imageView);
//判断网络是否可用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//进入系统判读网络
connManager = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connManager.getActiveNetworkInfo();
int type = activeNetworkInfo.getType();//当前网络具体类型(联通,电信,移动2G3G4G)
if(activeNetworkInfo!=null && activeNetworkInfo.isAvailable()){//有网
NetworkInfo.State state1 = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();//得到是否是wifi网络
if(NetworkInfo.State.CONNECTED==state1){
Toast.makeText(this,"您是wifi,放心使用",Toast.LENGTH_SHORT).show();
//网络请求的代码.....
}else{
NetworkInfo.State state2 = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();//得到是否是移动网络
if(NetworkInfo.State.CONNECTED==state2){//掏钱的移动数据
new AlertDialog.Builder(MainActivity.this)
.setTitle("网络")
.setMessage("当前是移动数据")
.setPositiveButton("土豪访问", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//继续编写网络请求的代码
}
})
.setNegativeButton("停止访问",null)
.show();
}
}
}else{
Toast.makeText(this,"您当前没有网络,请检查",Toast.LENGTH_SHORT).show();
// 跳转到无限wifi网络设置界面
startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
}
}