package com.example.zhoukao1; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; 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 com.google.gson.Gson; import com.limxing.xlistview.view.XListView; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.List; import static com.umeng.socialize.utils.DeviceConfig.context; /** * Created by dell on 2017/8/4. */ public class MyFragemt extends Fragment implements XListView.IXListViewListener { private View view; private XListView listview; private int inder = 1; private boolean flag = false; private ImageLoader imageLoader; private DisplayImageOptions options; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.myfragment, container, false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); listview = (XListView) view.findViewById(R.id.listview); listview.setPullLoadEnable(true); listview.setXListViewListener(this); try { getDates("http://apis.juhe.cn/cook/query.php?"); } catch (Exception e) { e.printStackTrace(); } } public void getDates(String path) { new AsyncTask<String, Void, String>() { private Myasdpter myasdpter; @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s != null) { Gson gson = new Gson(); ReadInfo readInfo = gson.fromJson(s, ReadInfo.class); List<ReadInfo.ResultBean.DataBean> data = readInfo.getResult().getData(); if (myasdpter == null) { myasdpter = new Myasdpter(data); listview.setAdapter(new Myasdpter(data)); } else { myasdpter.LoadMore(data, flag); myasdpter.notifyDataSetChanged(); } } } @Override protected String doInBackground(String... params) { try { String path = params[0]; URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); OutputStream outputStream = urlConnection.getOutputStream(); outputStream.write(("key=a71e47bbe5a7e7a2ecf068ca9a756032&menu=" + URLEncoder.encode("西红柿炒鸡蛋", "utf-8")).getBytes()); outputStream.flush(); int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { InputStream is = urlConnection.getInputStream(); String json = StreamTook.read(is); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(path); } @Override public void onRefresh() { ++inder; getDates("http://apis.juhe.cn/cook/query.php?"); flag = true; listview.stopRefresh(true); } @Override public void onLoadMore() { ++inder; getDates("http://apis.juhe.cn/cook/query.php?"); flag = false; listview.stopLoadMore(); } class Myasdpter extends BaseAdapter { List<ReadInfo.ResultBean.DataBean> data; public Myasdpter(List<ReadInfo.ResultBean.DataBean> data) { this.data = data; imageLoader=ImageLoader.getInstance(); String s = Environment.getExternalStorageDirectory() + "qwe"; File file = new File(s); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getActivity()) .memoryCacheSize(2 * 1024 * 1024) .memoryCacheExtraOptions(200, 200) .threadPoolSize(3) .threadPriority(1000) .diskCache(new UnlimitedDiskCache(file)) .diskCacheSize(50 * 1024 * 1024) .diskCacheFileCount(50) .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .build(); ImageLoader.getInstance().init(configuration); } public void LoadMore(List<ReadInfo.ResultBean.DataBean> datas, boolean flag) { for (ReadInfo.ResultBean.DataBean bean : datas) { if (flag) { data.add(0, bean); } else { data.add(bean); } } } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { if (position % 2 == 0) { return 0; } else { return 1; } } @Override public int getViewTypeCount() { return 2; } @Override public View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); switch (type) { case 0: { if (convertView == null) { convertView = convertView.inflate(getActivity(), R.layout.myfragitem1, null); } TextView textitem1 = (TextView) convertView.findViewById(R.id.textitem1); ImageView imagitem1 = (ImageView) convertView.findViewById(R.id.imagitem1); textitem1.setText(data.get(position).getTitle()); getimage(data.get(position).getSteps().get(0).getImg(), imagitem1); } break; case 1: { if (convertView == null) { convertView = convertView.inflate(getActivity(), R.layout.myfragitem2, null); } TextView textitem2 = (TextView) convertView.findViewById(R.id.textitem2); ImageView imagitem2 = (ImageView) convertView.findViewById(R.id.imagitem2); textitem2.setText(data.get(position).getTitle()); getimage(data.get(position).getSteps().get(0).getImg(), imagitem2); } break; } return convertView; } } public void getimage(String path, ImageView imageView) { DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheOnDisk(true) .cacheInMemory(true) .build(); ImageLoader.getInstance().displayImage(path, imageView, options); } }