一、展示一下基本的包、类,购物车的实现在AFragment
二、布局的XML:
三、直接从购物车的实现开始
总的效果图:
**/1.布局:/**组控件与子控件的实现使用的是二级列表
--------布局:ExpandableListView
①主布局
RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#f5d5ab"
android:id="@+id/line1">
<CheckBox
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="60dp"
android:textSize="20dp"
android:text="全选"
android:id="@+id/selAll"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="总价:"
android:id="@+id/Allprice"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="去结算"
android:id="@+id/jiesuan"/>
</LinearLayout>
<ExpandableListView
android:layout_above="@+id/line1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ExpListView"
></ExpandableListView>
/RelativeLayout>
②组控件的布局就一个CheckBox和一个TextView
③子控件的布局(注:把加减的布局引过来)
引加减布局
④加减的布局:三个TextView实现
/2.具体的代码执行:/
①构建出MVP基本的框架
②用OkHttp封装工具类
ModelImpe类里代码执行:
public class ModelImppe implements Model {
private MyCallBack callBack;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==0){
String str=(String) msg.obj;
Gson gson=new Gson();
MyData myData = gson.fromJson(str, MyData.class);
callBack.setData(myData.getData());
}
}
};
@Override
public void getData(String url, MyCallBack callBack) {
this.callBack=callBack;
OkUtils.getInstance().getAsync(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string();
handler.sendMessage(handler.obtainMessage(0,s));
}
});
}
}
/3.适配器:/
创建一个适配器的类继承BaseExpandableListAdapter
(二级列表适配器的个人解析:
)
写①②时,看封装的基本类里有没有判断状态
public class ErAdapter extends BaseExpandableListAdapter {
private List<MyData.DataBean> mlist;
private Context context;
public ErAdapter(List<MyData.DataBean> mlist, Context context) {
this.mlist = mlist;
this.context = context;
}
@Override
public int getGroupCount() {
return mlist.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mlist.get(groupPosition).getSpus().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) {
GroupHolder groupHolder;
if (convertView==null){
convertView=View.inflate(context,R.layout.group_list,null);
groupHolder=new GroupHolder();
groupHolder.GroupCb=convertView.findViewById(R.id.groupCB);
groupHolder.GroupTv=convertView.findViewById(R.id.groupTV);
convertView.setTag(groupHolder);
}else {
groupHolder=(GroupHolder)convertView.getTag();
}
groupHolder.GroupTv.setText(mlist.get(groupPosition).getName());
boolean childAllChecked = isChildAllChecked(groupPosition);
groupHolder.GroupCb.setChecked(childAllChecked);
groupHolder.GroupCb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapterCallBack!=null){
adapterCallBack.setGroupCheck(groupPosition);
}
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder;
if (convertView==null){
convertView=View.inflate(context,R.layout.child_list,null);
childHolder=new ChildHolder();
childHolder.ChildCb=convertView.findViewById(R.id.childCB);
childHolder.ChildImg=convertView.findViewById(R.id.childImg);
childHolder.ChildTv=convertView.findViewById(R.id.childTitle);
childHolder.ChildSal=convertView.findViewById(R.id.childSale);
childHolder.ChildPri=convertView.findViewById(R.id.childPrice);
childHolder.jiajianView=convertView.findViewById(R.id.jiajianV);
convertView.setTag(childHolder);
}else {
childHolder=(ChildHolder)convertView.getTag();
}
childHolder.ChildCb.setChecked(mlist.get(groupPosition).getSpus().get(childPosition).isChildChecked());
MyData.DataBean.SpusBean sBean = mlist.get(groupPosition).getSpus().get(childPosition);
Glide.with(context).load(sBean.getPic_url()).into(childHolder.ChildImg);
childHolder.ChildTv.setText(sBean.getName()+"");
childHolder.ChildSal.setText(sBean.getMonth_saled()+"");
childHolder.ChildPri.setText(sBean.getSkus().get(0).getPrice()+"");
childHolder.jiajianView.setNum(sBean.getPraise_num());
childHolder.ChildCb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapterCallBack!=null){
adapterCallBack.setChildCheck(groupPosition,childPosition);
}
}
});
childHolder.jiajianView.setOnChange(new JiajianView.OnCountChange() {
@Override
public void setCount(int count) {
if (adapterCallBack!=null){
adapterCallBack.setNumber(groupPosition,childPosition,count);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class GroupHolder{
CheckBox GroupCb;
TextView GroupTv;
}
class ChildHolder{
JiajianView jiajianView;
CheckBox ChildCb;
ImageView ChildImg;
TextView ChildTv;
TextView ChildSal;
TextView ChildPri;
}
//1.点击group的checkBox让所有child的复选框选中
public void childAllCheck(int groupPosition, boolean isCheck){
MyData.DataBean bean = mlist.get(groupPosition);
List<MyData.DataBean.SpusBean> spus = bean.getSpus();
for (int i = 0; i < spus.size(); i++) {
MyData.DataBean.SpusBean spusBean = spus.get(i);
spusBean.setChildChecked(isCheck);
}
}
//1.判断小组是否被选中
public boolean isChildAllChecked(int groupPositon){
MyData.DataBean bean = mlist.get(groupPositon);
List<MyData.DataBean.SpusBean> spus = bean.getSpus();
for (int i = 0; i < spus.size(); i++) {
MyData.DataBean.SpusBean spusBean = spus.get(i);
//只要有一个没有被点中 点这个checkbox就是全选的功能
if (!spusBean.isChildChecked()){
return false;
}
}
return true;
}
//2.点击child给他进行赋值
public void setChildChecked(int groupPositon, int childPositon, boolean isCheckBox){
MyData.DataBean.SpusBean spusBean = mlist.get(groupPositon).getSpus().get(childPositon);
spusBean.setChildChecked(isCheckBox);
}
//2.查看当前这个商品有没有被选中
public boolean isChildChecked(int groupPosition,int childPosition){
MyData.DataBean.SpusBean spusBean = mlist.get(groupPosition).getSpus().get(childPosition);
if (spusBean.isChildChecked()){
return true;
}
return false;
}
//给商品数量进行赋值
public void setShangPingNumber(int groupPosition, int childPosition, int number) {
MyData.DataBean.SpusBean spusBean = mlist.get(groupPosition).getSpus().get(childPosition);
spusBean.setPraise_num(number);
}
//3.①全选反选
public boolean isAllGoods(){
for (int i = 0; i <mlist.size() ; i++) {
MyData.DataBean bean = mlist.get(i);
for (int j = 0; j <bean.getSpus().size() ; j++) {
MyData.DataBean.SpusBean spusBean = bean.getSpus().get(j);
if (!spusBean.isChildChecked()){
return false;
}
}
}
return true;
}
//3.①全选反选功能
public void SelectAll(boolean isAllChecked){
for (int i = 0; i < mlist.size(); i++) {
MyData.DataBean bean = mlist.get(i);
for (int j = 0; j <bean.getSpus().size() ; j++) {
MyData.DataBean.SpusBean spusBean = bean.getSpus().get(j);
spusBean.setChildChecked(isAllChecked);
}
}
}
//3.②计算所有商品的价格
public float getAllPrice(){
float allPrice=0;
for (int i = 0; i <mlist.size() ; i++) {
MyData.DataBean bean = mlist.get(i);
for (int j = 0; j < bean.getSpus().size(); j++) {
MyData.DataBean.SpusBean spusBean = bean.getSpus().get(j);
if (spusBean.isChildChecked()){
allPrice=allPrice+spusBean.getPraise_num()*Float.parseFloat(spusBean.getSkus().get(0).getPrice());
}
}
}
return allPrice;
}
//3.②计算所有商品的数量
public int getAllNum(){
int allNum=0;
for (int i = 0; i <mlist.size() ; i++) {
MyData.DataBean bean = mlist.get(i);
for (int j = 0; j <bean.getSpus().size() ; j++) {
MyData.DataBean.SpusBean spusBean = bean.getSpus().get(j);
if (spusBean.isChildChecked()){
allNum=allNum+spusBean.getPraise_num();
}
}
}
return allNum;
}
//写一个接口
//点击Group和Child第CheckBox在主页面都需要刷新值所以做成接口回调
public interface AdapterCallBack{
void setGroupCheck(int groupPosition);
void setChildCheck(int groupPosition,int childPosition);
//点击加减按钮刷新
void setNumber(int groupPosition, int childPosition, int number);
}
//定义一个成员变量
private AdapterCallBack adapterCallBack;
//提供一个方法
public void setCallback(AdapterCallBack adapterCallBack){
this.adapterCallBack=adapterCallBack;
}
}
注解:
1.适配器和Fragment里标 1 的展现的是点击组的CheckBox与否实现子控件的CheckBox的全选与全不选
2.适配器和Fragment里标 2 的展现的子的CheckBox是与组的CheckBox的联动
-----①当子的所有CheckBox选中时组的CheckBox也自动选中
-----②当子的所有CheckBox有一个没有选中时组的CheckBox也自动取消
3.适配器和Fragment里标 3 的展现的底部所有功能
/3.AFragment:/
public class AFragment extends Fragment implements IView,View.OnClickListener {
private String mUrl = "http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
private ExpandableListView expListView;
private List<MyData.DataBean> mlist = new ArrayList<>();
private ErAdapter erAdapter;
private CheckBox selAll;
private TextView Allprice;
private TextView jiesuan;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, null);
initView(view);
initPageLogic();
erAdapter = new ErAdapter(mlist, getContext());
expListView.setAdapter(erAdapter);
erAdapter.setCallback(new ErAdapter.AdapterCallBack() {
@Override
public void setGroupCheck(int groupPosition) {
//1.是否被全选中
boolean childAllChecked = erAdapter.isChildAllChecked(groupPosition);
erAdapter.childAllCheck(groupPosition, !childAllChecked);
erAdapter.notifyDataSetChanged();
fleshBtnLayout();
}
@Override
public void setChildCheck(int groupPosition, int childPosition) {
//2.得到要点击的商品child是否时选中状态
boolean childChecked = erAdapter.isChildChecked(groupPosition, childPosition);
erAdapter.setChildChecked(groupPosition, childPosition, !childChecked);
erAdapter.notifyDataSetChanged();
fleshBtnLayout();
}
@Override
public void setNumber(int groupPosition, int childPosition, int number) {
erAdapter.setShangPingNumber(groupPosition, childPosition, number);
erAdapter.notifyDataSetChanged();
fleshBtnLayout();
}
});
return view;
}
private void initPageLogic() {
PresenterImpe presenterImpe = new PresenterImpe(this);
presenterImpe.getData(mUrl);
}
//查找控件
private void initView(View view) {
expListView = view.findViewById(R.id.ExpListView);
//去掉自带的小箭头
expListView.setGroupIndicator(null);
selAll = (CheckBox) view.findViewById(R.id.selAll);
selAll.setOnClickListener(this);
Allprice = (TextView) view.findViewById(R.id.Allprice);
jiesuan = (TextView) view.findViewById(R.id.jiesuan);
}
@Override
public void success(Object data) {
mlist.addAll((Collection<? extends MyData.DataBean>) data);
erAdapter.notifyDataSetChanged();
//首次加载全部展开
for (int i = 0; i <mlist.size() ; i++) {
expListView.expandGroup(i);
}
//不能点击伸缩
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
}
@Override
public void error(String error) {
}
//来回切换时数据不重新添加一遍
@Override
public void onStop() {
super.onStop();
mlist.clear();
}
//3.刷新底部数据
public void fleshBtnLayout() {
boolean allGoods = erAdapter.isAllGoods();
selAll.setChecked(allGoods);
//总价
float allPrice = erAdapter.getAllPrice();
Allprice.setText("价格:"+allPrice);
//数量
int allNum = erAdapter.getAllNum();
jiesuan.setText("去结算:"+allNum);
}
//3.全选按钮的监听
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.selAll:
boolean allGoods = erAdapter.isAllGoods();
erAdapter.SelectAll(!allGoods);
erAdapter.notifyDataSetChanged();
fleshBtnLayout();
break;
}
}
}