1. 程式人生 > >簡單的一個仿京東的分類的架構

簡單的一個仿京東的分類的架構

//Model

//LeftModel


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

}

//RightModel

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

//IView

//IView

 


public interface IView {
    void getleft(List<LeftBean.DataBean> list);
    void failed(Exception e);
}

//RightIView

public interface RightIView {
    void getrights(List<RightBean.DataBean> list);
    void failed(Exception e);
}

//presenter

//LeftPresenter

public class LeftPresenter {
    private IView iv;
    private LeftModel leftModel;
    public void attch(final IView iv){
        this.iv = iv;
        leftModel = new LeftModel();
    }
    public void getleft(){
        Type type = new TypeToken<LeftBean>(){}.getType();
        leftModel.getlefts("http://www.zhaoapi.cn/product/getCatagory", new ICallBack() {

            public void onSuccess(Object obj) {
                LeftBean leftBean = (LeftBean) obj;
                if (leftBean!=null){
                    iv.getleft(leftBean.getData());
                }
            }


            public void onFailed(Exception e) {
                iv.failed(e);
            }
        },type);
    }
    public void detach(){
        if (iv !=null){
            iv = null;
        }
    }
}

//RightPresent

public class RightPresent {
    private RightIView iv;
    private RightModel rightModel;
    public void attch(RightIView iv){
        this.iv = iv;
        rightModel = new RightModel();
    }
    public void getright(String url){
        Type type = new TypeToken<RightBean>(){}.getType();
        rightModel.getrights(url, new ICallBack() {

            public void onSuccess(Object obj) {
                RightBean rightBean = (RightBean) obj;
                if (rightBean!=null){
                    iv.getrights(rightBean.getData());
                }
            }

            @Override
            public void onFailed(Exception e) {
                iv.failed(e);
            }
        },type);
    }
    public void detach(){
        if (iv !=null){
            iv = null;
        }
    }
}

 

/MainActivityMainActivity

public class MainActivity extends AppCompatActivity implements IView,RightIView {
    private RecyclerView recleft;
    private LeftAdapter leftAdapter;
    private List<LeftBean.DataBean> beanList;
    private LeftPresenter leftPresenter;
    private LinearLayout layoutright;
    private RightAdapter rightAdapter;
    private RightPresent rightPresenter;
    private List<RightBean.DataBean> beanright;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recleft= findViewById(R.id.rec_left);
        layoutright=findViewById(R.id.right_lnl);


        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,1, LinearLayoutManager.VERTICAL,false);
        recleft.setLayoutManager(layoutManager);
        beanList = new ArrayList<>();
        beanright = new ArrayList<>();
        leftAdapter = new LeftAdapter(this,beanList);
        leftAdapter.setOnItemClickListener(new LeftAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemview, int position) {
                LeftBean.DataBean dataBean = beanList.get(position);
                rightPresenter.getright("http://www.zhaoapi.cn/product/getProductCatagory?cid="+dataBean.getCid());
            }
        });
        recleft.setAdapter(leftAdapter);
        leftPresenter = new LeftPresenter();
        leftPresenter.attch(this);
        leftPresenter.getleft();
        rightPresenter = new RightPresent();
        rightPresenter.attch(this);

    }

    @Override
    public void getleft(List<LeftBean.DataBean> list) {
        if (list !=null){
            beanList.clear();
            beanList.addAll(list);
            leftAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getrights(List<RightBean.DataBean> list) {
        if (list != null){
            layoutright.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);
                rightAdapter = new RightAdapter(MainActivity.this,list.get(i).getList());
                rvNextb.setAdapter(rightAdapter);
                beanright.clear();
                beanright.addAll(list);
                leftAdapter.notifyDataSetChanged();
                layoutright.addView(textView);
                layoutright.addView(rvNextb);
            }
        }
    }

    @Override
    public void failed(Exception e) {

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (leftPresenter!=null){
            leftPresenter.detach();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        rightPresenter.getright("http://www.zhaoapi.cn/product/getProductCatagory?cid=1");
    }
}