京东购物车左分类右显示

先看下效果 (代码里有三个接口回调,左边适配器有一个,是为了把下标传到Mainactivity的页面里获得右边的数据,因为用的一个接口,第二个适配器在右边的适配器里,是为了刷新自定义view里的加减和刷新Mainactivity里的总价格跟着动,第三个适配器在自定义view里是为了回传从数据里拿到的count,大家认真点看就完了)

京东购物车左分类右显示

 先看主布局左边一个recycler右边一个recycler

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/left"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/right"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/jiage"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="价格"/>
        <TextView
            android:id="@+id/jiesuan"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="去结算(0)"/>

    </LinearLayout>



</LinearLayout>

 Mainactivity

package com.example.bwie.gouwuche;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telecom.Call;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bwie.gouwuche.adapter.LeftAdapter;
import com.example.bwie.gouwuche.adapter.RightAdapter;
import com.example.bwie.gouwuche.bean.MyBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class MainActivity extends AppCompatActivity {

    private RecyclerView left;
    private RecyclerView right;
    private TextView jiage;
    private TextView jiesuan;
    private List<MyBean.DataBean> list=new ArrayList<>();
    private List<MyBean.DataBean.SpusBean> list1=new ArrayList<>();
    private LeftAdapter adapter;
    private String mUrl="http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            String s= (String) msg.obj;
            Gson gson=new Gson();
            MyBean myBean = gson.fromJson(s, MyBean.class);
            list.addAll(myBean.getData());
            adapter.notifyDataSetChanged();
        }
    };
    private RightAdapter adapter1;
    private List<MyBean.DataBean.SpusBean> spus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        adapter = new LeftAdapter(list,MainActivity.this);
        adapter1 = new RightAdapter(list1,MainActivity.this);
        right.setAdapter(adapter1);
        left.setAdapter(adapter);
        getData();
        adapter.setLeftShuju(new LeftAdapter.setLeft() {
            @Override
            public void LeftChecked(View v, int position) {
                list1.clear();
                spus = list.get(position).getSpus();
                for (int i = 0; i < spus.size(); i++) {
                    spus.get(i).setPraise_num(0);
                }
                list1.addAll(spus);
                adapter1.notifyDataSetChanged();
            }
        });
        adapter1.setCallBack(new RightAdapter.AdapterCallBack() {
            @Override
            public void shuaxin() {
                float price=0;
                int number=0;
                for (int i = 0; i < spus.size(); i++) {
                    price+=spus.get(i).getPraise_num()*Float.valueOf(spus.get(i).getSkus().get(0).getPrice());
                }
                for (int j = 0; j < spus.size(); j++) {
                    number+=spus.get(j).getPraise_num();
                }
                jiage.setText(price+"");
                jiesuan.setText(number+"");
            }
        });
    }
    private void initView() {
        left = (RecyclerView) findViewById(R.id.left);
        right = (RecyclerView) findViewById(R.id.right);
        jiage = (TextView) findViewById(R.id.jiage);
        jiesuan = (TextView) findViewById(R.id.jiesuan);
        left.setLayoutManager(new LinearLayoutManager(this));
        right.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    }
    private void getData() {
        OkHttpClient okHttpClient=new OkHttpClient();
        Request request=new Request.Builder().url(mUrl).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {

            }
            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                handler.sendMessage(handler.obtainMessage(0,response.body().string()));
            }
        });
    }
}

 左边的适配器

package com.example.bwie.gouwuche.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.TextView;

import com.example.bwie.gouwuche.R;
import com.example.bwie.gouwuche.bean.MyBean;

import java.util.List;

public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.ViewHolder> {
    private List<MyBean.DataBean> list;
    private Context context;

    public LeftAdapter(List<MyBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public LeftAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=LayoutInflater.from(context).inflate(R.layout.left,null);
        ViewHolder viewHolder=new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull LeftAdapter.ViewHolder viewHolder, final int i) {
        viewHolder.name.setText(list.get(i).getName());
        viewHolder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shuju!=null){
                    shuju.LeftChecked(v,i);
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.name);
        }
    }
    public interface setLeft{
        void LeftChecked(View v,int position);
    }
    private setLeft shuju;
    public void setLeftShuju(setLeft shuju){
        this.shuju=shuju;
    }
}

右边的适配器

