1. 程式人生 > >京東的分類Sort

京東的分類Sort

MainActivity

package com.qh.xuezhimin.sort;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qh.xuezhimin.sort.adapter.LeftCatagoryAdapter;
import com.qh.xuezhimin.sort.adapter.RightProductAdapter;
import com.qh.xuezhimin.sort.bean.Catagory;
import com.qh.xuezhimin.sort.bean.ProductCatagory;
import com.qh.xuezhimin.sort.left.presenter.LeftPresenter;
import com.qh.xuezhimin.sort.left.view.IView;
import com.qh.xuezhimin.sort.right.presenter.RightPresenter;
import com.qh.xuezhimin.sort.right.view.RightIView;

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

public class MainActivity extends AppCompatActivity implements IView, RightIView {

    private RecyclerView mLeftRec;
    private LinearLayout mRightLnl;
    private List<Catagory.DataBean> beanList;
    private List<ProductCatagory.DataBean> beanRight;
    private LeftCatagoryAdapter leftCatagoryAdapter;
    private RightPresenter rightPresenter;
    private RightProductAdapter rightProductAdapter;


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

        //初始化
        initView();

        //左側佈局管理器
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 1, LinearLayoutManager.VERTICAL, false);
        mLeftRec.setLayoutManager(layoutManager);

        beanList = new ArrayList<>();
        beanRight = new ArrayList<>();
        //左側介面卡
        leftCatagoryAdapter = new LeftCatagoryAdapter(this, beanList);
        mLeftRec.setAdapter(leftCatagoryAdapter);
        //左側條目點選事件
        leftCatagoryAdapter.setOnItemClickListener(new LeftCatagoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemview, int position) {

                Catagory.DataBean dataBean = beanList.get(position);
                rightPresenter.getright("http://www.zhaoapi.cn/product/getProductCatagory?cid=" + dataBean.getCid());

            }
        });


        //例項化p層
        LeftPresenter leftPresenter = new LeftPresenter();
        leftPresenter.LeftPresenter(this);
        leftPresenter.getLeft();

        rightPresenter = new RightPresenter();
        rightPresenter.RightPresenter(this);


    }

    //初始化控制元件
    private void initView() {
        mLeftRec = findViewById(R.id.left_rec);
        mRightLnl = findViewById(R.id.right_lnl);
    }


    @Override
    public void getLeft(List<Catagory.DataBean> list) {
        if (list != null) {
            beanList.clear();
            beanList.addAll(list);
            leftCatagoryAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void getrights(List<ProductCatagory.DataBean> list) {
        if (list != null) {
            mRightLnl.removeAllViews();

            for (int i = 0; i < list.size(); i++) {
                TextView textView = new TextView(this);
                textView.setText(list.get(i).getName());

                RecyclerView rvNextb = new RecyclerView(this);

                RecyclerView.LayoutManager layoutManager2 = new GridLayoutManager(this, 3);
                rvNextb.setLayoutManager(layoutManager2);
                rightProductAdapter = new RightProductAdapter(MainActivity.this, beanRight, list.get(i).getList());
                rvNextb.setAdapter(rightProductAdapter);
                beanRight.clear();
                beanRight.addAll(list);
                rightProductAdapter.notifyDataSetChanged();
                mRightLnl.addView(textView);
                mRightLnl.addView(rvNextb);


            }
        }


    }

    @Override
    public void failed(Exception e) {

    }
}

介面卡
left

package com.qh.xuezhimin.sort.adapter;

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

import com.qh.xuezhimin.sort.R;
import com.qh.xuezhimin.sort.bean.Catagory;

import java.util.List;

public class LeftCatagoryAdapter extends RecyclerView.Adapter<LeftCatagoryAdapter.ViewHolder> {

    private Context context;
    private List<Catagory.DataBean> list;

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


    public interface OnItemClickListener {
        void onItemClick(View itemview, int position);
    }

    private OnItemClickListener clickListener;

    public void setOnItemClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = View.inflate(context, R.layout.left_item, null);
        LeftCatagoryAdapter.ViewHolder holder = new ViewHolder(v);
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.txtleft.setText(list.get(position).getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickListener != null){
                    clickListener.onItemClick(view,position);
                }
            }
        });


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView txtleft;

        public ViewHolder(View itemview) {
            super(itemview);
            txtleft = itemview.findViewById(R.id.txt_left);
        }
    }

}

right

package com.qh.xuezhimin.sort.adapter;

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

import com.bumptech.glide.Glide;
import com.qh.xuezhimin.sort.R;
import com.qh.xuezhimin.sort.bean.ProductCatagory;

