mvp实现Xrecyclerview的上下拉和购物车功能(仿饿了么)

首先先导入咱们的依赖

implementation 'com.android.support:design:28.0.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.2.5'
    implementation 'com.squareup.okhttp3:okhttp:3.12.0'
    implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.youth.banner:banner:1.4.10'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'

其次就是我们要用到的权限

<uses-permission android:name="com.dingtao.week3.permission.JPUSH_MESSAGE"/>
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

然后就开始写 我们的主页面了

<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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <ImageView
            android:id="@+id/btn_back"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:padding="15dp"
            android:src="@drawable/btn_back" />

        <ImageView
            android:id="@+id/btn_layout"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:padding="15dp"
            android:src="@drawable/even_city_draw" />

        <EditText
            android:id="@+id/edit_keywords"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@+id/btn_layout"
            android:layout_toRightOf="@+id/btn_back"
            android:background="@drawable/search_edit_bg"
            android:hint="搜索"
            android:paddingLeft="40dp"
            android:singleLine="true"
            android:text="手机"
            android:textColor="@color/custom_gray"
            android:textColorHint="@color/grayblack"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/btn_search"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@+id/edit_keywords"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:src="@android:drawable/ic_search_category_default" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignRight="@+id/edit_keywords"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:src="@drawable/person_list_sound_up" />
    </RelativeLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:src="@android:color/darker_gray" />

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

        <TextView
            android:id="@+id/cate_text1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="综合▲"
            android:textColor="@color/set_font_color"
            android:textSize="14sp"></TextView>

        <TextView
            android:id="@+id/cate_text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="价格▲"
            android:textColor="@color/set_font_color"
            android:textSize="14sp"></TextView>

        <TextView
            android:id="@+id/cate_text3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="销量▲"
            android:textColor="@color/set_font_color"
            android:textSize="14sp"></TextView>

        <TextView
            android:id="@+id/cate_text4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="筛选▲"
            android:textColor="@color/set_font_color"
            android:textSize="14sp"></TextView>
    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:src="@android:color/darker_gray" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.jcodecraeer.xrecyclerview.XRecyclerView
            android:id="@+id/list_goods"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.jcodecraeer.xrecyclerview.XRecyclerView>
        <ImageView
            android:id="@+id/shop_car"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="50dp"
            android:src="@drawable/gouwuc_r"/>
    </RelativeLayout>
</LinearLayout>

布局中有自己写的shap还有自己的照片文件记得导入带drawable-xhdpi下

而xml文件则直接 导入到drawable文件下

先写bean类

public class Goods implements Serializable {


//        "bargainPrice": 111.99,
//            "createtime": "2017-10-14T21:39:05",
//            "detailUrl": "https:\/\/item.m.jd.com\/product\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends",
//            "images": "https:\/\/m.360buyimg.com\/n0\/jfs\/t9004\/210\/1160833155\/647627\/ad6be059\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\/\/m.360buyimg.com\/n0\/jfs\/t7504\/338\/63721388\/491286\/f5957f53\/598e95f1N7f2adb87.jpg!q70.jpg|https:\/\/m.360buyimg.com\/n0\/jfs\/t7441\/10\/64242474\/419246\/adb30a7d\/598e95fbNd989ba0a.jpg!q70.jpg",
//            "itemtype": 1,
//            "pid": 1,
//            "price": 118.0,
//            "pscid": 1,
//            "salenum": 0,
//            "sellerid": 17,
//            "subhead": "每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下",
//            "title": "北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"

    private double bargainPrice;
    private String createtime;
    private String detailUrl;
    private String images;
    private int num;
    private int pid;
    private double price;
    private int pscid;
    private int selected;
    private int sellerid;
    private String subhead;
    private String title;
    private int count=1;

