// mainActivity public class MainActivity extends AppCompatActivity { private FrameLayout mMainFragment; private RadioGroup mMainRadiogroup; private FragmentManager supportFragmentManager; private ClassifyFragment classifyFragment; private ShoppingFragment shoppingFragment; private AMapLocationListener mAMapLocationListener; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); classifyFragment = new ClassifyFragment(); shoppingFragment = new ShoppingFragment(); initView(); initFragmentManager(classifyFragment); textView = findViewById(R.id.weizhi); AMapLocationClient mLocationClient = null; mLocationClient = new AMapLocationClient(getApplicationContext()); mLocationClient.startLocation(); mAMapLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //解析定位结果 textView.setText(amapLocation.getCountry()+""+amapLocation.getProvince()+""+ amapLocation.getCity()+""+ amapLocation.getDistrict()+""+ amapLocation.getStreet()); } } } }; //设置定位回调监听 mLocationClient.setLocationListener(mAMapLocationListener); } private void initView() { mMainFragment = (FrameLayout) findViewById(R.id.main_fragment); mMainRadiogroup = (RadioGroup) findViewById(R.id.main_radiogroup); mMainRadiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.mian_classify: initFragmentManager(classifyFragment); break; case R.id.mian_shopping: initFragmentManager(shoppingFragment); break; } } }); } public void initFragmentManager(Fragment fragment){ supportFragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.main_fragment,fragment); fragmentTransaction.commit(); } } //mainactivity 布局
<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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="位置:" android:textSize="20dp" /> <TextView android:id="@+id/weizhi" android:layout_width="wrap_content" android:layout_height="50dp" android:text="河北" android:textSize="20dp" android:gravity="center_vertical" /> </LinearLayout> <FrameLayout android:id="@+id/main_fragment" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"></FrameLayout> <RadioGroup android:id="@+id/main_radiogroup" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <RadioButton android:id="@+id/mian_classify" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:text="分类" android:button="@null" android:gravity="center" /> <RadioButton android:id="@+id/mian_shopping" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:text="购物车" android:button="@null" android:gravity="center" /> </RadioGroup> </LinearLayout>
//fragment
public class ClassifyFragment extends Fragment implements IClassifyView { private static final String TAG = "ClassifyFragment"; private Classifypresenter classifypresenter; private RecyclerView recyclerView; private ClassifyLeftRecycle classifyLeftRecycle; private ExpandableListView expandableListView; private int cid = 1; private ClassifyRightExpandable classifyRightExpandable; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.classify_fragment,container,false); recyclerView = view.findViewById(R.id.classify_left_recycle); expandableListView = view.findViewById(R.id.classify_right_expandable); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); initnews(); } private void initnews() { classifypresenter = new Classifypresenter(this); classifypresenter.star(cid); } @Override public void onDestroy() { super.onDestroy(); classifypresenter.onDestroy(); } @Override public void onFailure(IOException e) { } @Override public void onResponse(String json) { Gson gson = new Gson(); ClassifyLeftBean classifyLeftBean = gson.fromJson(json, ClassifyLeftBean.class); List<ClassifyLeftBean.DataBean> data = classifyLeftBean.getData(); classifyLeftRecycle = new ClassifyLeftRecycle(data); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(classifyLeftRecycle); classifyLeftRecycle.setOnClassifyLeftRecycleClick(new ClassifyLeftRecycle.OnClassifyLeftRecycleClick() { @Override public void LeftRecycleClick(int cid) { Toast.makeText(getContext(),cid+"---",Toast.LENGTH_LONG).show(); classifypresenter.star(cid); } }); Log.d(TAG,data+""); } @Override public void onFailure1(IOException e) { } @Override public void onResponse1(String json) { Gson gson = new Gson(); ClassifyRightBean classifyRightBean = gson.fromJson(json, ClassifyRightBean.class); List<ClassifyRightBean.DataBean> data = classifyRightBean.getData(); classifyRightExpandable = new ClassifyRightExpandable(data,getContext()); expandableListView.setAdapter(classifyRightExpandable); for (int i = 0; i < data.size(); i++) { expandableListView.expandGroup(i); } } } //fragment副本布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/classify_left_recycle" android:layout_width="100dp" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> <ExpandableListView android:groupIndicator="@null" android:id="@+id/classify_right_expandable" android:layout_width="match_parent" android:layout_height="match_parent"></ExpandableListView> </LinearLayout> //fragment类
public class ShoppingFragment extends Fragment implements IShoppingView { private static final String TAG = "ShoppingFragment"; @BindView(R.id.shopping_expandable) ExpandableListView shoppingExpandable; @BindView(R.id.shopping_all_cb) CheckBox shoppingAllCb; @BindView(R.id.shopping_all_price) TextView shoppingAllPrice; @BindView(R.id.shopping_closing_price) TextView shoppingClosingPrice; Unbinder unbinder; private ShoppingPresenter shoppingPresenter; private String cid = "71"; private ShoppingExpandableAdaper shoppingExpandableAdaper; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.shopping_fragment, container, false); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); shoppingPresenter = new ShoppingPresenter(this); shoppingPresenter.star(cid); } @Override public void onFailure(IOException e) { } @Override public void onResponse(String json) { Gson gson = new Gson(); ShoppingBean shoppingBean = gson.fromJson(json, ShoppingBean.class); List<ShoppingBean.DataBean> data = shoppingBean.getData(); shoppingExpandableAdaper = new ShoppingExpandableAdaper(data,getContext()); shoppingExpandable.setAdapter(shoppingExpandableAdaper); for (int i = 0; i < data.size(); i++) { shoppingExpandable.expandGroup(i); } shoppingExpandableAdaper.setOnShoppingExpandableAdaper(new ShoppingExpandableAdaper.OnShoppingExpandableAdaper() { @Override public void OnParentClick(int groupPosition) { boolean isparentbelowchilds = shoppingExpandableAdaper.isparentbelowchilds(groupPosition); shoppingExpandableAdaper.changeparentbelowchilds(groupPosition,!isparentbelowchilds); shoppingExpandableAdaper.notifyDataSetChanged(); refreshShoppingFragment(); } @Override public void OnChildClick(int groupPosition, int childPosition) { //改变父布局下的子布局 shoppingExpandableAdaper.changechild(groupPosition,childPosition); shoppingExpandableAdaper.notifyDataSetChanged(); refreshShoppingFragment(); } @Override public void OnChildNumberClick(int groupPosition, int childPosition, int number) { //改变父布局下的子布局数量 shoppingExpandableAdaper.changechildNumber(groupPosition,childPosition,number); shoppingExpandableAdaper.notifyDataSetChanged(); refreshShoppingFragment(); } }); refreshShoppingFragment(); } public void refreshShoppingFragment(){ // boolean allCheckBox = shoppingExpandableAdaper.isAllCheckBox(); shoppingAllCb.setChecked(allCheckBox); //合计 float allprice = shoppingExpandableAdaper.gainAllPrice(); shoppingAllPrice.setText("合计 ¥:"+allprice); //数量 int allnumber = shoppingExpandableAdaper.gainAllnumber(); shoppingClosingPrice.setText("去结算("+allnumber+")"); } @Override public void onDestroy() { super.onDestroy(); shoppingPresenter.onDestroy(); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } @OnClick(R.id.shopping_all_cb) public void onViewClicked() { //判断所有的父布局的子布局的CheckBox; boolean parentCheckBox = shoppingExpandableAdaper.isParentCheckBox(); Log.d(TAG,parentCheckBox+"11111111111111111111111111111"); shoppingExpandableAdaper.changeParentCheckBox(!parentCheckBox); shoppingExpandableAdaper.notifyDataSetChanged(); refreshShoppingFragment(); } } //fragment副本布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ExpandableListView android:id="@+id/shopping_expandable" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" android:groupIndicator="@null" ></ExpandableListView> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <CheckBox android:id="@+id/shopping_all_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> <TextView android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选" android:paddingLeft="10dp" android:textSize="15dp" /> <TextView android:id="@+id/shopping_all_price" android:layout_gravity="center_vertical" android:paddingLeft="10dp" android:layout_weight="1" android:text="合计:2332.0" android:textSize="15dp" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/shopping_closing_price" android:gravity="center" android:layout_width="120dp" android:layout_height="match_parent" android:text="去结算(0)" android:background="#f00" android:textColor="#000000" android:textSize="15dp" /> </LinearLayout> </LinearLayout>//AdderSubtractor类
public class AdderSubtractor extends LinearLayout { @BindView(R.id.as_subtract) TextView asSubtract; @BindView(R.id.as_number) TextView asNumber; @BindView(R.id.as_add) TextView asAdd; private int number = 1; public AdderSubtractor(Context context) { super(context); } public AdderSubtractor(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public AdderSubtractor(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View view = inflate(context, R.layout.adder_subtractor, this); //初始化 ButterKnife.bind(view); } @OnClick({R.id.as_subtract, R.id.as_add}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.as_subtract: if (number>1){ -- number; asNumber.setText(number+""); if (onAdderSubtractor!=null){ onAdderSubtractor.onAdderSubtractorClick(number); } }else { Toast.makeText(getContext(),"不能再少了",Toast.LENGTH_LONG).show(); } break; case R.id.as_add: ++ number; asNumber.setText(number+""); if (onAdderSubtractor!=null){ onAdderSubtractor.onAdderSubtractorClick(number); } break; } } public void setNumber(int number) { this.number = number; asNumber.setText(number+""); } public int getNumber() { return number; } OnAdderSubtractor onAdderSubtractor; public void setOnAdderSubtractor(OnAdderSubtractor onAdderSubtractor) { this.onAdderSubtractor = onAdderSubtractor; } public interface OnAdderSubtractor{ void onAdderSubtractorClick(int number); } } //ClassifyLeftRecycle类
public class ClassifyLeftRecycle extends RecyclerView.Adapter<ClassifyLeftRecycle.ViewHolder> { private ViewHolder viewHolder; private List<ClassifyLeftBean.DataBean> data; public ClassifyLeftRecycle(List<ClassifyLeftBean.DataBean> data) { this.data = data; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.classify_left_recycle,parent,false); viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, final int position) { viewHolder.textView.setText(data.get(position).getName()); viewHolder.textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int cid = data.get(position).getCid(); onClassifyLeftRecycleClick.LeftRecycleClick(cid); } }); } @Override public int getItemCount() { return data == null ? 0 : data.size(); } class ViewHolder extends RecyclerView.ViewHolder{ private final TextView textView; public ViewHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.classify_left_recycle_tv); } } OnClassifyLeftRecycleClick onClassifyLeftRecycleClick; public void setOnClassifyLeftRecycleClick(OnClassifyLeftRecycleClick onClassifyLeftRecycleClick) { this.onClassifyLeftRecycleClick = onClassifyLeftRecycleClick; } public interface OnClassifyLeftRecycleClick{ void LeftRecycleClick(int cid); } } //ClassifyRightExpandable类
public class ClassifyRightExpandable extends BaseExpandableListAdapter { private List<ClassifyRightBean.DataBean> data; private Context context; private ClassifyRightRecycler classifyRightRecycler; public ClassifyRightExpandable(List<ClassifyRightBean.DataBean> data, Context context) { this.data = data; this.context = context; } @Override public int getGroupCount() { return data == null ? 0 : data.size(); } @Override public int getChildrenCount(int groupPosition) { return 1; } @Override public Object getGroup(int groupPosition) { return null; } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ParentViewHolder parentViewHolder; if (convertView == null) { convertView = View.inflate(context, R.layout.classify_parent, null); parentViewHolder = new ParentViewHolder(convertView); convertView.setTag(parentViewHolder); } else { parentViewHolder = (ParentViewHolder) convertView.getTag(); } parentViewHolder.classifyParentTv.setText(data.get(groupPosition).getName()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder; if (convertView == null) { convertView = View.inflate(context, R.layout.classify_child, null); childViewHolder = new ChildViewHolder(convertView); convertView.setTag(childViewHolder); }else { childViewHolder = (ChildViewHolder) convertView.getTag(); } ClassifyRightBean.DataBean dataBean = data.get(groupPosition); List<ClassifyRightBean.DataBean.ListBean> list = dataBean.getList(); classifyRightRecycler = new ClassifyRightRecycler(list,context); GridLayoutManager gridLayoutManager = new GridLayoutManager(context,2); gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); childViewHolder.classifyChildRecycler.setLayoutManager(gridLayoutManager); childViewHolder.classifyChildRecycler.setAdapter(classifyRightRecycler); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } static class ParentViewHolder { @BindView(R.id.classify_parent_tv) TextView classifyParentTv; ParentViewHolder(View view) { ButterKnife.bind(this, view); } } static class ChildViewHolder { @BindView(R.id.classify_child_recycler) RecyclerView classifyChildRecycler; ChildViewHolder(View view) { ButterKnife.bind(this, view); } } } //ClassifyRightRecycler类
public class ClassifyRightRecycler extends RecyclerView.Adapter<ClassifyRightRecycler.ChildViewHolder> { private ChildViewHolder childViewHolder; private List<ClassifyRightBean.DataBean.ListBean> list; private Context context; public ClassifyRightRecycler(List<ClassifyRightBean.DataBean.ListBean> list, Context context) { this.list = list; this.context = context; } @NonNull @Override public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.classify_right_recycler,parent,false); childViewHolder = new ChildViewHolder(view); return childViewHolder; } @Override public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) { //Glide.with(context).load(list.get(position).getIcon()).into(childViewHolder.imageView); ImageLoader.getInstance().displayImage(list.get(position).getIcon(),childViewHolder.imageView, MyApp.getOptions()); childViewHolder.textView.setText(list.get(position).getName()); } @Override public int getItemCount() { return list == null ? 0 : list.size(); } class ChildViewHolder extends RecyclerView.ViewHolder{ private final ImageView imageView; private final TextView textView; public ChildViewHolder(View itemView) { super(itemView); imageView = itemView.findViewById(R.id.classify_right_recycler_imav); textView = itemView.findViewById(R.id.classify_right_recycler_tv); //跳转页面 /* imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context, Main2Activity.class); int pcid = list.get(0).getPcid(); intent.putExtra("pcid",pcid); context.startActivity(intent); } });*/ } } } //ShoppingExpandableAdaper
public class ShoppingExpandableAdaper extends BaseExpandableListAdapter { private List<ShoppingBean.DataBean> list; private Context context; public ShoppingExpandableAdaper(List<ShoppingBean.DataBean> list, Context context) { this.list = list; this.context = context; } @Override public int getGroupCount() { return list == null ? 0 : list.size(); } @Override public int getChildrenCount(int groupPosition) { return list.get(groupPosition) == null ? 0 : list.get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return null; } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ParentViewHolder parentViewHolder; if (convertView == null) { convertView = View.inflate(context, R.layout.shopping_parent, null); parentViewHolder = new ParentViewHolder(convertView); convertView.setTag(parentViewHolder); } else { parentViewHolder = (ParentViewHolder) convertView.getTag(); } parentViewHolder.shoppingParentTv.setText(list.get(groupPosition).getSellerName()); //判断父布局的子布局的shoppingParentCb boolean isparentbelowchilds = isparentbelowchilds(groupPosition); parentViewHolder.shoppingParentCb.setChecked(isparentbelowchilds); parentViewHolder.shoppingParentCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onShoppingExpandableAdaper.OnParentClick(groupPosition); } }); return convertView; } public boolean isparentbelowchilds(int groupPosition) { List<ShoppingBean.DataBean.ListBean> list = this.list.get(groupPosition).getList(); for (int i = 0; i < list.size(); i++) { int selected = list.get(i).getSelected(); if (selected==0){ return false; } } return true; } public void changeparentbelowchilds(int groupPosition, boolean b) { List<ShoppingBean.DataBean.ListBean> list = this.list.get(groupPosition).getList(); for (int i = 0; i < list.size(); i++) { ShoppingBean.DataBean.ListBean listBean = list.get(i); listBean.setSelected(b ? 1 : 0); } } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder; if (convertView == null) { convertView = View.inflate(context, R.layout.shopping_child, null); childViewHolder = new ChildViewHolder(convertView); convertView.setTag(childViewHolder); } else { childViewHolder = (ChildViewHolder) convertView.getTag(); } List<ShoppingBean.DataBean.ListBean> list = this.list.get(groupPosition).getList(); ShoppingBean.DataBean.ListBean listBean = list.get(childPosition); //复选框 childViewHolder.shoppingChildCb.setChecked(listBean.getSelected() == 1); childViewHolder.shoppingChildCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onShoppingExpandableAdaper.OnChildClick(groupPosition,childPosition); } }); //图片 String[] split = list.get(childPosition).getImages().split("\\|"); ImageLoader.getInstance().displayImage(split[0], childViewHolder.shoppingChildImage, MyApp.getOptions()); // childViewHolder.shoppingChildTvName.setText(list.get(childPosition).getTitle()); childViewHolder.shoppingChildTvPrice.setText(list.get(childPosition).getPrice() + ""); childViewHolder.shoppingChildAs.setNumber(list.get(childPosition).getNum()); childViewHolder.shoppingChildAs.setOnAdderSubtractor(new AdderSubtractor.OnAdderSubtractor() { @Override public void onAdderSubtractorClick(int number) { onShoppingExpandableAdaper.OnChildNumberClick(groupPosition,childPosition,number); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } public void changechild(int groupPosition, int childPosition) { List<ShoppingBean.DataBean.ListBean> list = this.list.get(groupPosition).getList(); ShoppingBean.DataBean.ListBean listBean = list.get(childPosition); listBean.setSelected(listBean.getSelected()==0 ? 1 : 0); } public void changechildNumber(int groupPosition, int childPosition, int number) { List<ShoppingBean.DataBean.ListBean> list = this.list.get(groupPosition).getList(); ShoppingBean.DataBean.ListBean listBean = list.get(childPosition); listBean.setNum(number); } public boolean isAllCheckBox() { for (int i = 0; i < list.size(); i++) { List<ShoppingBean.DataBean.ListBean> listBeans = this.list.get(i).getList(); for (int j = 0; j < listBeans.size(); j++) { int selected = listBeans.get(j).getSelected(); if (selected==0){ return false; } } } return true; } public float gainAllPrice() { float allprice = 0; for (int i = 0; i < list.size(); i++) { List<ShoppingBean.DataBean.ListBean> listBeans = this.list.get(i).getList(); for (int j = 0; j < listBeans.size(); j++) { int selected = listBeans.get(j).getSelected(); if (selected==1){ int num = listBeans.get(j).getNum(); float price = listBeans.get(j).getPrice(); allprice += num * price; } } } return allprice; } public int gainAllnumber() { int number = 0; for (int i = 0; i < list.size(); i++) { List<ShoppingBean.DataBean.ListBean> listBeans = this.list.get(i).getList(); for (int j = 0; j < listBeans.size(); j++) { int selected = listBeans.get(j).getSelected(); if (selected==1){ int num = listBeans.get(j).getNum(); number += num; } } } return number; } public boolean isParentCheckBox() { for (int i = 0; i < list.size(); i++) { List<ShoppingBean.DataBean.ListBean> listBeans = this.list.get(i).getList(); for (int j = 0; j < listBeans.size(); j++) { int selected = listBeans.get(j).getSelected(); if (selected==0){ return false; } } } return true; } public void changeParentCheckBox(boolean b) { for (int i = 0; i < list.size(); i++) { List<ShoppingBean.DataBean.ListBean> listBeans = this.list.get(i).getList(); for (int j = 0; j < listBeans.size(); j++) { listBeans.get(j).setSelected(b ? 1 : 0); } } } static class ParentViewHolder { @BindView(R.id.shopping_parent_cb) CheckBox shoppingParentCb; @BindView(R.id.shopping_parent_tv) TextView shoppingParentTv; ParentViewHolder(View view) { ButterKnife.bind(this, view); } } static class ChildViewHolder { @BindView(R.id.shopping_child_cb) CheckBox shoppingChildCb; @BindView(R.id.shopping_child_image) ImageView shoppingChildImage; @BindView(R.id.shopping_child_tv_name) TextView shoppingChildTvName; @BindView(R.id.shopping_child_tv_price) TextView shoppingChildTvPrice; @BindView(R.id.shopping_child_as) AdderSubtractor shoppingChildAs; ChildViewHolder(View view) { ButterKnife.bind(this, view); } } OnShoppingExpandableAdaper onShoppingExpandableAdaper; public void setOnShoppingExpandableAdaper(OnShoppingExpandableAdaper onShoppingExpandableAdaper) { this.onShoppingExpandableAdaper = onShoppingExpandableAdaper; } public interface OnShoppingExpandableAdaper{ void OnParentClick(int groupPosition); void OnChildClick(int groupPosition, int childPosition); void OnChildNumberClick(int groupPosition, int childPosition, int number); } } //MyApp 记得注册
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); //1.自定义图片的缓存路径 File cachefile=new File( Environment.getExternalStorageDirectory().getPath()+"/abc"); //1.初使化 ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽 .threadPoolSize(2)//线程池的数量 .threadPriority(4) .memoryCacheSize(2*1024*1024)//设置内存缓存区大小 .diskCacheSize(20*1024*1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiscCache(cachefile))//自定义缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(configuration); } public static DisplayImageOptions getOptions(){ //2.显示配置 DisplayImageOptions imageOptions = new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时 .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2个字节 ARGB_8888 1px=4个字节 ARGB_4444 1px=2个字节 ALPHA_8 1px=1个字节 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 //.displayer(new RoundedBitmapDisplayer(100))//设置图片的圆角 注意:控件必须要设定宽度与高度 .build(); return imageOptions; } } //ClassifyModel类
public class ClassifyModel { private OkUtils okUtils; public void star(int cid, final IClassifyModel iClassifyModel) { okUtils = OkUtils.getokClient(); okUtils.doGet(UrlsUtils.classify_left, new OkUtils.IOkutils() { @Override public void onFailure(IOException e) { iClassifyModel.onFailure(e); } @Override public void onResponse(String json) { iClassifyModel.onResponse(json); } }); Map<String, String> map = new HashMap<>(); map.put("cid",cid+""); okUtils.doPost(UrlsUtils.classify_right, map, new OkUtils.IOkutils() { @Override public void onFailure(IOException e) { iClassifyModel.onFailure1(e); } @Override public void onResponse(String json) { iClassifyModel.onResponse1(json); } }); } public interface IClassifyModel{ void onFailure(IOException e); void onResponse(String json); void onFailure1(IOException e); void onResponse1(String json); } } //ShoppingModel类
public class ShoppingModel { private static final String TAG = "ShoppingModel"; private OkUtils okUtils; public void star(String cid, final IOnShoppingModel iOnShoppingModel) { // Map<String,String> map = new HashMap<>(); // map.put("cid",cid); okUtils = OkUtils.getokClient(); String u = UrlsUtils.SHOPPING+"?uid="+cid; okUtils.doGet(u, new OkUtils.IOkutils() { @Override public void onFailure(IOException e) { iOnShoppingModel.onFailure(e); } @Override public void onResponse(String json) { iOnShoppingModel.onResponse(json); } }); } public interface IOnShoppingModel{ void onFailure(IOException e); void onResponse(String json); } } //Classifypresenter类
public class Classifypresenter { private final ClassifyModel classifyModel; IClassifyView iClassifyView; public Classifypresenter(IClassifyView miClassifyView) { iClassifyView = miClassifyView; classifyModel = new ClassifyModel(); } public void star(int cid) { classifyModel.star(cid, new ClassifyModel.IClassifyModel() { @Override public void onFailure(IOException e) { iClassifyView.onFailure(e); } @Override public void onResponse(String json) { iClassifyView.onResponse(json); } @Override public void onFailure1(IOException e) { iClassifyView.onFailure1(e); } @Override public void onResponse1(String json) { iClassifyView.onResponse1(json); } }); } public void onDestroy() { iClassifyView = null; } } //ShoppingPresent类
public class ShoppingPresenter { private final ShoppingModel shoppingModel; IShoppingView iShoppingView; public ShoppingPresenter(IShoppingView miShoppingView) { iShoppingView = miShoppingView; shoppingModel = new ShoppingModel(); } public void star(String cid) { shoppingModel.star(cid, new ShoppingModel.IOnShoppingModel() { @Override public void onFailure(IOException e) { iShoppingView.onFailure(e); } @Override public void onResponse(String json) { iShoppingView.onResponse(json); } }); } public void onDestroy() { iShoppingView = null; } } //工具包
public class OkUtils { private final OkHttpClient okHttpClient; private static OkUtils mokUtils; private final Handler mhandler; private final HttpLoggingInterceptor httpLoggingInterceptor; private OkUtils() { httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); //创建handler mhandler = new Handler(Looper.getMainLooper()); okHttpClient = new OkHttpClient.Builder() .connectTimeout(5000, TimeUnit.MILLISECONDS) .readTimeout(5000,TimeUnit.MILLISECONDS) .writeTimeout(5000,TimeUnit.MILLISECONDS) .addInterceptor(httpLoggingInterceptor) .build(); } public static OkUtils getokClient(){ if (mokUtils == null){ synchronized (OkUtils.class){ if (mokUtils==null){ return mokUtils = new OkUtils(); } } } return mokUtils; } public void doGet(String url, final IOkutils iOkutils){ final Request request = new Request.Builder() .get() .url(url) .build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { mhandler.post(new Runnable() { @Override public void run() { iOkutils.onFailure(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { if (response!=null && response.isSuccessful()){ final String json = response.body().string(); mhandler.post(new Runnable() { @Override public void run() { iOkutils.onResponse(json); } }); } } }); } public void doPost(String url, Map<String,String> map, final IOkutils iOkutils){ FormBody.Builder builder = new FormBody.Builder(); for (String key :map.keySet()){ builder.add(key,map.get(key)); } FormBody build = builder.build(); Request request = new Request.Builder() .post(build) .url(url) .build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { mhandler.post(new Runnable() { @Override public void run() { iOkutils.onFailure(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { if (response !=null && response.isSuccessful()){ final String json = response.body().string(); mhandler.post(new Runnable() { @Override public void run() { iOkutils.onResponse(json); } }); } } }); } public interface IOkutils{ void onFailure(IOException e); void onResponse(String json); } } //json串
public class UrlsUtils { public static final String classify_left = "https://www.zhaoapi.cn/product/getCatagory"; public static final String classify_right = "https://www.zhaoapi.cn/product/getProductCatagory"; public static final String SHOPPING = "https://www.zhaoapi.cn/product/getCarts"; } //接口1 IClassifyView
public interface IClassifyView { void onFailure(IOException e); void onResponse(String json); void onFailure1(IOException e); void onResponse1(String json); } //接口2 IShoppingView
public interface IShoppingView { void onFailure(IOException e); void onResponse(String json); }
//同一写布局
adder_subtractor
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="40dp" android:background="#000000" android:orientation="horizontal" android:padding="2dp" > <TextView android:id="@+id/as_subtract" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="-" android:textSize="20dp" android:background="#eeeeee" /> <TextView android:layout_marginLeft="2dp" android:id="@+id/as_number" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="1" android:textSize="20dp" android:background="#eeeeee"/> <TextView android:layout_marginLeft="2dp" android:id="@+id/as_add" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="+" android:textSize="20dp" android:background="#eeeeee" /> </LinearLayout>//classify_child
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/classify_child_recycler" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout> //calssify_left_recycle
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:padding="10dp" android:id="@+id/classify_left_recycle_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" /> </LinearLayout> //classify_parent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/classify_parent_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:text="1" android:gravity="center" /> </LinearLayout> //classify_right_recycler
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/classify_right_recycler_imav" android:layout_gravity="center" android:layout_width="80dp" android:layout_height="100dp" /> <TextView android:id="@+id/classify_right_recycler_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout> //map_fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> //shopping_child
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:layout_gravity="center_vertical" android:id="@+id/shopping_child_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/shopping_child_image" android:layout_width="100dp" android:layout_height="120dp" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" /> <LinearLayout android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center_vertical" > <TextView android:id="@+id/shopping_child_tv_name" android:text="2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" /> <TextView android:id="@+id/shopping_child_tv_price" android:text="3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <bie.com.yukaomoni_2.adapter.AdderSubtractor android:id="@+id/shopping_child_as" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> </LinearLayout>//shopping_fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ExpandableListView android:id="@+id/shopping_expandable" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" android:groupIndicator="@null" ></ExpandableListView> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <CheckBox android:id="@+id/shopping_all_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> <TextView android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选" android:paddingLeft="10dp" android:textSize="15dp" /> <TextView android:id="@+id/shopping_all_price" android:layout_gravity="center_vertical" android:paddingLeft="10dp" android:layout_weight="1" android:text="合计:2332.0" android:textSize="15dp" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/shopping_closing_price" android:gravity="center" android:layout_width="120dp" android:layout_height="match_parent" android:text="去结算(0)" android:background="#f00" android:textColor="#000000" android:textSize="15dp" /> </LinearLayout> </LinearLayout>//shopping_parent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/shopping_parent_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/shopping_parent_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:text="1" android:paddingLeft="10dp" android:layout_gravity="center_vertical" /> </LinearLayout> //网络请求权限 和地图定义权限(全部)
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bie.com.yukaomoni_2"> <!-- 用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- 用于访问网络,网络定位需要上网 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 用于读取手机当前的状态 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 用于申请调用A-GPS模块 --> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <!-- 用于申请获取蓝牙信息进行室内定位 --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <application android:name=".app.MyApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:name="com.amap.api.v2.apikey" android:value="64eccb67af5e7988eaac1409d7776173" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.amap.api.location.APSService" /> </application> </manifest>