Main中点击监听
public class Main2Activity extends AppCompatActivity implements IView { private String murl = "http://www.wanandroid.com/tools/mockapi/6523/restaurant-list"; private IPresenterImpl presenter; private CheckBox Check_All; private TextView All_Price; private TextView Go_To_JS; private RelativeLayout bottom_layout; private ExpandableListView erji_view; private List<Work.DataBean> mdatas = new ArrayList<>(); private ErJiAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); adapter = new ErJiAdapter(this,mdatas); erji_view.setAdapter(adapter); presenter.requestData(murl); setListener(); } private void setListener() { adapter.setCallChecked(new ErJiAdapter.AdapterCallBack() { @Override public void setGroupCheckClick(int groupPosition) { boolean isallchecked = adapter.isChildAllChecked(groupPosition); adapter.setGridChecked(groupPosition,!isallchecked); adapter.notifyDataSetChanged(); getbuttomAllLayout(); } @Override public void setChildCheckClick(int groupPosition, int childPosition) { boolean ischildchecked = adapter.isChildChecked(groupPosition,childPosition); adapter.setChildChecked(groupPosition,childPosition,!ischildchecked); adapter.notifyDataSetChanged(); getbuttomAllLayout(); } @Override public void setSumNum(int groupPosition, int childPosition, int count) { adapter.setNumberChange(groupPosition,childPosition,count); adapter.notifyDataSetChanged(); getbuttomAllLayout(); } }); Check_All.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean boo = adapter.isAllGrid(); adapter.setAllShopChecked(!boo); adapter.notifyDataSetChanged(); getbuttomAllLayout(); } }); } private void initView() { Check_All = (CheckBox) findViewById(R.id.Check_All); All_Price = (TextView) findViewById(R.id.All_Price); Go_To_JS = (TextView) findViewById(R.id.Go_To_JS); bottom_layout = (RelativeLayout) findViewById(R.id.bottom_layout); erji_view = (ExpandableListView) findViewById(R.id.erji_view); erji_view.setGroupIndicator(null); presenter = new IPresenterImpl(this); } @Override public void showRequestData(Object data) { mdatas = (List<Work.DataBean>) data; adapter.setMdatas(mdatas); for(int i=0;i<adapter.getGroupCount();i++){ erji_view.expandGroup(i); } erji_view.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { return true; } }); } public void getbuttomAllLayout(){ boolean boo = adapter.isAllGrid(); Check_All.setChecked(boo); float mPrice = adapter.getPriceNumber(); int mNumber = adapter.getChangeNumber(); All_Price.setText("价格:"+mPrice); Go_To_JS.setText("去结算("+mNumber+")"); } }
二级列表的adapter:
public class ErJiAdapter extends BaseExpandableListAdapter { private Context context; private List<Work.DataBean> mdatas; public ErJiAdapter(Context context, List<Work.DataBean> mdatas) { this.context = context; this.mdatas = mdatas; } public void setMdatas(List<Work.DataBean> mdatas) { this.mdatas = mdatas; notifyDataSetChanged(); } @Override public int getGroupCount() { return mdatas.size(); } @Override public int getChildrenCount(int groupPosition) { return mdatas.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 holder = null; if(convertView == null){ holder = new GroupHolder(); convertView = View.inflate(context,R.layout.group_list,null); holder.mGroupCheck = convertView.findViewById(R.id.Group_CB); holder.mGroupTv = convertView.findViewById(R.id.Group_Name); convertView.setTag(holder); }else{ holder = (GroupHolder) convertView.getTag(); } Work.DataBean dataBean = mdatas.get(groupPosition); holder.mGroupTv.setText(dataBean.getName()); Boolean isallck = isChildAllChecked(groupPosition); holder.mGroupCheck.setChecked(isallck); holder.mGroupCheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(madapterCallBack!=null){ madapterCallBack.setGroupCheckClick(groupPosition); } } }); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildHolder holder = null; if(convertView==null){ holder = new ChildHolder(); convertView = View.inflate(context,R.layout.child_item,null); holder.mChildCheck = convertView.findViewById(R.id.Child_Check_CB); holder.mChildTitle = convertView.findViewById(R.id.Child_title); holder.mImage = convertView.findViewById(R.id.Child_Icon); holder.mChildPrice = convertView.findViewById(R.id.Child_price); holder.addNumView = convertView.findViewById(R.id.footer_add); convertView.setTag(holder); }else{ holder = (ChildHolder) convertView.getTag(); } final Work.DataBean.SpusBean spusBean = mdatas.get(groupPosition).getSpus().get(childPosition); Glide.with(context).load(spusBean.getPic_url()).into(holder.mImage); holder.mChildTitle.setText(spusBean.getName()+""); holder.mChildPrice.setText(spusBean.getSkus().get(0).getPrice()+""); holder.mChildCheck.setChecked(spusBean.isChildboo()); holder.addNumView.setNumber(spusBean.getPraise_num()); holder.mChildCheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(madapterCallBack!=null){ madapterCallBack.setChildCheckClick(groupPosition,childPosition); } } }); holder.addNumView.getNumClick(new ErjiFooterView.SetOnClick() { @Override public void setCountClick(int number) { if(madapterCallBack!=null){ madapterCallBack.setSumNum(groupPosition,childPosition,number); } } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class GroupHolder { private CheckBox mGroupCheck; private TextView mGroupTv; } class ChildHolder { private CheckBox mChildCheck; private TextView mChildTitle; private ImageView mImage; private TextView mChildPrice; private ErjiFooterView addNumView; } public void setGridChecked(int groupPosition,boolean boo){ Work.DataBean dataBean = mdatas.get(groupPosition); List<Work.DataBean.SpusBean> spus = dataBean.getSpus(); for(int i=0;i<spus.size();i++){ Work.DataBean.SpusBean spusBean = spus.get(i); spusBean.setChildboo(boo); } }; public boolean isChildAllChecked(int groupPosition){ boolean boo = true; Work.DataBean dataBean = mdatas.get(groupPosition); List<Work.DataBean.SpusBean> spus = dataBean.getSpus(); for(int i=0;i<spus.size();i++){ Work.DataBean.SpusBean spusBean = spus.get(i); if(!spusBean.isChildboo()){ return false; } } return boo; } public void setChildChecked(int groupPosition,int childPosition,boolean boo){ Work.DataBean.SpusBean spusBean = mdatas.get(groupPosition).getSpus().get(childPosition); spusBean.setChildboo(boo); } public boolean isChildChecked(int groupPosition,int childPosition){ Boolean childboo = mdatas.get(groupPosition).getSpus().get(childPosition).isChildboo(); if(childboo){ return true; } return false; } public boolean isAllGrid(){ boolean boo = true; for(int i=0;i<mdatas.size();i++){ Work.DataBean dataBean = mdatas.get(i); for(int j=0;j<dataBean.getSpus().size();j++){ Work.DataBean.SpusBean spusBean = dataBean.getSpus().get(j); if(!spusBean.isChildboo()){ return false; } } } return boo; } public void setAllShopChecked(boolean boo){ for(int i=0;i<mdatas.size();i++){ Work.DataBean dataBean = mdatas.get(i); for(int j=0;j<dataBean.getSpus().size();j++){ Work.DataBean.SpusBean spusBean = dataBean.getSpus().get(j); spusBean.setChildboo(boo); } } } public void setNumberChange(int groupPosition,int childPosition,int count){ Work.DataBean.SpusBean spusBean = mdatas.get(groupPosition).getSpus().get(childPosition); spusBean.setPraise_num(count); } public float getPriceNumber(){ float priceAll = 0; for(int i=0;i<mdatas.size();i++){ Work.DataBean dataBean = mdatas.get(i); for(int j=0;j<dataBean.getSpus().size();j++){ Work.DataBean.SpusBean spusBean = dataBean.getSpus().get(j); if(spusBean.isChildboo()){ priceAll += spusBean.getPraise_num()*Float.parseFloat(spusBean.getSkus().get(0).getPrice()); } } } return priceAll; } public int getChangeNumber(){ int number = 0; for(int i=0;i<mdatas.size();i++){ Work.DataBean dataBean = mdatas.get(i); for(int j=0;j<dataBean.getSpus().size();j++){ Work.DataBean.SpusBean spusBean = dataBean.getSpus().get(j); if(spusBean.isChildboo()){ number += spusBean.getPraise_num(); } } } return number; } public interface AdapterCallBack{ void setGroupCheckClick(int groupPosition); void setChildCheckClick(int groupPosition,int childPosition); void setSumNum(int groupPosition,int childPosition,int count); } private AdapterCallBack madapterCallBack; public void setCallChecked(AdapterCallBack adapterCallBack){ this.madapterCallBack = adapterCallBack; } }
recycleAdapter:
public class RecyGridAdapter extends RecyclerView.Adapter<RecyGridAdapter.ViewHolder> implements View.OnClickListener { private Context context; private List<PicBean.ResultsBean> mgrids; public RecyGridAdapter(Context context, List<PicBean.ResultsBean> mgrids) { this.context = context; this.mgrids = mgrids; } public void setMgrids(List<PicBean.ResultsBean> mgrids) { this.mgrids = mgrids; notifyDataSetChanged(); } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = View.inflate(context,R.layout.grid_item,null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { Glide.with(context).load(mgrids.get(i).getUrl()).into(viewHolder.imgGrid); viewHolder.titleGrid.setText(mgrids.get(i).getType()); viewHolder.itemView.setOnClickListener(this); viewHolder.itemView.setTag(i); } @Override public int getItemCount() { return mgrids.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private ImageView imgGrid; private TextView titleGrid; public ViewHolder(@NonNull View itemView) { super(itemView); imgGrid = itemView.findViewById(R.id.img_grid); titleGrid = itemView.findViewById(R.id.title_grid_item); } } public interface SetOnGridClick{ void setOnItemListenerClick(int i); } private SetOnGridClick msetOnGridClick; public void setOnCallBackClick(SetOnGridClick setOnGridClick){ this.msetOnGridClick = setOnGridClick; } @Override public void onClick(View v) { msetOnGridClick.setOnItemListenerClick((int) v.getTag()); } }
自定义加减View:
public class JiaJianView extends LinearLayout implements View.OnClickListener { private TextView mAdd; private TextView mDelete; private TextView mNumber; //商品数量 private int mCount; public JiaJianView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.add_remove_view_layout, this); initViews(); } private void initViews() { mAdd = findViewById(R.id.add_tv); mDelete = findViewById(R.id.delete_tv); mNumber = findViewById(R.id.product_number_tv); mAdd.setOnClickListener(this); mDelete.setOnClickListener(this); } //先给初始值赋值 public void setNumber(int number) { this.mCount = number; mNumber.setText(number + ""); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.delete_tv: if (mCount > 0) { mCount--; mNumber.setText(mCount + ""); if (mCountChange!=null){ mCountChange.setCount(mCount); } }else { Toast.makeText(getContext(), "商品已售空", Toast.LENGTH_SHORT).show(); } break; case R.id.add_tv: mCount++; mNumber.setText(mCount + ""); if (mCountChange!=null){ mCountChange.setCount(mCount); } break; } } public interface OnCountChange { void setCount(int count); } private OnCountChange mCountChange; public void setOnChange(OnCountChange countChange) { this.mCountChange = countChange; } }
presenter层:
public class IPresenterImpl implements IPresenter { private IView iView; private IModelImpl iModel; public IPresenterImpl(IView iView) { this.iView = iView; iModel = new IModelImpl(); } @Override public void requestData(String murl) { iModel.startRequestData(murl, new MyCallBack() { @Override public void setData(Object data) { iView.showRequestData(data); } }); }
}
Model:
public class IModelImpl implements IModel { private MyCallBack mcallBack; private Handler mhandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==0){ String jsonStr = (String) msg.obj; Gson gson = new Gson(); Work work = gson.fromJson(jsonStr,Work.class); mcallBack.setData(work.getData()); } } }; @Override public void startRequestData(final String murl, MyCallBack callBack) { this.mcallBack = callBack; new Thread(new Runnable() { @Override public void run() { MyHttpUntils.getInstance().getAsync(murl, new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e("onFailure",e.getMessage()+"onFailure"); } @Override public void onResponse(Call call, Response response) throws IOException { String data = response.body().string(); mhandler.sendMessage(mhandler.obtainMessage(0,data)); } }); } }).start(); } }
不要忘了 布局文件里checkbox 设置 focusable 为false
CheckBox android:id="@+id/Group_CB" android:layout_width="wrap_content" android:layout_height="wrap_content" -- focusable设置不强焦点 -- android:focusable="false"
View层在main层回调方法:
@Override public void showRequestData(Object data) { mdatas = (List<Work.DataBean>) data; adapter.setMdatas(mdatas); //初试化时展开列表 for(int i=0;i<adapter.getGroupCount();i++){ erji_view.expandGroup(i); } //这个方法是点击无效 不能收回列表 erji_view.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { return true; } }); }