DrawerLayout
aciivivity
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.a11561.exam19.MainActivity" android:id="@+id/mydrawer"> <LinearLayout android:id="@+id/mains" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/main_listview" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout> <LinearLayout android:id="@+id/left02" android:layout_gravity="start" android:orientation="vertical" android:layout_width="200dp" android:background="#fff" android:layout_height="match_parent"> <ListView android:id="@+id/left_listview" android:layout_width="wrap_content" android:layout_height="match_parent"> </ListView> </LinearLayout> </android.support.v4.widget.DrawerLayout>
left_sty
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <ImageView android:id="@+id/images" android:src="@mipmap/ic_launcher" android:layout_width="50dp" android:padding="5dp" android:layout_height="50dp" /> <TextView android:id="@+id/textss" android:text="商品" android:textSize="20dp" android:padding="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
main_style
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <ImageView android:id="@+id/m_images" android:src="@mipmap/ic_launcher" android:layout_width="50dp" android:padding="5dp" android:layout_marginLeft="100dp" android:layout_height="50dp" /> <TextView android:id="@+id/m_textss" android:text="商品" android:textSize="20dp" android:padding="5dp" android:layout_marginLeft="100dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
main_Datas
package com.example.a11561.exam19; import java.util.List; /** * Created by 11561 on 2018/4/20. */ public class main_Datas { /** * msg : 获取子分类成功 * code : 0 * data : [{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"月饼","pcid":1,"pscid":1},{"icon":"http://120.27.23.105/images/icon.png","name":"坚果炒货","pcid":1,"pscid":2},{"icon":"http://120.27.23.105/images/icon.png","name":"糖巧","pcid":1,"pscid":3},{"icon":"http://120.27.23.105/images/icon.png","name":"休闲零食","pcid":1,"pscid":4},{"icon":"http://120.27.23.105/images/icon.png","name":"肉干肉脯","pcid":1,"pscid":5},{"icon":"http://120.27.23.105/images/icon.png","name":"饼干蛋糕","pcid":1,"pscid":6},{"icon":"http://120.27.23.105/images/icon.png","name":"蜜饯果干","pcid":1,"pscid":7},{"icon":"http://120.27.23.105/images/icon.png","name":"无糖食品","pcid":1,"pscid":8}],"name":"休闲零食","pcid":"1"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"新鲜水果","pcid":2,"pscid":9},{"icon":"http://120.27.23.105/images/icon.png","name":"海鲜水产","pcid":2,"pscid":10},{"icon":"http://120.27.23.105/images/icon.png","name":"精选肉类","pcid":2,"pscid":11},{"icon":"http://120.27.23.105/images/icon.png","name":"蛋类","pcid":2,"pscid":12},{"icon":"http://120.27.23.105/images/icon.png","name":"新鲜蔬菜","pcid":2,"pscid":13},{"icon":"http://120.27.23.105/images/icon.png","name":"冷冻食品","pcid":2,"pscid":14},{"icon":"http://120.27.23.105/images/icon.png","name":"饮品甜品","pcid":2,"pscid":15},{"icon":"http://120.27.23.105/images/icon.png","name":"大闸蟹","pcid":2,"pscid":16}],"name":"京东生鲜","pcid":"2"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"大米","pcid":3,"pscid":21},{"icon":"http://120.27.23.105/images/icon.png","name":"面粉","pcid":3,"pscid":22},{"icon":"http://120.27.23.105/images/icon.png","name":"杂粮","pcid":3,"pscid":23},{"icon":"http://120.27.23.105/images/icon.png","name":"食用油","pcid":3,"pscid":24},{"icon":"http://120.27.23.105/images/icon.png","name":"调味品","pcid":3,"pscid":25},{"icon":"http://120.27.23.105/images/icon.png","name":"方便速食","pcid":3,"pscid":26},{"icon":"http://120.27.23.105/images/icon.png","name":"有机食品","pcid":3,"pscid":27}],"name":"粮油调味","pcid":"3"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"饮用水","pcid":4,"pscid":28},{"icon":"http://120.27.23.105/images/icon.png","name":"饮料","pcid":4,"pscid":29},{"icon":"http://120.27.23.105/images/icon.png","name":"牛奶乳品","pcid":4,"pscid":30},{"icon":"http://120.27.23.105/images/icon.png","name":"名茶","pcid":4,"pscid":31},{"icon":"http://120.27.23.105/images/icon.png","name":"蜂蜜","pcid":4,"pscid":32}],"name":"水饮茗茶","pcid":"4"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"白酒","pcid":5,"pscid":33},{"icon":"http://120.27.23.105/images/icon.png","name":"葡萄酒","pcid":5,"pscid":34},{"icon":"http://120.27.23.105/images/icon.png","name":"洋酒","pcid":5,"pscid":35},{"icon":"http://120.27.23.105/images/icon.png","name":"啤酒","pcid":5,"pscid":36},{"icon":"http://120.27.23.105/images/icon.png","name":"黄酒","pcid":5,"pscid":37},{"icon":"http://120.27.23.105/images/icon.png","name":"陈年老酒","pcid":5,"pscid":38}],"name":"中外名酒","pcid":"5"}] */ 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; } public static class DataBean { /** * cid : 1 * list : [{"icon":"http://120.27.23.105/images/icon.png","name":"月饼","pcid":1,"pscid":1},{"icon":"http://120.27.23.105/images/icon.png","name":"坚果炒货","pcid":1,"pscid":2},{"icon":"http://120.27.23.105/images/icon.png","name":"糖巧","pcid":1,"pscid":3},{"icon":"http://120.27.23.105/images/icon.png","name":"休闲零食","pcid":1,"pscid":4},{"icon":"http://120.27.23.105/images/icon.png","name":"肉干肉脯","pcid":1,"pscid":5},{"icon":"http://120.27.23.105/images/icon.png","name":"饼干蛋糕","pcid":1,"pscid":6},{"icon":"http://120.27.23.105/images/icon.png","name":"蜜饯果干","pcid":1,"pscid":7},{"icon":"http://120.27.23.105/images/icon.png","name":"无糖食品","pcid":1,"pscid":8}] * name : 休闲零食 * pcid : 1 */ private String cid; private String name; private String pcid; private List<ListBean> list; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPcid() { return pcid; } public void setPcid(String pcid) { this.pcid = pcid; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { /** * icon : http://120.27.23.105/images/icon.png * name : 月饼 * pcid : 1 * pscid : 1 */ private String icon; private String name; private int pcid; private int pscid; public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPcid() { return pcid; } public void setPcid(int pcid) { this.pcid = pcid; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } } } }
MainActivity
package com.example.a11561.exam19; import android.content.Intent; import android.os.AsyncTask; import android.provider.Contacts; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.google.gson.Gson; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; 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.net.URLConnection; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView main_listview,left_listview; private String path="http://120.27.23.105/product/getCatagory"; private List<LeftDatas.DataBean> data; private DrawerLayout myDrawer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取资源ID myDrawer = findViewById(R.id.mydrawer); main_listview=findViewById(R.id.main_listview); left_listview=findViewById(R.id.left_listview); MyTasks myTasks=new MyTasks(); myTasks.execute(path); //点击菜单 left_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //点击菜单的时候拿到cid int cid = data.get(i).getCid(); //拿到cid 后进行拼接 String m_path="http://120.27.23.105/product/getProductCatagory?cid="+cid; //主页面的请求数据 Main_datas mdatas=new Main_datas(); mdatas.execute(m_path); //关闭菜单 myDrawer.closeDrawer(Gravity.START); } }); } //菜单 class MyTasks extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... strings) { try { URL url=new URL(strings[0]); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(5000); urlConnection.setConnectTimeout(5000); if(urlConnection.getResponseCode()==200){ InputStream inputStream = urlConnection.getInputStream(); String s = getStrings(inputStream); return s; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); LeftDatas leftDatas = gson.fromJson(s, LeftDatas.class); data = leftDatas.getData(); LeftAdaper adaper=new LeftAdaper(MainActivity.this, data); left_listview.setAdapter(adaper); } } //主内容区数据 class Main_datas extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... strings) { HttpClient httpClient=new DefaultHttpClient(); HttpGet httpGet=new HttpGet(strings[0]); try { HttpResponse response = httpClient.execute(httpGet); if(response.getStatusLine().getStatusCode()==200){ HttpEntity entity = response.getEntity(); String s = EntityUtils.toString(entity); return s; } } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); main_Datas fromJson = gson.fromJson(s, main_Datas.class); List<main_Datas.DataBean.ListBean> list = fromJson.getData().get(0).getList(); MainAdaper adapers=new MainAdaper(MainActivity.this,list); main_listview.setAdapter(adapers); } } private String getStrings(InputStream stream) { ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(); int len; byte[] bytes=new byte[1024]; try { while ((len=stream.read(bytes))!=-1){ byteArrayOutputStream.write(bytes,0,len); } } catch (IOException e) { e.printStackTrace(); } return byteArrayOutputStream.toString(); } }
MainAdaper
package com.example.a11561.exam19; import android.content.Context; import android.graphics.Bitmap; 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.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import java.util.List; /** * Created by 11561 on 2018/4/20. */ public class MainAdaper extends BaseAdapter{ private Context context; private List<main_Datas.DataBean.ListBean> list; private DisplayImageOptions options; public MainAdaper(Context context, List<main_Datas.DataBean.ListBean> list) { this.context = context; this.list = list; options=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 .build(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder; if(view==null){ view = View.inflate(context, R.layout.main_style, null); holder=new ViewHolder(); holder.mtests=view.findViewById(R.id.m_textss); holder.mimageView=view.findViewById(R.id.m_images); view.setTag(holder); }else{ holder= (ViewHolder) view.getTag(); } holder.mtests.setText(list.get(i).getName()); ImageLoader.getInstance().displayImage(list.get(i).getIcon(),holder.mimageView,options); return view; } class ViewHolder{ TextView mtests; ImageView mimageView; } }
Myapp
package com.example.a11561.exam19; import android.app.Application; import android.os.Environment; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import java.io.File; /** * Created by 11561 on 2018/4/20. */ public class Myapp extends Application{ @Override public void onCreate() { super.onCreate(); //自定义sd卡路径 File cachefile=new File(Environment.getExternalStorageDirectory().getPath()+"/abc"); //初使化 ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽 .threadPoolSize(2)//线程池的数量 .threadPriority(4) .memoryCacheSize(30*1024*1024)//设置内存缓存区大小 .diskCacheSize(30*1024*1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiscCache(cachefile))//自定义缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(configuration); } }
LeftAdaper
package com.example.a11561.exam19; import android.content.Context; import android.graphics.Bitmap; 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.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import java.util.List; /** * Created by 11561 on 2018/4/20. */ public class LeftAdaper extends BaseAdapter{ private Context context; private List<LeftDatas.DataBean> list; private DisplayImageOptions options; public LeftAdaper(Context context, List<LeftDatas.DataBean> list) { this.context = context; this.list = list; options=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 .build(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { Viewholder holder; if(view==null){ view= View.inflate(context, R.layout.left_style, null); holder=new Viewholder(); holder.tests=view.findViewById(R.id.textss); holder.imageView= view.findViewById(R.id.images); view.setTag(holder); }else{ holder= (Viewholder) view.getTag(); } holder.tests.setText(list.get(i).getName()); ImageLoader.getInstance().displayImage(list.get(i).getIcon(),holder.imageView,options); return view; } class Viewholder{ TextView tests; ImageView imageView; } }
LeftDatas
package com.example.a11561.exam19; import java.util.List; /** * Created by 11561 on 2018/4/20. */ public class LeftDatas { /** * 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; } 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; } } }