1. 程式人生 > >購物車模擬分類表(左recycleView,右2recycleView巢狀)

購物車模擬分類表(左recycleView,右2recycleView巢狀)

MainActivity

package com.zjh.administrat.classify_recycleview.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.zjh.administrat.classify_recycleview.R;
import com.zjh.administrat.classify_recycleview.adapter.LeftAdapter;
import com.zjh.administrat.classify_recycleview.adapter.RightAdapter;
import com.zjh.administrat.classify_recycleview.bean.LeftBean;
import com.zjh.administrat.classify_recycleview.bean.RightBean;
import com.zjh.administrat.classify_recycleview.presenter.IPresenterImpl;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements IView{

    private RecyclerView recyclerLeft, recyclerRight;
    public String urlLeft = "http://www.zhaoapi.cn/product/getCatagory";
    public String urlRight = "http://www.zhaoapi.cn/product/getProductCatagory";
    private IPresenterImpl iPresenter;
    private LeftAdapter mAdapter;
    private RightAdapter mAdapter1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initLeft();
        initRight();
        leftType();
        //LeftAdapter介面回撥獲得cid
        mAdapter.setOnLeftClick(new LeftAdapter.OnLeftClick() {
            @Override
            public void OnClick(int i, String cid) {
                Toast.makeText(MainActivity.this, "點選了"+cid, Toast.LENGTH_SHORT).show();
                rightType(cid);

            }
        });
    }
    //左邊資料
    private void leftType() {
        Map<String, String> map = new HashMap<>();
        iPresenter.PRequestData(urlLeft, map, LeftBean.class);
    }
    private void rightType(String cid){
        Map<String, String> map = new HashMap<>();
        map.put("cid", cid);
        iPresenter.PRequestData(urlRight, map, RightBean.class);
        initRight();
    }

    //左邊操作
    private void initLeft() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerLeft.setLayoutManager(linearLayoutManager);
        mAdapter = new LeftAdapter(this);
        recyclerLeft.setAdapter(mAdapter);
        //分割線
        recyclerLeft.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }
    //右邊資料
    private void initRight() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerRight.setLayoutManager(linearLayoutManager);
        mAdapter1 = new RightAdapter(this);
        recyclerRight.setAdapter(mAdapter1);
        //分割線
        recyclerRight.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    //初始化view
    private void initView() {
        recyclerLeft = findViewById(R.id.recycleLeft);
        recyclerRight = findViewById(R.id.recycleRight);
        iPresenter = new IPresenterImpl(this);
    }

    @Override
    public void viewDatas(Object data) {
        if (data instanceof LeftBean){
            LeftBean leftBean = (LeftBean) data;
            mAdapter.setDatas(leftBean.getData());
        }else if(data instanceof RightBean){
            RightBean rightBean = (RightBean) data;
            mAdapter1.setDatas(rightBean.getData());
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iPresenter.onDetch();
    }
}

LeftAdapter左邊列表

package com.zjh.administrat.classify_recycleview.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.zjh.administrat.classify_recycleview.R;
import com.zjh.administrat.classify_recycleview.bean.LeftBean;

import java.util.ArrayList;
import java.util.List;

public class LeftAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<LeftBean.DataBean> mLeft;
    private Context mContext;

    public LeftAdapter(Context mContext) {
        this.mContext = mContext;
        mLeft = new ArrayList<>();
    }

    public void setDatas(List<LeftBean.DataBean> data) {
        if (data != null){
            mLeft.addAll(data);
        }
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.left_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        ViewHolder mHolder = (ViewHolder) viewHolder;
        mHolder.name.setText(mLeft.get(i).getName());

        //實現介面引數
        mHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnLeftClick != null){
                    mOnLeftClick.OnClick(i, mLeft.get(i).getCid());
                }
            }
        });
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
        }
    }

    //成員變數
    OnLeftClick mOnLeftClick;
    //set方法
    public void setOnLeftClick(OnLeftClick onLeftClick){
        mOnLeftClick = onLeftClick;
    }
    //定義一個介面
    public interface OnLeftClick{
        void OnClick(int i, String cid);
    }

}

RightAdapter右邊商品

package com.zjh.administrat.classify_recycleview.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
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.zjh.administrat.classify_recycleview.R;
import com.zjh.administrat.classify_recycleview.bean.RightBean;

import java.util.ArrayList;
import java.util.List;

public class RightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<RightBean.DataBean> mRight;
    private Context mContext;

    public RightAdapter(Context mContext) {
        this.mContext = mContext;
        mRight = new ArrayList<>();
    }

    public void setDatas(List<RightBean.DataBean> data) {
        if (data != null){
            mRight.addAll(data);
        }
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.right_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ViewHolder mHolder = (ViewHolder) viewHolder;
        mHolder.name.setText(mRight.get(i).getName());

        //商品詳情
        GoodsAdapter gAdapter = new GoodsAdapter(mContext);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mHolder.recyclerData.setLayoutManager(linearLayoutManager);
        mHolder.recyclerData.setAdapter(gAdapter);
        mHolder.recyclerData.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        gAdapter.setDatas(mRight.get(i).getList());

    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        private RecyclerView recyclerData;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            recyclerData = itemView.findViewById(R.id.recycleData);
            name = itemView.findViewById(R.id.name);
        }
    }
}

GoodsAdapter商品

package com.zjh.administrat.classify_recycleview.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.zjh.administrat.classify_recycleview.R;
import com.zjh.administrat.classify_recycleview.bean.RightBean;

import java.util.ArrayList;
import java.util.List;

public class GoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<RightBean.DataBean.ListBean> mList;
    private Context mContext;

    public GoodsAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();
    }
    public void setDatas(List<RightBean.DataBean.ListBean> list) {
        if (list != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.goods_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ViewHolder mHolder = (ViewHolder) viewHolder;
        mHolder.textView.setText(mList.get(i).getName());
        Glide.with(mContext).load(mList.get(i).getIcon()).into(mHolder.imageView);
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}

佈局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="horizontal"
    tools:context=".view.MainActivity">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/recycleLeft"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="3"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleRight"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7"/>

</LinearLayout>

mvp框架,okhttp/post請求資料

購物車分類