    public void setCount(int count) {
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    public double getBargainPrice() {
        return bargainPrice;
    }

    public void setBargainPrice(double bargainPrice) {
        this.bargainPrice = bargainPrice;
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public String getDetailUrl() {
        return detailUrl;
    }

    public void setDetailUrl(String detailUrl) {
        this.detailUrl = detailUrl;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getPscid() {
        return pscid;
    }

    public void setPscid(int pscid) {
        this.pscid = pscid;
    }

    public int getSelected() {
        return selected;
    }

    public void setSelected(int selected) {
        this.selected = selected;
    }

    public int getSellerid() {
        return sellerid;
    }

    public void setSellerid(int sellerid) {
        this.sellerid = sellerid;
    }

    public String getSubhead() {
        return subhead;
    }

    public void setSubhead(String subhead) {
        this.subhead = subhead;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
public class Result<T> {
    int code;
    String msg;
    T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
public class Shop {
    List<Goods> list;
    String sellerName;
    String sellerid;
    int textColor = 0xffffffff;
    int background = R.color.grayblack;

    boolean check;


    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setBackground(int background) {
        this.background = background;
    }

    public int getBackground() {
        return background;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public boolean isCheck() {
        return check;
    }

    public List<Goods> getList() {
        return list;
    }

    public void setList(List<Goods> list) {
        this.list = list;
    }

    public String getSellerName() {
        return sellerName;
    }

    public void setSellerName(String sellerName) {
        this.sellerName = sellerName;
    }

    public String getSellerid() {
        return sellerid;
    }

    public void setSellerid(String sellerid) {
        this.sellerid = sellerid;
    }
}

core类

public interface DataCall<T> {

    void success(T data);

    void fail(Result result);

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

}
public class DTApplication extends Application {

    private static DTApplication instance;
    private SharedPreferences mSharedPreferences;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        mSharedPreferences = getSharedPreferences("application",
                Context.MODE_PRIVATE);
    }

    public static DTApplication getInstance() {
        return instance;
    }

    public SharedPreferences getShare() {
        return mSharedPreferences;
    }

}
//写完之后记得配置清单文件

util层

public class HttpUtils {
    public static String get(String urlString){

        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder().url(urlString).get().build();

        try {
            Response response = okHttpClient.newCall(request).execute();
            String result = response.body().string();
            Log.i("dt","请求结果:"+result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String postForm(String url,String[] name,String[] value){

        OkHttpClient okHttpClient = new OkHttpClient();

        FormBody.Builder formBuild = new FormBody.Builder();
        for (int i = 0; i < name.length; i++) {
            formBuild.add(name[i],value[i]);
        }

        Request request = new Request.Builder().url(url).post(formBuild.build()).build();

        try {
            Response response = okHttpClient.newCall(request).execute();

            String result = response.body().string();
            Log.i("dt",result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String postFile(String url,String[] name,String[] value,String fileParamName,File file){

        OkHttpClient okHttpClient = new OkHttpClient();

        MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if(file != null){
            // MediaType.parse() 里面是上传的文件类型。
            RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
            String filename = file.getName();
            // 参数分别为: 文件参数名 ,文件名称 , RequestBody
            requestBody.addFormDataPart(fileParamName, "jpg", body);
        }
        if (name!=null) {
            for (int i = 0; i < name.length; i++) {
                requestBody.addFormDataPart(name[i], value[i]);
            }
        }

        Request request = new Request.Builder().url(url).post(requestBody.build()).build();

        try {
            Response response = okHttpClient.newCall(request).execute();
            if (response.code()==200) {
                return response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String postJson(String url,String jsonString){

        OkHttpClient okHttpClient = new OkHttpClient();

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),jsonString);

        Request request = new Request.Builder().url(url).post(requestBody).build();

        try {
            Response response = okHttpClient.newCall(request).execute();

            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
public class GlideImageLoader extends ImageLoader {
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
        /**
          注意:
          1.图片加载器由自己选择,这里不限制,只是提供几种使用方法
          2.返回的图片路径为Object类型,由于不能确定你到底使用的那种图片加载器,
          传输的到的是什么格式,那么这种就使用Object接收和返回,你只需要强转成你传输的类型就行,
          切记不要胡乱强转!
         */

        //Glide 加载图片简单用法
        Glide.with(context).load(path).into(imageView);

        //Picasso 加载图片简单用法
//        Picasso.with(context).load(path).into(imageView);
        
        //用fresco加载图片简单用法,记得要写下面的createImageView方法
//        Uri uri = Uri.parse((String) path);
//        imageView.setImageURI(uri);
    }
    
    //提供createImageView 方法,如果不用可以不重写这个方法,主要是方便自定义ImageView的创建
//    @Override
//    public ImageView createImageView(Context context) {
//        //使用fresco,需要创建它提供的ImageView,当然你也可以用自己自定义的具有图片加载功能的ImageView
//        SimpleDraweeView simpleDraweeView=new SimpleDraweeView(context);
//        return simpleDraweeView;
//    }
}

这个是设置图片的东西

然后设置日志

public class SpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spacing;

    public SpacingItemDecoration(int spacing) {
        this.spacing = spacing;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.top = spacing / 2;
        outRect.bottom = spacing / 2;
        outRect.left = spacing / 2;
        outRect.right = spacing / 2;
    }
}

写的自定义view

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

    @SuppressLint("NewApi")
    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.car_add_sub_layout,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);
    }
}

model层

public class GoodsListModel {

    public static Result goodsList(String keywords, final String page) {
        String resultString = HttpUtils.postForm("http://www.zhaoapi.cn/product/searchProducts",
                new String[]{"keywords", "page"}, new String[]{keywords, page});

        try {
            Gson gson = new Gson();

            Type type = new TypeToken<Result<List<Goods>>>() {
            }.getType();

            Result result = gson.fromJson(resultString, type);
//        Result<List<Goods>> result = new Result<>();
//        result.setCode(0);
//        List<Goods> list = new ArrayList<>();
//        for (int i = 0; i < 30; i++) {
//            Goods goods = new Goods();
//            goods.setImages("");
//            goods.setTitle("手机"+i);
//            list.add(goods);
//        }
//        result.setData(list);

            return result;
        } catch (Exception e) {

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

}

presenter层

public class GoodsListPresenter extends BasePresenter {

    private int page=1;
    private boolean isRefresh=true;

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

    @Override
    protected Result getData(Object... args) {
        isRefresh = (boolean) args[0];//是否需要刷新
        if (isRefresh){//刷新
            page = 1;
        }else{
            page++;
        }
        Result result = GoodsListModel.goodsList((String)args[1],page+"");//调用网络请求获取数据
        return result;
    }

    public boolean isResresh(){
        return isRefresh;
    }
}

给主页面的xrecycleview设置适配器

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.GoodsHodler> {

    private List<Goods> mList = new ArrayList<>();//数据集合
    private Context context;


    public final static int LINEAR_TYPE = 0;//线性
    public final static int GRID_TYPE = 1;//网格

    private int viewType = LINEAR_TYPE;

    private OnItemClickListener onItemClickListener;

    private OnItemLongClickListener onItemLongClickListener;

    public GoodsListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        return viewType;
    }

    //设置item的视图类型
    public void setViewType(int viewType) {
        this.viewType = viewType;
    }


    @NonNull
    @Override
    public GoodsHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = null;
        if (viewType == LINEAR_TYPE) {//通过第二个参数viewType判断选用的视图
            view = View.inflate(viewGroup.getContext(), R.layout.goods_linear_item, null);//加载item布局
        } else {
            view = View.inflate(viewGroup.getContext(), R.layout.goods_grid_item, null);//加载item布局
        }
        GoodsHodler goodsHodler = new GoodsHodler(view);

        return goodsHodler;
    }

    @Override
    public void onBindViewHolder(@NonNull final GoodsHodler goodsHodler, final int position) {
        final Goods goods = mList.get(position);//拿到商品,开始赋值

        goodsHodler.itemView.setTag(mList.get(position));

        //增加点击事件
        goodsHodler.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //*******跳转webActivity进行网页访问
//                Intent intent = new Intent(context,WebActivity.class);
//                intent.putExtra("url",goods.getDetailUrl());
//                context.startActivity(intent);

                //————————跳转自定义的详情页面
                Intent intent = new Intent(context,GoodsDetailActivity.class);
                Bundle bundle = new Bundle();//使用bundle传递引用数据类型的对象
                bundle.putSerializable("goods",goods);
                intent.putExtras(bundle);//一定要把值放入了。
                context.startActivity(intent);

//                if (onItemClickListener!=null) {
//                onItemClickListener.onItemClick(goods);
//                }
            }
        });
        goodsHodler.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //*****************方案1*************
//                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                        goodsHodler.itemView,"alpha",1,0
//                );
//                objectAnimator.setDuration(1000);
//                objectAnimator.setInterpolator(new LinearInterpolator());
//                objectAnimator.addListener(new Animator.AnimatorListener() {
//                    @Override
//                    public void onAnimationStart(Animator animation) {
//
//                    }
//
//                    @Override
//                    public void onAnimationEnd(Animator animation) {
//                        mList.remove(position);//动画执行结束,移除
//                        notifyDataSetChanged();//刷新列表
//                        goodsHodler.itemView.setAlpha(1);//由于复用机制,需要重新改成不透明
//                    }
//
//                    @Override
//                    public void onAnimationCancel(Animator animation) {
//                        mList.remove(position);//动画执行结束,移除
//                        notifyDataSetChanged();//刷新列表
//                        goodsHodler.itemView.setAlpha(1);//由于复用机制,需要重新改成不透明
//                    }
//
//                    @Override
//                    public void onAnimationRepeat(Animator animation) {
//
//                    }
//                });
//                objectAnimator.start();

                //***********方案二**************
                if (onItemLongClickListener!=null) {
                    onItemLongClickListener.onItemLongClick(position);
                }
                return true;
            }
        });

        goodsHodler.text.setText(goods.getTitle());
        //由于我们的数据图片提供的不标准,所以我们需要切割得到图片
        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(goodsHodler.imageView);//加载图片
    }

    public static void main(String[] args) {
        String aa = "a111a222a333a";
        String[] b = aa.split("a");
        System.out.println(b[0]);
    }

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

    /**
     * 添加集合数据
     */
    public void addAll(List<Goods> data) {
        if (data != null) {
            mList.addAll(data);
        }
    }

    /**
     * 清空数据
     */
    public void clearList() {
        mList.clear();
    }

    /**
     * 设置点击方法
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void remove(int position) {
        mList.remove(position);
    }

    public class GoodsHodler extends RecyclerView.ViewHolder {
        TextView text;
        ImageView imageView;

        public GoodsHodler(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
            imageView = itemView.findViewById(R.id.image);
        }
    }

    /**
     * @author dingtao
     * @date 2018/12/15 10:28 AM
     * 点击接口
     */
    public interface OnItemClickListener {
        void onItemClick(Goods goods);
    }

    /**
     * @author dingtao
     * @date 2018/12/15 10:28 AM
     * 点击接口
     */
    public interface OnItemLongClickListener {
        void onItemLongClick(int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
}

列表条目布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:minHeight="50dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aa"
        android:padding="10dp"/>

</LinearLayout>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_margin="10dp"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:minHeight="50dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aa"
        android:padding="10dp"/>

</LinearLayout>

完成

设置点击条目进入详情页面

详情页布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <com.youth.banner.Banner
                android:id="@+id/goods_banner"
                android:layout_width="match_parent"
                android:layout_height="200dp" />
        </LinearLayout>

    </ScrollView>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <Button
            android:id="@+id/goods_add_cart_btn"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@color/mark_red"
            android:textColor="@color/white"
            android:layout_alignParentRight="true"
            android:text="加入购物车"/>
    </RelativeLayout>
</LinearLayout>

详情页功能实现

public class GoodsDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private Goods mGoods;//商品详情

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

        Bundle bundle = getIntent().getExtras();
        mGoods = (Goods) bundle.getSerializable("goods");//读取列表传递过来的商品数据

        initBanner();

        findViewById(R.id.goods_add_cart_btn).setOnClickListener(this);

    }

    /**
     * 初始化banner
     */
    private void initBanner() {

        List<String> imageList = new ArrayList<>();//图片url集合
        String[] imageurls = mGoods.getImages().split("https");//对图片进行切割
        for (int i = 0; i < imageurls.length; i++) {
            if (!TextUtils.isEmpty(imageurls[i])) {
                String url = "https" + imageurls[i];
                Log.i("dt", "imageUrl: " + url);
                url = url.substring(0, url.lastIndexOf(".jpg") + ".jpg".length());
                imageList.add(url);//图片路径拼接完成,重新赋值给数组
            }
        }

        Banner banner = findViewById(R.id.goods_banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置banner样式
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);

        //设置图片集合
        banner.setImages(imageList);
        //设置标题集合(当banner样式有显示title时)
//        banner.setBannerTitles(titleList);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置自动轮播,默认为true
        banner.isAutoPlay(false);
        //设置轮播时间
//        banner.setDelayTime(1500);
        //设置指示器位置(当banner模式中有指示器时)
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.goods_add_cart_btn){
            Toast.makeText(this,"加入购物车",Toast.LENGTH_LONG).show();
        }
    }
}

点击购物车进入购物车页面

购物车页面的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/left_recycler"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="@color/grayblack">

        </android.support.v7.widget.RecyclerView>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/right_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <ImageView
            android:id="@+id/shop_car_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:src="@drawable/gouwuc_r" />
        <TextView
            android:id="@+id/goods_sum_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="价格:"
            android:layout_marginLeft="20dp"
            android:layout_centerVertical="true"/>

        <TextView
            android:id="@+id/goods_number"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:textSize="10sp"
            android:gravity="center"
            android:textColor="@color/white"
            android:layout_marginLeft="-10dp"
            android:background="@drawable/circle_red_bg"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/shop_car_image"
            android:text="7"
            tools:ignore="RtlCompat" />
    </RelativeLayout>
</LinearLayout>

model层

public class CartModel {

    public static Result goodsList() {
//        String resultString = HttpUtils.get("http://www.zhaoapi.cn/product/getCarts?uid=71");
        String resultString = "{\"msg\":\"请求成功\",\"code\":\"0\",\"data\":[{\"list\":[],\"sellerName\":\"\",\"sellerid\":\"0\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":11,\"pid\":11,\"price\":8989.0,\"pscid\":1,\"selected\":0,\"sellerid\":4,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家4\",\"sellerid\":\"4\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-03T23:43:53\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":1,\"pid\":13,\"price\":465.0,\"pscid\":1,\"selected\":0,\"sellerid\":6,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家6\",\"sellerid\":\"6\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":63,\"price\":10000.0,\"pscid\":40,\"selected\":0,\"sellerid\":7,\"subhead\":\"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)\",\"title\":\"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5\\/8G\\/256G\"}],\"sellerName\":\"商家7\",\"sellerid\":\"7\"},{\"list\":[{\"bargainPrice\":399.0,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5887\\/201\\/859509257\\/69994\\/6bde9bf6\\/59224c24Ne854e14c.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5641\\/233\\/853609022\\/57374\\/5c73d281\\/59224c24N3324d5f4.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5641\\/233\\/853609022\\/57374\\/5c73d281\\/59224c24N3324d5f4.jpg!q70.jpg\",\"num\":1,\"pid\":87,\"price\":888.0,\"pscid\":85,\"selected\":0,\"sellerid\":8,\"subhead\":\"满2件,总价打6.50折\",\"title\":\"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33\\/32(175\\/84A)\"},{\"bargainPrice\":3455.0,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9106\\/106\\/1785172479\\/537280\\/253bc0ab\\/59bf78a7N057e5ff7.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9106\\/106\\/1785172479\\/537280\\/253bc0ab\\/59bf78a7N057e5ff7.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8461\\/5\\/1492479653\\/68388\\/7255e013\\/59ba5e84N91091843.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8461\\/5\\/1492479653\\/68388\\/7255e013\\/59ba5e84N91091843.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8803\\/356\\/1478945529\\/489755\\/2a163ace\\/59ba5e84N7bb9a666.jpg!q70.jpg\",\"num\":2,\"pid\":52,\"price\":666.0,\"pscid\":39,\"selected\":0,\"sellerid\":8,\"subhead\":\"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机\",\"title\":\"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】\"}],\"sellerName\":\"商家8\",\"sellerid\":\"8\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":65,\"price\":12000.0,\"pscid\":40,\"selected\":0,\"sellerid\":9,\"subhead\":\"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)\",\"title\":\"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5\\/8G\\/256G\"},{\"bargainPrice\":2999.0,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/2385655.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2068\\/298\\/2448145915\\/157953\\/7be197df\\/56d51a42Nd86f1c8e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2437\\/128\\/1687178395\\/117431\\/bcc190c1\\/56d3fcbaNb2963d21.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2467\\/222\\/2263160610\\/95597\\/927b8a2f\\/56d3eafeNdecebeb6.jpg!q70.jpg\",\"num\":2,\"pid\":53,\"price\":777.0,\"pscid\":39,\"selected\":0,\"sellerid\":9,\"subhead\":\"Super AMOLED三星双曲面2K 屏,支持无线充电!\",\"title\":\"三星 Galaxy S7 edge(G9350)4GB+32GB 铂光金 移动联通电信4G手机 双卡双待\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":1,\"pid\":16,\"price\":199.0,\"pscid\":1,\"selected\":0,\"sellerid\":9,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家9\",\"sellerid\":\"9\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":66,\"price\":13000.0,\"pscid\":40,\"selected\":0,\"sellerid\":10,\"subhead\":\"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)\",\"title\":\"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5\\/8G\\/256G\"},{\"bargainPrice\":159.0,\"createtime\":\"2017-10-14T21:49:15\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8716\\/197\\/1271594444\\/173291\\/2f40bb4f\\/59b743bcN8509428e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8347\\/264\\/1286771527\\/92188\\/5cf5ec04\\/59b7420fN65378e9e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7363\\/165\\/3000956253\\/190883\\/179a372\\/59b743bfNd0c79d93.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7399\\/112\\/2935531768\\/183594\\/b77c7d4a\\/59b7441aNc3d40133.jpg!q70.jpg\",\"num\":5,\"pid\":99,\"price\":2100.0,\"pscid\":112,\"selected\":0,\"sellerid\":10,\"subhead\":\"针织针织闪闪闪亮你的眼\",\"title\":\"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":2,\"pid\":17,\"price\":299.0,\"pscid\":1,\"selected\":0,\"sellerid\":10,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家10\",\"sellerid\":\"10\"},{\"list\":[{\"bargainPrice\":159.0,\"createtime\":\"2017-10-14T21:49:15\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8716\\/197\\/1271594444\\/173291\\/2f40bb4f\\/59b743bcN8509428e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8347\\/264\\/1286771527\\/92188\\/5cf5ec04\\/59b7420fN65378e9e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7363\\/165\\/3000956253\\/190883\\/179a372\\/59b743bfNd0c79d93.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7399\\/112\\/2935531768\\/183594\\/b77c7d4a\\/59b7441aNc3d40133.jpg!q70.jpg\",\"num\":1,\"pid\":100,\"price\":2200.0,\"pscid\":112,\"selected\":0,\"sellerid\":11,\"subhead\":\"针织针织闪闪闪亮你的眼\",\"title\":\"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL\"},{\"bargainPrice\":22.9,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t1930\\/284\\/2865629620\\/390243\\/e3ade9c4\\/56f0a08fNbd3a1235.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2137\\/336\\/2802996626\\/155915\\/e5e90d7a\\/56f0a09cN33e01bd0.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t1882\\/31\\/2772215910\\/389956\\/c8dbf370\\/56f0a0a2Na0c86ea6.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2620\\/166\\/2703833710\\/312660\\/531aa913\\/57709035N33857877.jpg!q70.jpg\",\"num\":5,\"pid\":34,\"price\":9.0,\"pscid\":2,\"selected\":0,\"sellerid\":11,\"subhead\":\"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》\",\"title\":\"三只松鼠 坚果炒货 零食奶油味 碧根果225g\\/袋\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":2,\"pid\":18,\"price\":399.0,\"pscid\":1,\"selected\":0,\"sellerid\":11,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家11\",\"sellerid\":\"11\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":3,\"pid\":19,\"price\":499.0,\"pscid\":1,\"selected\":0,\"sellerid\":12,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家12\",\"sellerid\":\"12\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":3,\"pid\":1,\"price\":118.0,\"pscid\":1,\"selected\":0,\"sellerid\":17,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家17\",\"sellerid\":\"17\"}]}";
        try {
            Gson gson = new Gson();

            Type type = new TypeToken<Result<List<Shop>>>() {
            }.getType();

            Result result = gson.fromJson(resultString, type);
//        Result<List<Goods>> result = new Result<>();
//        result.setCode(0);
//        List<Goods> list = new ArrayList<>();
//        for (int i = 0; i < 30; i++) {
//            Goods goods = new Goods();
//            goods.setImages("");
//            goods.setTitle("手机"+i);
//            list.add(goods);
//        }
//        result.setData(list);

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

}

两个recycleview 的条目布局

<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/left_text"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:textSize="16sp"
        android:gravity="center"
        android:textColor="@color/white"
        android:text="aa" />
</LinearLayout>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:minHeight="50dp"
        android:layout_alignParentLeft="true"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_toRightOf="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="aa"
        android:padding="10dp"/>
    <TextView
        android:id="@+id/text_price"
        android:layout_toRightOf="@+id/image"
        android:layout_below="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="价格"
        android:padding="10dp"/>

    <com.example.yue3.util.view.AddSubLayout
        android:id="@+id/add_sub_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp">

    </com.example.yue3.util.view.AddSubLayout>

</RelativeLayout>

加减布局

<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="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/btn_add"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:focusable="false"
        android:textSize="20sp"
        android:gravity="center"
        android:text="+" />

    <TextView
        android:id="@+id/text_number"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:textSize="14sp"
        android:text="1000" />

    <TextView
        android:id="@+id/btn_sub"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:textSize="20sp"
        android:focusable="false"
        android:gravity="center"
        android:text="-" />
</LinearLayout>

适配器

两个recycleview 的适配器

public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.MyHolder> {

    private List<Shop> mList = new ArrayList<>();

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

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

    @Override
    public void onBindViewHolder(@NonNull final 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 <mList.size() ; j++) {
                    mList.get(j).setTextColor(0xffffffff);
                    mList.get(j).setBackground(R.color.grayblack);
                }
                shop.setBackground(R.color.white);
                shop.setTextColor(0xff000000);
                notifyDataSetChanged();
                onItemClickListenter.onItemClick(shop);//切换右边的列表
            }
        });
    }

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

    public List<Shop> getList() {
        return mList;
    }

    class MyHolder extends RecyclerView.ViewHolder{

        TextView text;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.left_text);
        }
    }

