配置图片缓存
package com.bwie.action.banner; import android.content.Context; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; /** * Created by ASUS on 2017/7/27. */ public class App extends com.youth.banner.loader.ImageLoader { public App(Context context){ // 配置图片缓存 DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheInMemory(true) .cacheOnDisk(true) .showImageOnLoading(R.mipmap.ic_launcher) .showImageForEmptyUri(R.mipmap.ic_launcher) .showImageOnFail(R.mipmap.ic_launcher) .build(); // 初始化全局配置 ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder( context ).defaultDisplayImageOptions(options)// 默认配置每张图片都缓存 .build(); ImageLoader.getInstance().init(configuration);// 初始化配置文件 } @Override public void displayImage(Context context, Object path, ImageView imageView) { Glide.with(context).load(path).into(imageView); } }MenuInfo类
package com.bwie.action.banner; import java.util.List; /** * Created by ASUS on 2017/7/27. */ public class Bean { /** * banners : [{"url":"tuchong://?type=event&id=483436","src":"https://s1.tuchong.com/content-image/b4e54f08012de43be4d3a78dceefdd3a.jpg"},{"url":"tuchong://?type=event&id=483438","src":"https://s1.tuchong.com/content-image/03c89ccbdf5eb5ca82b4dbf4756611fd.jpg"},{"url":"tuchong://?type=tag&id=449941","src":"https://s1.tuchong.com/content-image/ae54fd1e185b075ebdc3f45bb6a8c1f2.jpg"}] * hotEvents : [{"tag_id":"483438","tag_name":"京东摄影金像奖·手机组","status":"open","posts":"9643","new_posts":"3622","participants":"3640","end_at":"2017-08-07 00:00:00","title":"京东摄影金像奖·手机组","url":"http://tuchong.com/events/483438/","event_type":"competition","remainingDays":11,"image_posts":[],"list_banner":true,"images":["https://s1.tuchong.com/event-banners/483438/pc-list.jpg?3"],"app_url":"http://tuchong.com/app-events/483438/"},{"tag_id":"436636","tag_name":"第六届侯登科纪实摄影奖","status":"open","posts":"8399","new_posts":"658","participants":"3603","end_at":"2017-07-31 00:00:00","title":"第六届侯登科纪实摄影奖","url":"http://tuchong.com/events/436636/","event_type":"competition","remainingDays":4,"image_posts":[],"list_banner":false,"images":["https://s1.tuchong.com/event-banners/436636/app.jpg?0"],"app_url":"http://tuchong.com/app-events/436636/"},{"tag_id":"467306","tag_name":"有温度的人像","status":"open","posts":"7432","new_posts":"1061","participants":"3379","end_at":null,"title":"有温度的人像","url":"http://tuchong.com/events/467306/","event_type":"topic","remainingDays":999,"image_posts":[],"list_banner":false,"images":["https://s1.tuchong.com/event-banners/467306/app.jpg?0"],"app_url":"http://tuchong.com/app-events/467306/"},{"tag_id":"458081","tag_name":"旧物之美","status":"open","posts":"2323","new_posts":"65","participants":"1434","end_at":null,"title":"旧物之美","url":"http://tuchong.com/events/458081/","event_type":"topic","remainingDays":999,"image_posts":[],"list_banner":false,"images":["https://s1.tuchong.com/event-banners/458081/app.jpg?0"],"app_url":"http://tuchong.com/app-events/458081/"},{"tag_id":"437309","tag_name":"朋友,这是艺术","status":"open","posts":"2364","new_posts":"75","participants":"1094","end_at":null,"title":"朋友,这是艺术","url":"http://tuchong.com/events/437309/","event_type":"topic","remainingDays":999,"image_posts":[],"list_banner":false,"images":["https://s1.tuchong.com/event-banners/437309/app.jpg?0"],"app_url":"http://tuchong.com/app-events/437309/"}] * categories : [{"tag_name":"热门","tag_id":-1},{"tag_name":"最新","tag_id":-2},{"tag_name":"精选","tag_id":-3},{"tag_name":"人像","tag_id":131},{"tag_name":"风光","tag_id":693},{"tag_name":"人文","tag_id":133},{"tag_name":"旅行","tag_id":743},{"tag_name":"建筑","tag_id":767},{"tag_name":"静物","tag_id":684},{"tag_name":"黑白","tag_id":729}] * result : SUCCESS */ private String result; private List<BannersEntity> banners; private List<HotEventsEntity> hotEvents; private List<CategoriesEntity> categories; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public List<BannersEntity> getBanners() { return banners; } public void setBanners(List<BannersEntity> banners) { this.banners = banners; } public List<HotEventsEntity> getHotEvents() { return hotEvents; } public void setHotEvents(List<HotEventsEntity> hotEvents) { this.hotEvents = hotEvents; } public List<CategoriesEntity> getCategories() { return categories; } public void setCategories(List<CategoriesEntity> categories) { this.categories = categories; } public static class BannersEntity { /** * url : tuchong://?type=event&id=483436 * src : https://s1.tuchong.com/content-image/b4e54f08012de43be4d3a78dceefdd3a.jpg */ private String url; private String src; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getSrc() { return src; } public void setSrc(String src) { this.src = src; } } public static class HotEventsEntity { /** * tag_id : 483438 * tag_name : 京东摄影金像奖·手机组 * status : open * posts : 9643 * new_posts : 3622 * participants : 3640 * end_at : 2017-08-07 00:00:00 * title : 京东摄影金像奖·手机组 * url : http://tuchong.com/events/483438/ * event_type : competition * remainingDays : 11 * image_posts : [] * list_banner : true * images : ["https://s1.tuchong.com/event-banners/483438/pc-list.jpg?3"] * app_url : http://tuchong.com/app-events/483438/ */ private String tag_id; private String tag_name; private String status; private String posts; private String new_posts; private String participants; private String end_at; private String title; private String url; private String event_type; private int remainingDays; private boolean list_banner; private String app_url; private List<?> image_posts; private List<String> images; public String getTag_id() { return tag_id; } public void setTag_id(String tag_id) { this.tag_id = tag_id; } public String getTag_name() { return tag_name; } public void setTag_name(String tag_name) { this.tag_name = tag_name; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getPosts() { return posts; } public void setPosts(String posts) { this.posts = posts; } public String getNew_posts() { return new_posts; } public void setNew_posts(String new_posts) { this.new_posts = new_posts; } public String getParticipants() { return participants; } public void setParticipants(String participants) { this.participants = participants; } public String getEnd_at() { return end_at; } public void setEnd_at(String end_at) { this.end_at = end_at; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getEvent_type() { return event_type; } public void setEvent_type(String event_type) { this.event_type = event_type; } public int getRemainingDays() { return remainingDays; } public void setRemainingDays(int remainingDays) { this.remainingDays = remainingDays; } public boolean isList_banner() { return list_banner; } public void setList_banner(boolean list_banner) { this.list_banner = list_banner; } public String getApp_url() { return app_url; } public void setApp_url(String app_url) { this.app_url = app_url; } public List<?> getImage_posts() { return image_posts; } public void setImage_posts(List<?> image_posts) { this.image_posts = image_posts; } public List<String> getImages() { return images; } public void setImages(List<String> images) { this.images = images; } } public static class CategoriesEntity { /** * tag_name : 热门 * tag_id : -1 */ private String tag_name; private int tag_id; public String getTag_name() { return tag_name; } public void setTag_name(String tag_name) { this.tag_name = tag_name; } public int getTag_id() { return tag_id; } public void setTag_id(int tag_id) { this.tag_id = tag_id; } } }listViewTask类
package com.bwie.action.banner; import android.content.Context; import android.os.AsyncTask; import android.view.View; import android.widget.Toast; import com.google.gson.Gson; import com.youth.banner.Banner; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import me.maxwin.view.XListView; /** * Created by ASUS on 2017/7/27. */ public class LisrViewTask extends AsyncTask<Object,Integer,String>{ private XListView xlistview; private Context context; private Banner banner; @Override protected String doInBackground(Object... params) { //耗时操作 网络数据的加载 String path = (String) params[0]; xlistview = (XListView) params[1]; context = (Context) params[2]; /** * 请求网络数据 * * urlStr */ try { // 3初始化URL URL url = new URL(path); // 4打开建立连接 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // 5设置请求方法 connection.setRequestMethod("GET"); // 6设置超时 connection.setReadTimeout(3000); connection.setConnectTimeout(3000); if (connection.getResponseCode() == 200) {// 7请求成功 // 8获取网路输入流 InputStream inputStream = connection.getInputStream(); // 9数据流转换 String json = readStr(inputStream); return json; }else{ Toast.makeText(context,"请求失败请重试",Toast.LENGTH_LONG).show(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); //解析数据 并且改变UI View view = View.inflate(context, R.layout.activity_banner, null); banner = (Banner) view.findViewById(R.id.banner); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); List<Bean.BannersEntity> listbanners = bean.getBanners(); List<Bean.HotEventsEntity> list = bean.getHotEvents(); XListViewAdapter adapter = new XListViewAdapter(list, context); initBanner(listbanners); xlistview.addHeaderView(view); xlistview.setAdapter(adapter); } private void initBanner(List<Bean.BannersEntity> listbanners) { ArrayList<String> stringList = new ArrayList<>(); for (int i =0;i<listbanners.size();i++){ String ss = listbanners.get(i).getSrc(); stringList.add(ss); } banner.setImageLoader(new App(context)); banner.setDelayTime(1000); banner.setImages(stringList); banner.start(); } /** * 输入流转换字符串 * * @param inputStream * @return */ private String readStr(InputStream inputStream) { String json = null;// 返回值 // 输出流 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // 缓存数组 byte[] b = new byte[1024]; int len = 0; try { while ((len = inputStream.read(b)) != -1) {// 当-1读取结束 outputStream.write(b, 0, len);// 写入输出流 } // 转化字符串,注意编码格式 json = new String(outputStream.toByteArray(), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } finally {// 记住关闭流 try { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return json; } }MainActivity
package com.bwie.action.banner; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private TextView time; private int COUNT=5; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (COUNT==0){ Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); finish(); }else { time.setText(COUNT+"秒后跳转"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { COUNT--; handler.sendEmptyMessage(0); } }, 0, 1000); } private void initView() { time = (TextView) findViewById(R.id.activity_main_time); } }
SecondActivity
package com.bwie.action.banner; import android.app.Activity; import android.os.Bundle; import me.maxwin.view.XListView; /** * Created by ASUS on 2017/7/27. */ public class SecondActivity extends Activity implements XListView.IXListViewListener{ private XListView xListView; private final String PATH="http://api.tuchong.com/discover-app?os_api=23&device_type=MI&device_platform=android&ssmix=a&manifest_version_code=232&dpi=440&abflag=0&uuid=326132612301652&version_code=232&app_name=tuchong&version_name=2.3.2&openudid=4352d5fadf543531&resolution=1280*1000&os_version=5.8.1&ac=wifi&aid=0"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); initView(); initData(); } private void initData() { LisrViewTask lisrViewTask = new LisrViewTask(); lisrViewTask.execute(PATH,xListView,SecondActivity.this); } private void initView() { xListView = (XListView) findViewById(R.id.xlistview); } @Override public void onRefresh() { } @Override public void onLoadMore() { } }XListviewAdapter
package com.bwie.action.banner; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; /** * Created by ASUS on 2017/7/27. */ public class XListViewAdapter extends BaseAdapter { private Context context; private List<Bean.HotEventsEntity> list; private ViewHolder1 holder1; private ViewHolder2 holder2; private int TYPE1 = 0; private int TYPE2 = 1; public XListViewAdapter(List<Bean.HotEventsEntity> 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 int getItemViewType(int position) { return position % getViewTypeCount(); } @Override public int getViewTypeCount() { return 2; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder1 holder1; ViewHolder2 holder2; if (convertView == null) { holder1 = new ViewHolder1(); holder2 = new ViewHolder2(); int type = getItemViewType(position); if (type == TYPE1) { convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null); holder1.title = (TextView) convertView.findViewById(R.id.title); holder1.img = (ImageView) convertView.findViewById(R.id.img); convertView.setTag(holder1); } else if (type == TYPE2) { convertView = LayoutInflater.from(context).inflate(R.layout.list_item2, null); holder2.title = (TextView) convertView.findViewById(R.id.listview_item2_title); holder2.end_at = (TextView) convertView.findViewById(R.id.listview_item2_end_at); convertView.setTag(holder2); } if (type == TYPE1) { holder1 = (ViewHolder1) convertView.getTag(); ; } else if (type == TYPE2) { holder2 = (ViewHolder2) convertView.getTag(); ; } if (type == TYPE1) { holder1.title.setText(list.get(position).getTitle()); List<String> images = list.get(position).getImages(); for (String ss : images) { ImageLoader.getInstance().displayImage(ss, holder1.img); } ; } else if (type == TYPE2) { holder2.title.setText(list.get(position).getTitle()); holder2.end_at.setText(list.get(position).getEnd_at()); } } return convertView; } class ViewHolder1 { TextView title; ImageView img; } class ViewHolder2 { TextView title; TextView end_at; } }