import java.util.List;

public class RightProductAdapter extends RecyclerView.Adapter<RightProductAdapter.ViewHolder> {

    private Context context;
    private List<ProductCatagory.DataBean.ListBean> list;

    public RightProductAdapter(Context context, List<ProductCatagory.DataBean> beanRight, List<ProductCatagory.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = View.inflate(context, R.layout.right_item, null);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).getIcon()).into(holder.imageright);
        holder.textright.setText(list.get(position).getName());


    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageright;
        private TextView textright;

        public ViewHolder(View itemView) {
            super(itemView);
            imageright = itemView.findViewById(R.id.img_right);
            textright = itemView.findViewById(R.id.txt_right);
        }
    }


}

Left_MVP
Model

package com.qh.xuezhimin.sort.left.model;

import com.qh.xuezhimin.sort.net.HttpUtils;
import com.qh.xuezhimin.sort.net.ICallBack;

import java.lang.reflect.Type;

public class LeftModel {

    public void getlefts(String url, ICallBack callBack, Type type) {
        HttpUtils.getInstance().get(url, callBack, type);
    }


}

View

package com.qh.xuezhimin.sort.left.view;

import com.qh.xuezhimin.sort.bean.Catagory;

import java.util.List;

public interface IView {

    void getLeft(List<Catagory.DataBean> list);
    void failed(Exception e);


}

Presenter

package com.qh.xuezhimin.sort.left.presenter;

import com.google.gson.reflect.TypeToken;
import com.qh.xuezhimin.sort.bean.Catagory;
import com.qh.xuezhimin.sort.left.model.LeftModel;
import com.qh.xuezhimin.sort.left.view.IView;
import com.qh.xuezhimin.sort.net.ICallBack;

import java.lang.reflect.Type;

public class LeftPresenter {

    //model
    private LeftModel leftModel;
    //view
    private IView iView;

    public void LeftPresenter(IView iView) {
        this.iView = iView;
        leftModel = new LeftModel();
    }

    public void getLeft() {
        Type type = new TypeToken<Catagory>() {
        }.getType();
        leftModel.getlefts("http://www.zhaoapi.cn/product/getCatagory", new ICallBack() {
            @Override
            public void success(Object obj) {
                Catagory catagory = (Catagory) obj;
                if (catagory != null) {
                    iView.getLeft(catagory.getData());
                }
            }

            @Override
            public void failed(Exception e) {
                iView.failed(e);
            }
        }, type);

    }

    public void datach() {
        if (iView != null) {
            iView = null;
        }
    }


}

Right_MVP
Model

package com.qh.xuezhimin.sort.right.model;

import com.qh.xuezhimin.sort.MainActivity;
import com.qh.xuezhimin.sort.net.HttpUtils;
import com.qh.xuezhimin.sort.net.ICallBack;

import java.lang.reflect.Type;

public class RightModel {

    public void getrights(String url, ICallBack callBack, Type type) {
        HttpUtils.getInstance().get(url, callBack, type);
    }


}

View

package com.qh.xuezhimin.sort.right.view;

import com.qh.xuezhimin.sort.bean.ProductCatagory;

import java.util.List;

public interface RightIView {

    void getrights(List<ProductCatagory.DataBean> list);
    void failed(Exception e);


}

Presenter

package com.qh.xuezhimin.sort.right.presenter;

import com.google.gson.reflect.TypeToken;
import com.qh.xuezhimin.sort.bean.ProductCatagory;
import com.qh.xuezhimin.sort.net.ICallBack;
import com.qh.xuezhimin.sort.right.model.RightModel;
import com.qh.xuezhimin.sort.right.view.RightIView;

import java.lang.reflect.Type;

public class RightPresenter {

    private RightIView rightIView;
    private RightModel rightModel;

    public void RightPresenter(RightIView rightIView) {
        this.rightIView = rightIView;
        rightModel = new RightModel();
    }

    public void getright(String url) {
        Type type = new TypeToken<ProductCatagory>() {
        }.getType();

        rightModel.getrights(url, new ICallBack() {
            @Override
            public void success(Object obj) {
                ProductCatagory productCatagory = (ProductCatagory) obj;
                if (productCatagory != null) {
                    rightIView.getrights(productCatagory.getData());
                }

            }

            @Override
            public void failed(Exception e) {
                rightIView.failed(e);
            }
        }, type);


    }

    public void detach() {
        if (rightIView != null) {
            rightIView = null;
        }
    }


}

效果
在這裡插入圖片描述