//APP 进行全局初始化
package com.example.accessimage;
import android.app.Application;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = null;
DisplayImageOptions options = new DisplayImageOptions.Builder()
.build();
configuration = new ImageLoaderConfiguration.Builder(this)
//内存缓存大小 10 百分之10
.memoryCacheSizePercentage(10)
//配置磁盘缓存
.diskCacheSize(50*1024*1024)
.build();
//初始化 : 只有第一次初始化有效果
ImageLoader.getInstance().init(configuration);
}
}
//主页面
package com.example.accessimage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Button button;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listview);
button = findViewById(R.id.lleft);
adapter = new MyAdapter(this);
listView.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//加载数据
NetUtil.getInstance().getRequset(
"http://api.expoon.com/AppNews/getNewsList/type/1/p/1",
Bean.class,
new NetUtil.Callbak<Bean>() {
@Override
public void onSuccess(Bean o) {
adapter.setMlist(o.getData());
}
});
}
});
//非静态方法必须通过类的实例对象调用: new
//静态方法可以直接通过类调用
//NetUtil nu = NetUtil.getInstance();
//NetUtil nu2 = new NetUtil();
}
}
//工具类
package com.example.accessimage;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class NetUtil {
//单列:整个应用中只有一个实例对象
private static NetUtil instance;
//1 私有构造方法
private NetUtil() {}
//2 提供静态方法让外面获取实例对象
public static NetUtil getInstance() {
if(instance == null) {
instance = new NetUtil();
}
return instance;
}
public interface Callbak<T>{
void onSuccess(T t);
}
public void getRequset(final String urlStr,final Class clazz,final Callbak callbak ){
new AsyncTask<String,Void,Object>(){
@Override
protected Object doInBackground(String... strings) {
return getRequset(urlStr, clazz);
}
@Override
protected void onPostExecute(Object o) {
callbak.onSuccess(o);
}
}.execute(urlStr);
}
public <T> T getRequset(String urlStr,Class clazz){
return (T) new Gson().fromJson(getRequset(urlStr),clazz);
}
public String getRequset(String urlStr){
String result = "";
try {
URL url = new URL(urlStr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200){
result = stream2String(urlConnection.getInputStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private String stream2String(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
for (String tmp = bf.readLine(); tmp != null ; tmp = bf.readLine()){
sb.append(tmp);
}
return sb.toString();
}
}
//适配器
package com.example.accessimage;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Display;
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.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context context;
private List <Bean.DataBean> mlist;
public MyAdapter(Context context) {
this.context = context;
mlist = new ArrayList<>();
}
public void setMlist(List<Bean.DataBean> mlist) {
this.mlist = mlist;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Bean.DataBean getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
holder.textView1 = convertView.findViewById(R.id.textview1);
holder.textView2 = convertView.findViewById(R.id.textview2);
holder.imageView = convertView.findViewById(R.id.imageview);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView1.setText(mlist.get(position).getNews_id());
holder.textView2.setText(mlist.get(position).getNews_title());
DisplayImageOptions options = new DisplayImageOptions.Builder()
//配置色彩模式
.bitmapConfig(Bitmap.Config.RGB_565)
//配置是否缓存
.cacheInMemory(true)
.cacheOnDisk(true)
//设置缩放
.imageScaleType(ImageScaleType.EXACTLY)
//圆角
.displayer(new RoundedBitmapDisplayer(9))
.displayer(new FadeInBitmapDisplayer(2000))
.build();
ImageLoader.getInstance().displayImage(getItem(position).getPic_url(),holder.imageView,options);
return convertView;
}
class ViewHolder{
private TextView textView1,textView2;
private ImageView imageView;
}
}
//bean类
package com.example.accessimage;
import java.util.List;
public class Bean {
private int status;
private String info;
private List<DataBean> data;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String news_id;
private String news_title;
private String news_summary;
private String pic_url;
public String getNews_id() {
return news_id;
}
public void setNews_id(String news_id) {
this.news_id = news_id;
}
public String getNews_title() {
return news_title;
}
public void setNews_title(String news_title) {
this.news_title = news_title;
}
public String getNews_summary() {
return news_summary;
}
public void setNews_summary(String news_summary) {
this.news_summary = news_summary;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}
}
}