//activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.test.homedemo.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="150dp"> <com.youth.banner.Banner android:id="@+id/ban" android:layout_width="match_parent" android:layout_height="match_parent"> </com.youth.banner.Banner> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="0dp" android:layout_height="20dp" android:layout_gravity="center" android:layout_weight="1" android:src="@drawable/a_q" /> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="9" android:id="@+id/sy_ss" /> <ImageView android:layout_width="0dp" android:layout_height="20dp" android:layout_gravity="center" android:src="@drawable/a_n" android:layout_weight="1" /> </LinearLayout> </FrameLayout> <ImageView android:layout_width="match_parent" android:layout_height="50dp" android:background="@mipmap/ic_launcher" android:src="@mipmap/ic_launcher" /> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/recy"> </android.support.v7.widget.RecyclerView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="京东秒杀" android:textSize="15sp" /> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ms_recy"> </android.support.v7.widget.RecyclerView> </LinearLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tj_recy"> </android.support.v7.widget.RecyclerView> </LinearLayout>
//item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="10dp"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/ite_iv" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/item_tv" /> </LinearLayout>
//ms_item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/ms_iv" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ms_tv" /> </LinearLayout>
//tj_item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tj_iv" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tj_tv" /> </LinearLayout>
//MainActivity
public class MainActivity extends AppCompatActivity { Banner banner; List<String> list; Handler handler=new Handler(); RecyclerView recy,ms_recy,tj_recy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); banner= (Banner) findViewById(R.id.ban); recy= (RecyclerView) findViewById(R.id.recy); ms_recy= (RecyclerView) findViewById(R.id.ms_recy); tj_recy= (RecyclerView) findViewById(R.id.tj_recy); list=new ArrayList<String>(); OkHttp3Util.doGet("http://120.27.23.105/ad/getAd", new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ final String string = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { Gson gson=new Gson(); SuperClass superClass = gson.fromJson(string, SuperClass.class); List<SuperClass.DataBean> data = superClass.getData(); // for (int i=0;i<data.size();i++){ // String icon = data.get(i).getIcon(); // list.add(icon); // } banner.setImageLoader( new Imageloader()); Log.i("Tag000", "onCreate: "+list.toString()); banner.setImages(list); banner.start(); SuperClass.MiaoshaBean miaosha = superClass.getMiaosha(); List<SuperClass.MiaoshaBean.ListBeanX> list = miaosha.getList(); ms_Myadapter ms_myadapter = new ms_Myadapter(MainActivity.this, list); ms_recy.setLayoutManager(new GridLayoutManager(MainActivity.this, 2, LinearLayoutManager.HORIZONTAL, false)); ms_recy.setAdapter(ms_myadapter); SuperClass.TuijianBean tuijian = superClass.getTuijian(); List<SuperClass.TuijianBean.ListBean> list1 = tuijian.getList(); tuijian_Myadapter tuijian_myadapter = new tuijian_Myadapter(MainActivity.this, list1); tj_recy.setLayoutManager(new GridLayoutManager(MainActivity.this, 2, LinearLayoutManager.VERTICAL, false)); tj_recy.setAdapter(tuijian_myadapter); } }); } } }); OkHttp3Util.doGet("http://120.27.23.105/product/getCatagory", new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, final Response response) throws IOException { if(response.isSuccessful()){ final String string = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { Gson gson=new Gson(); BeanClass beanClass = gson.fromJson(string, BeanClass.class); List<BeanClass.DataBean> data = beanClass.getData(); Myadapter myadapter=new Myadapter(MainActivity.this, data); recy.setLayoutManager(new GridLayoutManager(MainActivity.this, 2, LinearLayoutManager.HORIZONTAL, false)); recy.setAdapter(myadapter); } }); } } }); } }
adapter文件夹下//ms_Myadapter
public class ms_Myadapter extends RecyclerView.Adapter<ms_Myadapter.viewholder> { Context context; List<SuperClass.MiaoshaBean.ListBeanX> list; public ms_Myadapter(Context context, List<SuperClass.MiaoshaBean.ListBeanX> list) { this.context = context; this.list = list; } @Override public viewholder onCreateViewHolder(ViewGroup parent, int viewType) { viewholder viewholder=new viewholder(LayoutInflater.from(context).inflate(R.layout.ms_item,parent,false)); return viewholder; } @Override public void onBindViewHolder(viewholder holder, int position) { String images = list.get(position).getImages(); String[] split = images.split("\\|"); ImageLoader.getInstance().displayImage(split[0],holder.iv); holder.tv.setText(list.get(position).getSubhead()); } @Override public int getItemCount() { return list.size(); } class viewholder extends RecyclerView.ViewHolder { ImageView iv; TextView tv; public viewholder(View itemView) { super(itemView); iv=itemView.findViewById(R.id.ms_iv); tv=itemView.findViewById(R.id.ms_tv); } } }
//Myadapter
public class Myadapter extends RecyclerView.Adapter<Myadapter.viewholder>{ Context context; List<BeanClass.DataBean> data; public Myadapter(Context context, List<BeanClass.DataBean> data) { this.context = context; this.data = data; } @Override public viewholder onCreateViewHolder(ViewGroup parent, int viewType) { viewholder viewholder=new viewholder(LayoutInflater.from(context).inflate(R.layout.item,parent,false)); return viewholder; } @Override public void onBindViewHolder(viewholder holder, int position) { ImageLoader.getInstance().displayImage(data.get(position).getIcon(),holder.iv); holder.tv.setText(data.get(position).getName()); } @Override public int getItemCount() { return data.size(); } class viewholder extends RecyclerView.ViewHolder { ImageView iv; TextView tv; public viewholder(View itemView) { super(itemView); iv=itemView.findViewById(R.id.ite_iv); tv=itemView.findViewById(R.id.item_tv); } } }
//tuijian_Myadapter
public class tuijian_Myadapter extends RecyclerView.Adapter<tuijian_Myadapter.viewholder> { Context context; List<SuperClass.TuijianBean.ListBean> list1; public tuijian_Myadapter(Context context, List<SuperClass.TuijianBean.ListBean> list1) { this.context = context; this.list1 = list1; } @Override public viewholder onCreateViewHolder(ViewGroup parent, int viewType) { viewholder viewholder=new viewholder(LayoutInflater.from(context).inflate(R.layout.tj_item,parent,false)); return viewholder; } @Override public void onBindViewHolder(viewholder holder, int position) { String images = list1.get(position).getImages(); String[] split = images.split("\\|"); ImageLoader.getInstance().displayImage(split[0],holder.iv); holder.tv.setText(list1.get(position).getSubhead()); } @Override public int getItemCount() { return list1.size(); } class viewholder extends RecyclerView.ViewHolder { ImageView iv; TextView tv; public viewholder(View itemView) { super(itemView); iv=itemView.findViewById(R.id.tj_iv); tv=itemView.findViewById(R.id.tj_tv); } } }
app文件夹下//Image
public class Image extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration imageLoaderConfiguration = ImageLoaderConfiguration.createDefault(this); ImageLoader.getInstance().init(imageLoaderConfiguration); } }
util文件夹下//Imageloader
public class Imageloader extends ImageLoader { @Override public void displayImage(Context context, Object path, ImageView imageView) { Glide.with(context).load(path).into(imageView); } }
//tuijian
public class tuijian { }
//OkHttp3Util
public class OkHttp3Util { private static OkHttpClient okHttpClient = null; private OkHttp3Util() { } public static OkHttpClient getInstance() { if (okHttpClient == null) { //加同步安全 synchronized (OkHttp3Util.class) { if (okHttpClient == null) { //okhttp可以缓存数据....指定缓存路径 File sdcache = new File(Environment.getExternalStorageDirectory(), "cache"); //指定缓存大小 int cacheSize = 10 * 1024 * 1024; okHttpClient = new OkHttpClient.Builder()//构建器 .connectTimeout(15, TimeUnit.SECONDS)//连接超时 .writeTimeout(20, TimeUnit.SECONDS)//写入超时 .readTimeout(20, TimeUnit.SECONDS)//读取超时 .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))//设置缓存 .build(); } } } return okHttpClient; } /** * get请求 * 参数1 url * 参数2 回调Callback */ public static void doGet(String oldUrl, Callback callback) { //创建OkHttpClient请求对象 OkHttpClient okHttpClient = getInstance(); //创建Request Request request = new Request.Builder().url(oldUrl).build(); //得到Call对象 Call call = okHttpClient.newCall(request); //执行异步请求 call.enqueue(callback); } /** * post请求 * 参数1 url * 参数2 Map<String, String> params post请求的时候给服务器传的数据 * add..("","") * add() */ public static void doPost(String url, Map<String, String> params, Callback callback) { //创建OkHttpClient请求对象 OkHttpClient okHttpClient = getInstance(); //3.x版本post请求换成FormBody 封装键值对参数 FormBody.Builder builder = new FormBody.Builder(); //遍历集合,,,map集合遍历方式 for (String key : params.keySet()) { builder.add(key, params.get(key)); } //创建Request....formBody...new formBody.Builder()...add()....build() Request request = new Request.Builder().url(url).post(builder.build()).build(); Call call = okHttpClient.newCall(request); call.enqueue(callback); } /** * post请求上传文件....包括图片....流的形式传任意文件... * 参数1 url * file表示上传的文件 * fileName....文件的名字,,例如aaa.jpg * params ....传递除了file文件 其他的参数放到map集合 */ public static void uploadFile(String url, File file, String fileName,Map<String,String> params,Callback callback) { //创建OkHttpClient请求对象 OkHttpClient okHttpClient = getInstance(); //MultipartBody多功能的请求实体对象,,,formBody只能传表单形式的数据 MultipartBody.Builder builder = new MultipartBody.Builder(); builder.setType(MultipartBody.FORM); //参数 if (params != null){ for (String key : params.keySet()){ builder.addFormDataPart(key,params.get(key)); } } //文件...参数name指的是请求路径中所接受的参数...如果路径接收参数键值是fileeeee,此处应该改变 builder.addFormDataPart("file",fileName, RequestBody.create(MediaType.parse("application/octet-stream"),file)); //构建 MultipartBody multipartBody = builder.build(); //创建Request Request request = new Request.Builder().url(url).post(multipartBody).build(); //得到Call Call call = okHttpClient.newCall(request); //执行请求 call.enqueue(callback); } /** * Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"} * 参数一:请求Url * 参数二:请求的JSON * 参数三:请求回调 */ public static void doPostJson(String url, String jsonParams, Callback callback) { RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams); Request request = new Request.Builder().url(url).post(requestBody).build(); Call call = getInstance().newCall(request); call.enqueue(callback); } /** * 下载文件 以流的形式把apk写入的指定文件 得到file后进行安装 * 参数er:请求Url * 参数san:保存文件的文件夹....download */ public static void download(final Activity context, final String url, final String saveDir) { Request request = new Request.Builder().url(url).build(); Call call = getInstance().newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { //com.orhanobut.logger.Logger.e(e.getLocalizedMessage()); } @Override public void onResponse(Call call, final Response response) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len = 0; FileOutputStream fos = null; try { is = response.body().byteStream();//以字节流的形式拿回响应实体内容 //apk保存路径 final String fileDir = isExistDir(saveDir); //文件 File file = new File(fileDir, getNameFromUrl(url)); fos = new FileOutputStream(file); while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); context.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(context, "下载成功:" + fileDir + "," + getNameFromUrl(url), Toast.LENGTH_SHORT).show(); } }); //apk下载完成后 调用系统的安装方法 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) is.close(); if (fos != null) fos.close(); } } }); } /** * 判断下载目录是否存在......并返回绝对路径 * * @param saveDir * @return * @throws IOException */ public static String isExistDir(String saveDir) throws IOException { // 下载位置 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir); if (!downloadFile.mkdirs()) { downloadFile.createNewFile(); } String savePath = downloadFile.getAbsolutePath(); Log.e("savePath", savePath); return savePath; } return null; } /** * @param url * @return 从下载连接中解析出文件名 */ private static String getNameFromUrl(String url) { return url.substring(url.lastIndexOf("/") + 1); } }
添加依赖//
compile 'com.android.support.test:runner:0.5' compile 'com.android.support.test.espresso:espresso-core:2.2.2' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.youth.banner:banner:1.4.9' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.squareup.okhttp3:okhttp:3.9.1' compile 'com.squareup.okio:okio:1.13.0' compile 'com.google.code.gson:gson:2.6.2' compile 'com.github.bumptech.glide:glide:3.6.1' compile 'com.hjm:BottomTabBar:1.1.1' compile 'com.jcodecraeer:xrecyclerview:1.5.2' compile 'org.greenrobot:eventbus:3.0.0'
添加权限//
<uses-permission android:name="android.permission.INTERNET"/> <application android:name="com.bwie.test.app.Image" </application>
//BeanClass
public class BeanClass { /** * msg : * code : 0 * data : [{"cid":1,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/shop.png","ishome":1,"name":"京东超市"},{"cid":2,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/qqg.png","ishome":1,"name":"全球购"},{"cid":3,"createtime":"2017-10-10T19:45:11","icon":"http://120.27.23.105/images/category/phone.png","ishome":1,"name":"手机数码"},{"cid":5,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/man.png","ishome":1,"name":"男装"},{"cid":6,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girl.png","ishome":1,"name":"女装"},{"cid":7,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/manshoe.png","ishome":1,"name":"男鞋"},{"cid":8,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girlshoe.png","ishome":1,"name":"女鞋"},{"cid":9,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/shirt.png","ishome":1,"name":"内衣配饰"},{"cid":10,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/bag.png","ishome":1,"name":"箱包手袋"},{"cid":11,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/beauty.png","ishome":1,"name":"美妆个护"},{"cid":12,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/jewel.png","ishome":1,"name":"钟表珠宝"},{"cid":13,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/luxury.png","ishome":1,"name":"奢侈品"},{"cid":14,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/computer.png","ishome":1,"name":"电脑办公"},{"cid":15,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"家用电器"},{"cid":16,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"食品生鲜"},{"cid":17,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"酒水饮料"},{"cid":18,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"母婴童装"},{"cid":19,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"玩具乐器"},{"cid":20,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"医药保健"}] */ private String msg; private String code; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } @Override public String toString() { return "BeanClass{" + "msg='" + msg + '\'' + ", code='" + code + '\'' + ", data=" + data + '}'; } public static class DataBean { /** * cid : 1 * createtime : 2017-10-10T19:41:39 * icon : http://120.27.23.105/images/category/shop.png * ishome : 1 * name : 京东超市 */ private int cid; private String createtime; private String icon; private int ishome; private String name; public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public int getIshome() { return ishome; } public void setIshome(int ishome) { this.ishome = ishome; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "DataBean{" + "cid=" + cid + ", createtime='" + createtime + '\'' + ", icon='" + icon + '\'' + ", ishome=" + ishome + ", name='" + name + '\'' + '}'; } } }