购物车RecycleView

 private List spus;
        private boolean isChecked;
         private List status_remind_list;
            private boolean isChildChecked;

MainActivity页面

package com.example.myliushibuju;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myliushibuju.adapter.MyAdapter;
import com.example.myliushibuju.adapter.MyChildAdapter;
import com.example.myliushibuju.bean.MyData;
import com.example.myliushibuju.presenter.PresenterImpl;
import com.example.myliushibuju.view.IView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IView {
    private String mUrl="http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
    private RecyclerView Recy_Group;
    private RecyclerView Recy_Child;
    private ImageView Recy_Image;
    private TextView Jia_Text;
    private TextView Zong_Text;
    private RelativeLayout Rl;
    private ArrayList mList=new ArrayList<>();
    private ArrayList mData=new ArrayList<>();
    private PresenterImpl presenter;
    private MyAdapter myAdapter;
    private MyChildAdapter myChildAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        presenter = new PresenterImpl(this);
        myAdapter = new MyAdapter(mList,this);
        myChildAdapter=new MyChildAdapter(mData,this);
        Recy_Child.setAdapter(myChildAdapter);
        Recy_Group.setAdapter(myAdapter);
        presenter.startRequse(mUrl);
        //设置点击事件
        myAdapter.setOnItemClick(new MyAdapter.ItemClik() {
            @Override
            public void setItem(View v, int position) {
                mData.clear();
                //给数量置零
                mData.addAll(mList.get(position).getSpus());
                for (int i = 0; i 

适配器Group

package com.example.myliushibuju.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.myliushibuju.R;
import com.example.myliushibuju.bean.MyData;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter implements OnClickListener {

    private ArrayList mList;
    private Context mContext;

    public MyAdapter(ArrayList mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.group_item, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHolder viewHolder, int i) {

        MyData.DataBean dataBean = mList.get(i);
        viewHolder.group_text.setText(dataBean.getName());
        viewHolder.itemView.setTag(i);
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView group_text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            group_text = itemView.findViewById(R.id.Group_Text);
        }
    }

    public interface ItemClik {

        void setItem(View v, int position);
    }

    private ItemClik itemClik;

    public void setOnItemClick(ItemClik itemClick) {
        this.itemClik = itemClick;
    }

    @Override
    public void onClick(View v) {

        itemClik.setItem(v, (Integer) v.getTag());
    }

}

适配器Child

package com.example.myliushibuju.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myliushibuju.MainActivity;
import com.example.myliushibuju.R;
import com.example.myliushibuju.bean.MyData;
import com.example.myliushibuju.weight.JiaJiaView;

import java.util.ArrayList;

public class MyChildAdapter extends RecyclerView.Adapter {

    private ArrayList mData = new ArrayList<>();
    private Context mContext;

    public MyChildAdapter(ArrayList mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public MyChildAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.child_item, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        //view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyChildAdapter.ViewHolder viewHolder, final int i) {

        MyData.DataBean.SpusBean spusBean = mData.get(i);
        viewHolder.child_title.setText(spusBean.getName());
        viewHolder.child_price.setText(spusBean.getSkus().get(0).getPrice() + "");
        Glide.with(mContext).load(spusBean.getPic_url()).into(viewHolder.child_image);

        viewHolder.Jia_Jian_View.setNumber(mData.get(i).getPraise_num());
        viewHolder.Jia_Jian_View.setOnChange(new JiaJiaView.OnCountChange() {
            @Override
            public void setCount(int number) {
                mData.get(i).setPraise_num(number);
                adapterCallback.shuaXin();

            }
        });
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView child_image;
        private TextView child_price;
        private TextView child_title;
        private JiaJiaView Jia_Jian_View;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            child_image = itemView.findViewById(R.id.Child_Image);
            child_price = itemView.findViewById(R.id.Child_Price);
            child_title = itemView.findViewById(R.id.Child_Title);
            Jia_Jian_View = itemView.findViewById(R.id.Jia_Jian_View);

        }
    }


    public interface AdapterCallback {

        void shuaXin();
    }

    private AdapterCallback adapterCallback;

    public void setCallback(AdapterCallback adapterCallback) {
        this.adapterCallback = adapterCallback;
    }


    public float getAllGoodsPrice() {
        float allPrice = 0;


        for (int i = 0; i < mData.size(); i++) {
            MyData.DataBean.SpusBean spusBean = mData.get(i);
            allPrice += spusBean.getPraise_num() * Float.parseFloat(spusBean.getSkus().get(0).getPrice());
        }

        return allPrice;
    }


}

工具类

package com.example.myliushibuju.utils;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpUtils {

    private OkHttpClient okHttpClient;
    private OkHttpUtils(){
        okHttpClient=new OkHttpClient();
    }
    public static OkHttpUtils getInstance(){

        return okHttpHolde.utils;

    }

    static class okHttpHolde{

        private static final OkHttpUtils utils=new OkHttpUtils();

    }

    public String get(String url) throws IOException {
        Request request=new Request.Builder().url(url).build() ;

        Response execute = okHttpClient.newCall(request).execute();

        return execute.body().string();
    }


}

自定义View

package com.example.myliushibuju.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myliushibuju.R;

public class JiaJiaView extends LinearLayout implements View.OnClickListener {


    private TextView mAdd;
    private TextView mDelete;
    private TextView mNumber;
    private int mCount=0;

    public JiaJiaView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.jiajian_item, 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 mCount){
        this.mCount=mCount;

        if(mCount==0){
            mDelete.setVisibility(GONE);
            mNumber.setVisibility(GONE);
        }else{
            mNumber.setVisibility(VISIBLE);
            mDelete.setVisibility(VISIBLE);
        }

        mNumber.setText(mCount+"");
    }

    public interface OnCountChange{
        void setCount(int mCount);
    }
    private OnCountChange mOnCountChange;

    public void setOnChange(OnCountChange onCountChange){
        this.mOnCountChange=onCountChange;
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.delete_tv:
                if(mCount>0){
                    mCount--;
                    mNumber.setText(mCount+"");
                    if(mOnCountChange!=null){
                        mOnCountChange.setCount(mCount);
                    }if(mCount==0){
                        mDelete.setVisibility(GONE);
                        mNumber.setVisibility(GONE);
                    }
                    }


                break;
            case R.id.add_tv:
                mCount++;
                mNumber.setVisibility(VISIBLE);
                mDelete.setVisibility(VISIBLE);
                mNumber.setText(mCount+"");
                if(mOnCountChange!=null){
                    mOnCountChange.setCount(mCount);
                }
                break;
        }
    }
}

IView

package com.example.myliushibuju.view;

public interface IView {
    void success(T data);
    void error(T error);
}

Model

package com.example.myliushibuju.model;

import com.example.myliushibuju.callback.MyCallBack;

public interface Model {
    void getData(String url, MyCallBack callBack);
}

ModelImpl

package com.example.myliushibuju.model;

import android.os.AsyncTask;

import com.example.myliushibuju.bean.MyData;
import com.example.myliushibuju.callback.MyCallBack;
import com.example.myliushibuju.utils.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;

public class ModelImpl implements Model {

    private MyCallBack callBack;
    @Override
    public void getData(String url, MyCallBack callBack) {
        this.callBack=callBack;
        new MyTask().execute(url);
    }

    class MyTask extends AsyncTask{

        @Override
        protected MyData doInBackground(String... strings) {
            try {
                String jsonStr = OkHttpUtils.getInstance().get(strings[0]);
                Gson gson=new Gson();
                MyData myData = gson.fromJson(jsonStr, MyData.class);
                return myData;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(MyData myData) {
            super.onPostExecute(myData);
            callBack.setData(myData);
        }
    }

}

MyCallBack

package com.example.myliushibuju.callback;

public interface MyCallBack {
   void setData(T user);
   void setError(T error);
}

Presenter

package com.example.myliushibuju.presenter;

public interface Presenter {

    void startRequse(String url);
}

PresenterImpl

package com.example.myliushibuju.presenter;

import com.example.myliushibuju.callback.MyCallBack;
import com.example.myliushibuju.model.ModelImpl;
import com.example.myliushibuju.view.IView;

public class PresenterImpl implements Presenter {
    private ModelImpl model;
    private IView iView;

    public PresenterImpl(IView iView) {
        this.iView = iView;
        model=new ModelImpl();
    }

    @Override
    public void startRequse(final String url) {

        model.getData(url, new MyCallBack() {
            @Override
            public void setData(Object user) {
                iView.success(user);
            }

            @Override
            public void setError(Object error) {
            iView.error(error);
            }
        });
    }
}

布局文件

MainActivity布局




    
        

      
    

    

        

        
        
    




Group布局




    


Child布局



    

    

        
        
        
    






自定义布局




    

    

       




你可能感兴趣的:(购物车RecycleView)