模仿点餐系统模式

效果图
模仿点餐系统模式_第1张图片
模仿点餐系统模式_第2张图片

依赖

implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'

Activity里面的代码

public class Shopping extends AppCompatActivity implements DataCall> {

private TextView mSumPrice;
private TextView mCount;

private RecyclerView mLeftRecycler, mRightRecycler;

private LeftAdapter mLeftAdapter;
private RightAdapter mRightAdapter;
private CartPresenter cartPresenter = new CartPresenter(this);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hurry);

    mSumPrice = findViewById(R.id.goods_sum_price);
    mCount = findViewById(R.id.goods_number);
    mLeftRecycler = findViewById(R.id.left_recycler);
    mRightRecycler = findViewById(R.id.right_recycler);

    mLeftRecycler.setLayoutManager(new LinearLayoutManager(this));
    mRightRecycler.setLayoutManager(new LinearLayoutManager(this));

    mLeftAdapter = new LeftAdapter(this);


    mLeftAdapter.setOnItemClickListenter(new LeftAdapter.OnItemClickListenter() {
        @Override
        public void onItemClick(Shop shop) {
            mRightAdapter.clearList();//清空数据
            mRightAdapter.addAll(shop.getList());
            mRightAdapter.notifyDataSetChanged();
        }
    });
    mLeftRecycler.setAdapter(mLeftAdapter);

    mRightAdapter = new RightAdapter(this);

    mRightAdapter.setOnNumListener(new RightAdapter.OnNumListener() {
        @Override
        public void onNum() {
            calculatePrice(mLeftAdapter.getList());
        }
    });
    mRightRecycler.setAdapter(mRightAdapter);
    cartPresenter.requestData();

}

@Override
public void success(List data) {
    calculatePrice(data);//计算价格和数量

    mLeftAdapter.addAll(data);//左边的添加类型

    //得到默认选中的shop,设置上颜色和背景
    Shop shop = data.get(1);
    shop.setTextColor(0xff000000);
    shop.setBackground(R.color.white);

    mRightAdapter.addAll(shop.getList());

    mLeftAdapter.notifyDataSetChanged();
    mRightAdapter.notifyDataSetChanged();
}

@Override
public void fail(Result result) {
    Toast.makeText(this, result.getCode() + "   " + result.getMsg(), Toast.LENGTH_LONG).show();
}

private void calculatePrice(List shopList) {
    double totalPrice = 0;
    int totalNum = 0;

    for (int i = 0; i 

Model层

public class CartModel {
public static Result goodsList() {
    String resultString = HttpUtils.get("http://www.zhaoapi.cn/product/getCarts?uid=71");
    try {
        Gson gson = new Gson();

        Type type = new TypeToken>>() {
        }.getType();

        Result result = gson.fromJson(resultString, type);
        return result;
    } catch (Exception e) {

    }
    Result result = new Result();
    result.setCode(-1);
    result.setMsg("数据解析异常");

    return result;
}
}

Presenter层

public class CartPresenter extends BasePresenter {

public CartPresenter(DataCall dataCall) {
    super(dataCall);
}

@Override
protected Result getData(Object... args) {
    Result result = CartModel.goodsList();//调用网络请求获取数据
    return result;
}
}

抽象类BasePresenter

public abstract class BasePresenter {

DataCall dataCall;

public BasePresenter(DataCall dataCall){
    this.dataCall = dataCall;
}


Handler mHandler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {

        Result result = (Result) msg.obj;
        if (result.getCode()==0){
            dataCall.success(result.getData());
        }else{
            dataCall.fail(result);
        }
    }
};



public void requestData(final Object...args){
    new Thread(new Runnable() {
        @Override
        public void run() {


            Message message = mHandler.obtainMessage();
            message.obj = getData(args);
            mHandler.sendMessage(message);

        }
    }).start();
}

protected abstract Result getData(Object...args);

public void unBindCall(){
    this.dataCall = null;
}
}	

View层

public interface DataCall {
void success(T data);
void fail(Result result);
}

自定义控件

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();
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public AddSubLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initView();
}

private void initView(){
    //加载layout布局,第三个参数ViewGroup一定写成this
    View view = View.inflate(getContext(),R.layout.activity_add_btn,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);
}
}

适配器
LeftAdapter

public class LeftAdapter extends RecyclerView.Adapter {

private List mList = new ArrayList<>();

private Context context;
public LeftAdapter(Context context) {
    this.context = context;
}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(viewGroup.getContext(), R.layout.activity_left_item,null);
    MyHolder myHolder = new MyHolder(view);
    return myHolder;
}

@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
    final Shop shop = mList.get(i);
    myHolder.text.setText(shop.getSellerName());
    myHolder.text.setBackgroundResource(shop.getBackground());
    myHolder.text.setTextColor(shop.getTextColor());
    myHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            for (int j = 0; j  getList() {
    return mList;
}

class MyHolder extends RecyclerView.ViewHolder{

    TextView text;

    public MyHolder(@NonNull View itemView) {
        super(itemView);
        text = itemView.findViewById(R.id.left_text);
    }
}
public void addAll(List data) {
    mList.addAll(data);
}

private OnItemClickListenter onItemClickListenter;

public void setOnItemClickListenter(OnItemClickListenter onItemClickListenter) {
    this.onItemClickListenter = onItemClickListenter;
}

public interface OnItemClickListenter{
    void onItemClick(Shop shop);
}
}

RightAdapter

public class RightAdapter extends RecyclerView.Adapter  {
private List mList = new ArrayList<>();
private Context context;
public RightAdapter(Context context) {
    this.context = context;
}

@NonNull
@Override
public ChildHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(viewGroup.getContext(), R.layout.activity_right_item, null);
    ChildHolder myHolder = new ChildHolder(view);
    return myHolder;
}

@Override
public void onBindViewHolder(@NonNull ChildHolder childHolder, int i) {
    final Goods goods = mList.get(i);
    childHolder.text.setText(goods.getTitle());
    childHolder.price.setText("单价:" + goods.getPrice());//单价

    String imageurl = "https" + goods.getImages().split("https")[1];
    Log.i("dt", "imageUrl: " + imageurl);
    imageurl = imageurl.substring(0, imageurl.lastIndexOf(".jpg") + ".jpg".length());
    Glide.with(context).load(imageurl).into(childHolder.image);//加载图片

    childHolder.addSub.setCount(goods.getNum());

    //数量监听
    childHolder.addSub.setAddSubListener(new AddSubLayout.AddSubListener() {
        @Override
        public void addSub(int count) {
            goods.setNum(count);

            onNumListener.onNum();//计算价格
        }
    });

}

@Override
public int getItemCount() {
    return mList.size();
}

public void addAll(List list) {
    mList.addAll(list);
}

public void clearList() {
    mList.clear();
}

class ChildHolder extends RecyclerView.ViewHolder {

    TextView text;
    TextView price;
    ImageView image;
    AddSubLayout addSub;

    public ChildHolder(@NonNull View itemView) {
        super(itemView);
        text = itemView.findViewById(R.id.text);
        price = itemView.findViewById(R.id.text_price);
        image = itemView.findViewById(R.id.image);
        addSub = itemView.findViewById(R.id.add_sub_layout);
    }
}
private OnNumListener onNumListener;

public void setOnNumListener(OnNumListener onNumListener) {
    this.onNumListener = onNumListener;
}

public interface OnNumListener{
    void onNum();
}
}

布局
activity_hurry




    

    
    

    




    
    

    


activity_left_item





activity_right_item










	

你可能感兴趣的:(进阶)