ok封装可参考之前博客
1.先导包
compile 'com.google.code.gson:gson:2.8.2' compile 'com.jcodecraeer:xrecyclerview:1.3.2' compile 'com.android.support:recyclerview-v7:27+' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.squareup.okio:okio:1.11.0' compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' compile 'com.github.bumptech.glide:glide:3.5.2' implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.amap.api:location:latest.integration'
2.添加权限
<uses-permission android:name="android.permission.INTERNET" />
3.编写xml
在values文件夹下编写 dimens
<resources> <dimen name="left_space_pading">12dp</dimen> </resources>
编写attrs 用来做自定义view的组合控件
<declare-styleable name="MyApp"> <attr name="title_text" format="string"></attr> <attr name="title_color" format="color"></attr> <attr name="title_text_right" format="string"></attr> <attr name="title_color_right" format="color"></attr> </declare-styleable>
//分类的xml
actiity_main2
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".view.Main2Activity" android:orientation="vertical"> <TextView android:id="@+id/main2_tv" android:layout_width="match_parent" android:layout_height="30dp" android:textSize="20sp" android:text="地址"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ListView android:id="@+id/ledt_lv" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:background="@color/colorPrimary"> </ListView> <ListView android:id="@+id/right_lv" android:layout_weight="3" android:layout_width="0dp" android:layout_height="match_parent" android:background="@color/colorAccent"> </ListView> </LinearLayout> </LinearLayout>
left_item
<?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/left_item_text" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:text="商品" android:textSize="30sp"> </TextView>
right_item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/right_title" android:textSize="20sp" android:text="标题" android:layout_width="match_parent" android:layout_height="wrap_content" /> <com.example.thinkpad.wsjykmn.view.myGridview android:id="@+id/right_gv" android:numColumns="3" android:layout_width="match_parent" android:layout_height="wrap_content"></com.example.thinkpad.wsjykmn.view.myGridview> </LinearLayout>
gv_item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/gv_pic" android:layout_width="60dp" android:layout_height="60dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/gv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商品名称" android:textSize="15sp" /> </LinearLayout>
//自定义标题xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:background="#bbfeee" > <TextView android:id="@+id/gw_bia_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="购物车" android:textSize="30sp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/gw_right_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_alignParentRight="true" android:text="编辑" /> </RelativeLayout>//购物车的xml
actiity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".view.MainActivity" android:orientation="vertical"> <com.example.thinkpad.wsjykmn.app.MyApp android:layout_width="match_parent" android:layout_height="40dp" app:title_text="购物车" app:title_color="@color/colorAccent" app:title_text_right="编辑" app:title_color_right="@color/colorPrimary"> </com.example.thinkpad.wsjykmn.app.MyApp> <ExpandableListView android:id="@+id/el_cart" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9"/> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#eeeeee" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_care_all_select" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选"/> <TextView android:id="@+id/tv_cart_total_price" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="20dp" android:text="合计:¥0.00"/> <Button android:id="@+id/btn_cart_pay" android:layout_width="100dp" android:layout_height="wrap_content" android:text="去结算(0)"/> </LinearLayout> </LinearLayout>
add_remove_view_layput
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="60dp" android:layout_height="30dp" android:padding="2dp" android:gravity="center_vertical" android:background="#99000000" android:layout_gravity="center_vertical"> <TextView android:background="#ffffff" android:layout_weight="1" android:layout_width="0dp" android:id="@+id/sun_tv" android:gravity="center" android:text="-" android:textSize="16sp" android:layout_height="match_parent" /> <TextView android:text="1" android:background="#ffffffff" android:layout_marginLeft="2dp" android:layout_weight="1" android:id="@+id/product_number_tv" android:layout_width="0dp" android:layout_height="match_parent" android:gravity="center"/> <TextView android:background="#ffffff" android:layout_weight="1" android:layout_marginLeft="2dp" android:layout_width="0dp" android:id="@+id/add_tv" android:gravity="center" android:text="+" android:textSize="16sp" android:layout_height="match_parent" /> </LinearLayout>
item_cart_child
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="120dp" android:gravity="center_vertical" android:paddingLeft="@dimen/left_space_pading"> <CheckBox android:id="@+id/child_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/product_icon_iv" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginLeft="20dp" android:scaleType="centerCrop" android:src="@color/colorPrimary" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/left_space_pading" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/product_title_name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="2" android:text="商品标题" /> <TextView android:id="@+id/product_price_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="¥0.0" /> </LinearLayout> <com.example.thinkpad.wsjykmn.app.MyAddSubView android:id="@+id/add_remove_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" /> </LinearLayout>
item_cart_parent
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center_vertical" android:paddingLeft="@dimen/textandiconmargin"> <CheckBox android:id="@+id/seller_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/seller_name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp"/> </LinearLayout>
//现在开始上实现类
//分类
MyAdapterl
package com.example.thinkpad.wsjykmn.adapters; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.example.thinkpad.wsjykmn.R; import com.example.thinkpad.wsjykmn.model.bean.LeftBean; import java.util.List; public class MyAdapterl extends BaseAdapter { private Context context; private List<LeftBean.DataBean> list; public MyAdapterl(Context context,List<LeftBean.DataBean> list){ this.context=context; this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { MyViewHolder myViewHolder=null; if(convertView==null){ convertView= View.inflate(context, R.layout.left_item,null); TextView textView = convertView.findViewById(R.id.left_item_text); myViewHolder=new MyViewHolder(textView); convertView.setTag(myViewHolder); }else{ myViewHolder = (MyViewHolder) convertView.getTag(); } myViewHolder.getTextView().setText(list.get(position).getName()); return convertView; } class MyViewHolder{ private TextView textView; public MyViewHolder(TextView textView) { this.textView = textView; } public TextView getTextView() { return textView; } public void setTextView(TextView textView) { this.textView = textView; } } }
MyAdapterl2
package com.example.thinkpad.wsjykmn.adapters; import android.content.Context; import android.content.Intent; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.TextView; import android.widget.Toast; import com.example.thinkpad.wsjykmn.view.MainActivity; import com.example.thinkpad.wsjykmn.R; import com.example.thinkpad.wsjykmn.model.bean.Rightbean; import java.util.List; public class MyAdapterl2 extends BaseAdapter{ private Context context; private List<Rightbean.DataBean> list; public MyAdapterl2(Context context,List<Rightbean.DataBean> list){ this.context=context; this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { MyViewHolder2 myViewHolder2=null; if(convertView==null){ convertView=View.inflate(context, R.layout.right_item,null); TextView right_title = convertView.findViewById(R.id.right_title); GridView right_gv= convertView.findViewById(R.id.right_gv); myViewHolder2= new MyViewHolder2(right_title,right_gv); convertView.setTag(myViewHolder2); }else{ myViewHolder2 = (MyViewHolder2) convertView.getTag(); } //赋值 myViewHolder2.getRight_tv().setText(list.get(position).getName()); List<Rightbean.DataBean.ListBean> list = this.list.get(position).getList(); MyAdapter3 myAdapter3 = new MyAdapter3(context,list); myViewHolder2.getRight_gv().setAdapter(myAdapter3); myViewHolder2.getRight_gv().setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(context,"Postiion"+position,Toast.LENGTH_SHORT).show(); Intent intent = new Intent(context, MainActivity.class); context.startActivity(intent); } }); return convertView; } public class MyViewHolder2{ private TextView right_tv; private GridView right_gv; public MyViewHolder2(TextView right_tv, GridView right_gv) { this.right_tv = right_tv; this.right_gv = right_gv; } public TextView getRight_tv() { return right_tv; } public void setRight_tv(TextView right_tv) { this.right_tv = right_tv; } public GridView getRight_gv() { return right_gv; } public void setRight_gv(GridView right_gv) { this.right_gv = right_gv; } } }
MyAdapter3
package com.example.thinkpad.wsjykmn.adapters; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.thinkpad.wsjykmn.R; import com.example.thinkpad.wsjykmn.model.bean.Rightbean; import java.util.List; public class MyAdapter3 extends BaseAdapter{ private Context context; private List<Rightbean.DataBean.ListBean> list; public MyAdapter3(Context context,List<Rightbean.DataBean.ListBean> list){ this.context=context; this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { MyViewHodel3 myViewHodel3; if(convertView==null){ convertView=View.inflate(context, R.layout.gv_item,null); ImageView gv_pic= convertView.findViewById(R.id.gv_pic); TextView gv_name = convertView.findViewById(R.id.gv_name); myViewHodel3= new MyViewHodel3(gv_pic,gv_name); convertView.setTag(myViewHodel3); }else{ myViewHodel3= (MyViewHodel3) convertView.getTag(); } //赋值 myViewHodel3.getGv_name().setText(list.get(position).getName()); Glide.with(context).load(list.get(position).getIcon()).into(myViewHodel3.getGv_pic()); return convertView; } public class MyViewHodel3{ ImageView gv_pic; TextView gv_name; public MyViewHodel3(ImageView gv_pic, TextView gv_name) { this.gv_pic = gv_pic; this.gv_name = gv_name; } public ImageView getGv_pic() { return gv_pic; } public void setGv_pic(ImageView gv_pic) { this.gv_pic = gv_pic; } public TextView getGv_name() { return gv_name; } public void setGv_name(TextView gv_name) { this.gv_name = gv_name; } } }
//model
HttpConfig
package com.example.thinkpad.wsjykmn.model.util; public class HttpConfig { public static String left_url = "https://www.zhaoapi.cn/product/getCatagory"; public static String right_url = "https://www.zhaoapi.cn/product/getProductCatagory"; public static String gowu="https://www.zhaoapi.cn/product/getCarts"; }
GetRightListener
package com.example.thinkpad.wsjykmn.model; public interface GetRightListener { //右边数据成功,失败方法 void getLeftSuccess(String json); void getLeftError(Exception error); }
GetLeftListener
package com.example.thinkpad.wsjykmn.model; public interface GetLeftListener { //左边数据成功失败的方法 void getLeftSuccess(String json); void getLeftError(Exception error); }
IModel
package com.example.thinkpad.wsjykmn.model; import java.util.Map; public interface IModel { //获取左侧列表 void getLeftList(String url, Map<String,String> map, GetLeftListener getLeftListener); //获取右侧列表数据 void getRightList(String url, Map<String,String> map,GetRightListener getRightListener); }
ModelImpl
package com.example.thinkpad.wsjykmn.model; import android.util.Log; import com.example.thinkpad.wsjykmn.model.util.OkHttpUtil; import java.util.Map; public class ModelImpl implements IModel { private static final String TAG = "MainActivity"; @Override public void getLeftList(String url, Map<String, String> map, final GetLeftListener getLeftListener) { OkHttpUtil instance = OkHttpUtil.getInstance(); instance.dopost(url, map, new OkHttpUtil.OkCallback() { @Override public void onFailure(Exception e) { getLeftListener.getLeftError(e); } @Override public void onResponse(String json) { getLeftListener.getLeftSuccess(json); } }); } //右边 @Override public void getRightList(String url, Map<String, String> map, final GetRightListener getRightListener) { OkHttpUtil instance = OkHttpUtil.getInstance(); instance.dopost(url,map,new OkHttpUtil.OkCallback() { @Override public void onFailure(Exception e) { getRightListener.getLeftError(e); } @Override public void onResponse(String json) { getRightListener.getLeftSuccess(json); } }); } }
//p层
IPresenter
package com.example.thinkpad.wsjykmn.pesntes; import com.example.thinkpad.wsjykmn.model.IModel; import com.example.thinkpad.wsjykmn.view.IMainview; public interface IPresenter { //显示左侧列表 void showLeftToView(IModel iModel, IMainview iMainView); //显示右侧列表 void showRightToView(IModel iModel, IMainview iMainView); }
Presenter
package com.example.thinkpad.wsjykmn.pesntes; import android.support.annotation.NonNull; import android.util.Log; import com.example.thinkpad.wsjykmn.model.GetLeftListener; import com.example.thinkpad.wsjykmn.model.GetRightListener; import com.example.thinkpad.wsjykmn.model.IModel; import com.example.thinkpad.wsjykmn.model.bean.LeftBean; import com.example.thinkpad.wsjykmn.model.bean.Rightbean; import com.example.thinkpad.wsjykmn.model.util.HttpConfig; import com.example.thinkpad.wsjykmn.view.IMainview; import com.google.gson.Gson; import java.util.Collection; import java.util.HashMap; import java.util.Map; public class Presenter implements IPresenter{ private static final String TAG = "Presenter"; @Override public void showLeftToView(IModel iModel, final IMainview iMainView) { Map<String,String> map= new HashMap<>(); iModel.getLeftList(HttpConfig.left_url, map, new GetLeftListener() { @Override public void getLeftSuccess(String json) { Gson gson = new Gson(); LeftBean leftBean = gson.fromJson(json, LeftBean.class); String code = leftBean.getCode(); if("0".equalsIgnoreCase(code)){ if (iMainView!=null){ iMainView.showLeftView(leftBean.getData()); } }else{ Log.d(TAG, "失败" + json); } } @Override public void getLeftError(Exception error) { Log.d(TAG, "getLeftError: " + error); } }); } //右侧 @Override public void showRightToView(IModel iModel, final IMainview iMainView) { Map<String,String> map= new HashMap<>(); map.put("cid",iMainView.getCid()); iModel.getRightList(HttpConfig.right_url,map, new GetRightListener() { @Override public void getLeftSuccess(String json) { Gson gson = new Gson(); Rightbean rightbean = gson.fromJson(json,Rightbean.class); String code = rightbean.getCode(); if("0".equalsIgnoreCase(code)){ iMainView.showRightView(rightbean.getData()); }else{ Log.d(TAG, "失败" + json); } } @Override public void getLeftError(Exception error) { Log.d(TAG, "getLeftError: " + error); } }); } }view
IMainview
package com.example.thinkpad.wsjykmn.view; import com.example.thinkpad.wsjykmn.model.bean.LeftBean; import com.example.thinkpad.wsjykmn.model.bean.Rightbean; import java.util.List; public interface IMainview { //显示左侧列表 void showLeftView(List<LeftBean.DataBean> list); //显示右侧 void showRightView(List<Rightbean.DataBean> list); //获取分类的cid String getCid(); }
Main2Activity
package com.example.thinkpad.wsjykmn.view; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationListener; import com.example.thinkpad.wsjykmn.R; import com.example.thinkpad.wsjykmn.adapters.MyAdapterl; import com.example.thinkpad.wsjykmn.adapters.MyAdapterl2; import com.example.thinkpad.wsjykmn.model.ModelImpl; import com.example.thinkpad.wsjykmn.model.bean.LeftBean; import com.example.thinkpad.wsjykmn.model.bean.Rightbean; import com.example.thinkpad.wsjykmn.pesntes.Presenter; import java.util.List; public class Main2Activity extends AppCompatActivity implements IMainview{ private static final String TAG = "MainActivity"; private ListView left_lv; private ListView right_lv; private List<LeftBean.DataBean> list; private String cid; private Presenter presenter; private TextView main2_tv; //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initViews(); initDatas(); } //显示数据 private void initDatas() { presenter = new Presenter(); presenter.showLeftToView(new ModelImpl(),Main2Activity.this); } private void initViews() { main2_tv = findViewById(R.id.main2_tv); left_lv = findViewById(R.id.ledt_lv); right_lv = findViewById(R.id.right_lv); left_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Main2Activity.this.cid= list.get(position).getCid()+""; presenter.showRightToView(new ModelImpl(),Main2Activity.this); } }); //初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //启动定位 mLocationClient.startLocation(); //异步获取定位结果 AMapLocationListener mAMapLocationListener = new AMapLocationListener(){ @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //解析定位结果 String city = amapLocation.getCity(); main2_tv.setText(city); } } } }; //设置定位回调监听 mLocationClient.setLocationListener(mAMapLocationListener); } //显示左侧列表 @Override public void showLeftView(List<LeftBean.DataBean> list) { this.list=list; MyAdapterl myAdapterl = new MyAdapterl(this,list); left_lv.setAdapter(myAdapterl); //显示右侧的默认值,就是cid=1 int cid = list.get(0).getCid(); Main2Activity.this.cid= cid +""; presenter.showRightToView(new ModelImpl(),Main2Activity.this); } @Override public void showRightView(List<Rightbean.DataBean> list) { Log.d(TAG, "右侧------: "+list); MyAdapterl2 myAdapterl2 = new MyAdapterl2(this,list); right_lv.setAdapter(myAdapterl2); } @Override public String getCid() { return this.cid; } }
myGridview
package com.example.thinkpad.wsjykmn.view; import android.content.Context; import android.service.wallpaper.WallpaperService; import android.util.AttributeSet; import android.widget.GridView; import okhttp3.Interceptor; public class myGridview extends GridView{ public myGridview(Context context) { super(context); } public myGridview(Context context, AttributeSet attrs) { super(context, attrs); } public myGridview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, i); } } // int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); // super.onMeasure(widthMeasureSpec, i);