MainActivity
package com.zsd.frag;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.ListView;
import com.zsd.adapter.MyAdapter;
import com.zsd.app.R;
import com.zsd.bean.Json;
import com.zsd.masync.MyasynTask;
import java.util.ArrayList;
public class Frag_01 extends Fragment {
public String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
private ListView listView;
private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();
private MyAdapter adapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
listView = view.findViewById(R.id.listView1);
adapter = new MyAdapter(list, getActivity());
listView.setAdapter(adapter);
//传值
new MyasynTask(list).execute(urlString);
//刷新
adapter.notifyDataSetChanged();
return view;
}
}
MyAdapter
package com.zsd.adapter;
import android.content.Context;
import android.media.Image;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.zsd.app.R;
import com.zsd.bean.Json;
import com.zsd.masync.MyBitmapasyncTask;
import java.util.ArrayList;
public class MyAdapter extends BaseAdapter {
private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();
private Context context;
public MyAdapter(ArrayList<Json.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView==null){
convertView = View.inflate(context, R.layout.item,null);
holder.text1 = convertView.findViewById(R.id.text1);
holder.image1 = convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}else {
holder = (Holder) convertView.getTag();
}
new MyBitmapasyncTask(holder.image1).execute(list.get(position).getPic_url());
holder.text1.setText(list.get(position).getNews_summary());
return convertView;
}
class Holder{
TextView text1;
ImageView image1;
}
}
MyBitmapasyncTask 图片
package com.zsd.masync;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import com.zsd.net.NetWorkUtils;
public class MyBitmapasyncTask extends AsyncTask<String , View, Bitmap> {
ImageView image1;
public MyBitmapasyncTask(ImageView image1) {
this.image1 = image1;
}
@Override
protected Bitmap doInBackground(String... stirngs) {
Bitmap bitmap = NetWorkUtils.getBitmapJson(stirngs[0]);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
image1.setImageBitmap(bitmap);
}
}
MyasynTask
package com.zsd.masync;
import android.os.AsyncTask;
import android.view.View;
import com.google.gson.Gson;
import com.zsd.bean.Json;
import com.zsd.net.NetWorkUtils;
import java.util.ArrayList;
public class MyasynTask extends AsyncTask<String ,View, String> {
private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();
public MyasynTask(ArrayList<Json.DataBean> list) {
this.list = list;
}
//主线程
@Override
protected String doInBackground(String... strings) {
String json = NetWorkUtils.getJson(strings[0]);
return json;
}
//子线程
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Json json = gson.fromJson(s, Json.class);
ArrayList<Json.DataBean> listtemp = json.getData();
list.addAll(listtemp);
}
}