    private OnItemClickListenter onItemClickListenter;

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

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

}
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ChildHolder> {

    private List<Goods> mList = new ArrayList<>();

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

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

    @Override
    public void onBindViewHolder(@NonNull ChildHolder childHolder, int position) {

        final Goods goods = mList.get(position);
        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(DTApplication.getInstance()).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 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();
    }
}

页面实现

public class ShopCartActivity1 extends AppCompatActivity implements
        DataCall<List<Shop>> {

    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_cart);

        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();
        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();
        mRightAdapter.setOnNumListener(new RightAdapter.OnNumListener() {
            @Override
            public void onNum() {
                calculatePrice(mLeftAdapter.getList());
            }
        });
        mRightRecycler.setAdapter(mRightAdapter);

        cartPresenter.requestData();
    }

    @Override
    public void success(List<Shop> 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();
    }

    /**
     * @author dingtao
     * @date 2018/12/18 7:01 PM
     * 计算总价格
     */
    private void calculatePrice(List<Shop> shopList){
        double totalPrice=0;
        int totalNum = 0;
        for (int i = 0; i < shopList.size(); i++) {//循环的商家
            Shop shop = shopList.get(i);
            for (int j = 0; j < shop.getList().size(); j++) {
                Goods goods = shop.getList().get(j);
                //计算价格
                totalPrice = totalPrice + goods.getNum() * goods.getPrice();
                totalNum+=goods.getNum();//计数
            }
        }
        mSumPrice.setText("价格:"+totalPrice);
        mCount.setText(""+totalNum);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_42793219/article/details/85137749