package com.example.bwie.gouwuche.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 com.bumptech.glide.Glide;
import com.example.bwie.gouwuche.R;
import com.example.bwie.gouwuche.bean.MyBean;
import com.example.bwie.gouwuche.weight.JiajianView;

import java.util.List;

public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ViewHolder> {
    List<MyBean.DataBean.SpusBean> list;
    private Context context;

    public RightAdapter(List<MyBean.DataBean.SpusBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RightAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(context).inflate(R.layout.right, null);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.right_title.setText(list.get(i).getName());
        viewHolder.right_price.setText(list.get(i).getSkus().get(0).getPrice());
        Glide.with(context).load(list.get(i).getPic_url()).into(viewHolder.right_icon);
        viewHolder.jiajianView.setNum(list.get(i).getPraise_num());
        viewHolder.jiajianView.setRightshuju(new JiajianView.setRight() {
            @Override
            public void Rightshuju(int count) {
                list.get(i).setPraise_num(count);
                notifyDataSetChanged();
                adapterCallBack.shuaxin();
            }
        });

    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    public interface AdapterCallBack{
        void shuaxin();
    }
    AdapterCallBack adapterCallBack;
    public void setCallBack(AdapterCallBack adapterCallBack){
       this.adapterCallBack=adapterCallBack;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView right_title;
        TextView right_price;
        ImageView right_icon;
        JiajianView jiajianView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            right_title = itemView.findViewById(R.id.right_title);
            right_price = itemView.findViewById(R.id.right_price);
            right_icon = itemView.findViewById(R.id.right_icon);
            jiajianView=itemView.findViewById(R.id.Jia_Jian_View);
        }
    }
}

左布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

右布局(里边有一个自定义view)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/right_icon"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="20dp"
        android:scaleType="centerCrop"
        android:src="@color/colorPrimary" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/right_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:text="商品标题" />

        <TextView
            android:id="@+id/right_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="¥0.0" />

    </LinearLayout>

    <com.example.bwie.gouwuche.weight.JiajianView
        android:id="@+id/Jia_Jian_View"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></com.example.bwie.gouwuche.weight.JiajianView>
</LinearLayout>

自定义view类

package com.example.bwie.gouwuche.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.bwie.gouwuche.R;

public class JiajianView extends LinearLayout implements View.OnClickListener {
    private TextView delete_tv;
    private TextView product_number_tv;
    private TextView add_tv;
    private int count=0;

    public JiajianView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.add_remove_view_layout, this);
        initView();
    }

    private void initView() {
        delete_tv=findViewById(R.id.delete_tv);
        product_number_tv=findViewById(R.id.product_number_tv);
        add_tv=findViewById(R.id.add_tv);
        delete_tv.setOnClickListener(this);
        add_tv.setOnClickListener(this);
    }
    public void  setNum(int number){
        this.count=number;
        if(count==0){
            delete_tv.setVisibility(GONE);
        }else{
            delete_tv.setVisibility(VISIBLE);
        }
        product_number_tv.setText(count+"");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.delete_tv:
                count--;
                if (count<0){
                    Toast.makeText(getContext(), "商品已售空!!", Toast.LENGTH_SHORT).show();
                    return;
                }
                product_number_tv.setText(count+"");
                if (setright!=null){
                    setright.Rightshuju(count);
                }
                break;
            case R.id.add_tv:
                count++;
                product_number_tv.setText(count+"");
                if (setright!=null){
                    setright.Rightshuju(count);
                }
                break;
        }
    }
    public interface setRight{
        void Rightshuju(int count);
    }
    private setRight setright;
    public void setRightshuju(setRight setright){
        this.setright=setright;
    }

}

自定义view的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:background="#99000000"
    android:gravity="center_vertical"
    android:padding="2dp">

    <TextView
        android:id="@+id/delete_tv"
        android:layout_width="25dp"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:gravity="center"
        android:text="-"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/product_number_tv"
        android:layout_width="25dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="2dp"
        android:layout_weight="1"
        android:background="#ffffff"
        android:gravity="center"
         />

    <TextView
        android:id="@+id/add_tv"
        android:layout_width="25dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="2dp"
        android:background="#ffffff"
        android:gravity="center"
        android:text="+"
        android:textSize="16sp" />

</LinearLayout>

完结