MVP契约类

本文介绍了如何在Android应用中使用MVP模式实现契约类,包括View接口、Presenter接口以及Model接口的定义。通过具体例子如AddSubLayout、CarBean和Constans等,展示了如何在MainActivity中运用这些契约类进行数据交互和视图操作。
摘要由CSDN通过智能技术生成
<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=".ui.activity.MainActivity">

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9">
    </FrameLayout>

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <RadioButton
            android:checked="true"
            android:background="@drawable/sel_bj"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="购物车" />

        <RadioButton
            android:background="@drawable/sel_bj"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="我的" />


    </RadioGroup>


</LinearLayout>

//AddSubLayout
public class AddSubLayout extends LinearLayout implements View.OnClickListener {

    private TextView mAddBtn,mSubBtn;
    private TextView mNumText;
    private AddSubListener addSubListener;


    public AddSubLayout(Context context) {
        super(context);
        initView();
    }

    public AddSubLayout(Context context,  AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public AddSubLayout(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        //加载layout布局,第三个参数ViewGroup一定写成this
        View view = View.inflate(getContext(),R.layout.car_add_sub,this);

        mAddBtn = view.findViewById(R.id.btn_add);
        mSubBtn = view.findViewById(R.id.btn_sub);
        mNumText = view.findViewById(R.id.text_number);
        mAddBtn.setOnClickListener(this);
        mSubBtn.setOnClickListener(this);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        int width = r-l;//getWidth();
        int height = b-t;//getHeight();
    }

    @Override
    public void onClick(View v) {
        int number = Integer.parseInt(mNumText.getText().toString());

        switch (v.getId()){
            case R.id.btn_add:
                number++;
                mNumText.setText(number+"");
                break;
            case R.id.btn_sub:
                if (number==0){
                    Toast.makeText(getContext(),"数量不能小于0",Toast.LENGTH_LONG).show();
                    return;
                }
                number--;
                mNumText.setText(number+"");
                break;
        }

        if (addSubListener!=null){
            addSubListener.addSub(number);
        }

    }

    public void setCount(int count) {
        mNumText.setText(count+"");
    }

    public void setAddSubListener(AddSubListener addSubListener) {
        this.addSubListener = addSubListener;
    }

    public interface AddSubListener{
        void addSub(int count);
    }
}

//CarAdapter

public class CarAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<CarBean.DataBean> mShopList = new ArrayList<>();

    public CarAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getGroupCount() {
        return mShopList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mShopList.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mShopList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mShopList.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    public void addAll(List<CarBean.DataBean> data){
        if (data!=null){
            mShopList.addAll(data);
        }
    }
    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHodler hodler=null;
        if (convertView==null){
            convertView = View.inflate(parent.getContext(), R.layout.cart2_group_item, null);
            hodler = new GroupHodler();
            hodler.checkBox=convertView.findViewById(R.id.checkBox);
            convertView.setTag(hodler);
        }else {
            hodler = (GroupHodler) convertView.getTag();
        }

        final CarBean.DataBean dataBean = mShopList.get(groupPosition);
        //给商家赋值
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值