1.MainActiv类
private XListView xLv;
private MyBaseAdapter adapter;
//定义一个页码变量记录用户刷新到第几页了
private int pageIndex = 1;
//定义一个变量去记录用户操作行为(例如:上拉、下拉)
private boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xLv = (XListView) findViewById(R.id.xLv);
//设置是否可以上下拉加载
xLv.setPullLoadEnable(true);
xLv.setXListViewListener(this);
if(isOnline()) {
//有网,联网请求数据
getData();
}else{
//弹出提示对话框
showDialog();
}
}
private void showDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("怎么可没网呢");
builder.setNegativeButton("不联网,",null);
builder.setPositiveButton("联网", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//跳转网络设置界面
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
builder.create().show();
}
public boolean isOnline() {
//得到一个连接管理者
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
//得到联网信息
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
//判断设备是否联网
return (networkInfo != null && networkInfo.isConnected());
}
private void getData(){
try {
getNetWorkData("http://apis.juhe.cn/cook/query.php?menu="+ URLEncoder.encode("西红柿","utf-8")+"&key=12345&pn="+pageIndex+"&rn=10");
} catch (Exception e) {
e.printStackTrace();
}
}
private void getNetWorkData(String url){
new AsyncTask(){
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s == null){
return;
}
Gson gson = new Gson();
MenuInfo menuInfo = gson.fromJson(s,MenuInfo.class);
List dataBeanList = menuInfo.getResult().getData();
if(adapter == null) {
//将数据绑定到适配上
adapter = new MyBaseAdapter(MainActivity.this, dataBeanList);
//将适配器上的数据映射到ListView
xLv.setAdapter(adapter);
}else{
adapter.loadMore(dataBeanList,flag);
}
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int code = connection.getResponseCode();
if(code == 200){
InputStream is = connection.getInputStream();
return StreamTools.readFromNetWork(is);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(url);
}
@Override
public void onRefresh() {
flag = true;
++pageIndex;
getData();
//停止下拉刷新
xLv.stopRefresh(true);
}
@Override
public void onLoadMore() {
Log.d("MainActivity", "上拉加载更多数据");
flag = false;
++pageIndex;
getData();
//停止加载更多
xLv.stopLoadMore();
}
@Override
public void onCreate() {
super.onCreate();
String path = Environment.getExternalStorageDirect
ory()+"1507DAccess";
File cacheDir = new File(path);
ImageLoaderConfiguration
config = new ImageLoaderConfiguration
.Builder(this)
.threadPriority(100)
.threadPoolSize(3)
.memoryCacheExtraOptions(200,200)
.memoryCacheSize(2 * 1024 * 1024)
.diskCache(new UnlimitedDiskCache(cacheDir))
.diskCacheFileNameGenerat
or(new Md5FileNameGenerator())
.diskCacheSize(50 * 1024 * 1024)
.build();
ImageLoader.getInstance().init(config);
}
public static DisplayImageOptions getOptons(){
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageForEmptyUri(R.mipmap.ic_empty)
.showImageOnFail(R.mipmap.ic_error)
.showImageOnLoading(R.mipmap.loading)
.build();
return options;
}
private Context context;
private List dataBeanList;
public MyBaseAdapter(Context context,List dataBeanList){
this.context=context;
this.dataBeanList = dataBeanList;
}
public void loadMore(List datas,boolean flag){
for (MenuInfo.ResultBean.DataBean dataBean : datas) {
//flag = true 代表下拉
false 上拉
if(flag){
dataBeanList.add(0,dataBean);
}else {
//将更多的数据添加到原来与适配器绑定的集合中去
dataBeanList.add(dataBean);
}
}
//刷新界面
notifyDataSetChanged();
}
@Override
public int getCount() {
return dataBeanList != null?dataBeanList.size() : 0;
}
@Override
public Object getItem(int position) {
return dataBeanList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//填充布局
if(convertView == null){
convertView = convertView.inflate(context,R.layout.item,null);
}
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
TextView tvContent = (TextView) convertView.findViewById(R.id.tvContent);
//设置文本信息
tvContent.setText(dataBeanList.get(position).getTitle());
//加载图片
ImageLoader.getInstance().displayImage(dataBeanList.get(position).getAlbums().get(0),ivIcon,MyApplication.getOptons());
return convertView;
}
![xListView(联网判断) xListView(联网判断)](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
}
2.Application类
public class MyApplication extends Application {
}
3.适配器类
public class MyBaseAdapter extends BaseAdapter {
}
4.工具类
5.Json串类
6.访问网络、读取内存权限
![xListView(联网判断) xListView(联网判断)](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)