MVP 模式 淘宝分类

//bean名称


子类GoodsList

左侧SortBean

右侧SortRithtChildBean

//M层、


public interface IModel {


    void onLoad(String url, int i);


}

//Model实现IModel

public class Model implements IModel {


    private onFinishListenner onFinishListenner;


    public interface onFinishListenner {
        void onFinsh(SortBean sortBean, int i);


        void onGoodsListFinsh(GoodsList goodsList, int i);


        void onChildFinsh(SortRithtChildBean sortRithtChildBean, int i);
    }


    public void setOnFinishListenner(onFinishListenner onFinishListenner) {
        this.onFinishListenner = onFinishListenner;


    }


    ;


    //获取数据
    @Override
    public void onLoad(String url, int i) {
        if (i == 0) {
            OkHttp3Utils.doGet(url, new GsonObjectCallback<SortBean>() {
                @Override
                public void onUi(SortBean sortBean) {
                    if (onFinishListenner != null) {
                        onFinishListenner.onFinsh(sortBean, 0);
                    }
                }


                @Override
                public void onFailed(Call call, IOException e) {


                }
            });
        } else if (i == 1) {
            Log.e("Iv", url + "");
            OkHttp3Utils.doGet(url, new GsonObjectCallback<SortRithtChildBean>() {
                @Override
                public void onUi(SortRithtChildBean sortRithtChildBean) {
                    if (onFinishListenner != null) {
                        onFinishListenner.onChildFinsh(sortRithtChildBean, 1);
                    }


                }


                @Override
                public void onFailed(Call call, IOException e) {


                }
            });


        } else if (i == 2) {
            OkHttp3Utils.doGet(url, new GsonObjectCallback<GoodsList>() {
                @Override
                public void onUi(GoodsList goodsList) {


                    if (onFinishListenner != null) {


                        onFinishListenner.onGoodsListFinsh(goodsList, 2);
                    }
                }


                @Override
                public void onFailed(Call call, IOException e) {


                }
            });


        }


    }
}



//V层

public interface IView {


    void getSortBean(SortBean sortBean);


    void getSortChildBean(SortRithtChildBean sortRithtChildBean);


}

//实现右侧商品点击事件

public interface GoodsListIView {


    void getGoodsList(GoodsList goodsList);


}


//P层实现M层回调方式

public class Passer implements Model.onFinishListenner {


    private IView iView;
    private Model model;
    private GoodsListIView goodsListIView;


    public Passer(IView iView) {
        this.iView = iView;
        model = new Model();
    }


    public Passer() {
        model = new Model();
    }


    public void setGoodsListIView(GoodsListIView goodsListIView) {
        this.goodsListIView = goodsListIView;
    }


    public void onLoad(String url, int i) {
        model.setOnFinishListenner(this);
        model.onLoad(url, i);
    }




    //结果回传
    @Override
    public void onFinsh(SortBean sortBean, int i) {


        if (iView != null) {
            if (sortBean != null) {
                iView.getSortBean(sortBean);
            }
        }


    }


    //子类结果回传
    @Override
    public void onChildFinsh(SortRithtChildBean sortRithtChildBean, int i) {


        if (iView != null) {
            if (sortRithtChildBean != null) {
                iView.getSortChildBean(sortRithtChildBean);
            }
        }


    }


    //获取商品列表数据
    @Override
    public void onGoodsListFinsh(GoodsList goodsList, int i) {
        if (goodsListIView != null) {
            if (goodsList != null) {
                goodsListIView.getGoodsList(goodsList);
            }
        }
    }
}

//接口

public class Url {


    public static final String SORTGOODSURL = "http://120.27.23.105/product/getCatagory";
    public static final String SORTGOODSCHILDURL = "http://120.27.23.105/product/getProductCatagory";
    public static final String GOODSDDETAILS = "http://120.27.23.105/product/getProducts";
}


//

//左侧适配器

public class SortLeftAdapter extends RecyclerView.Adapter<SortLeftAdapter.MVH> {


    private List<SortBean.DataBean> list;
    private Context context;
    private setonclick setonclick;


    public interface setonclick {
        void onclick(View view, int positon, String str);
    }


    public SortLeftAdapter(List<SortBean.DataBean> list, Context context, setonclick setonclick) {
        this.list = list;
        this.context = context;
        this.setonclick = setonclick;
    }


    @Override
    public MVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View sortleft = View.inflate(context, R.layout.sortleft, null);
        MVH mvh = new MVH(sortleft);
        return mvh;
    }


    @Override
    public void onBindViewHolder(final MVH holder, final int position) {
        holder.sortname.setText(list.get(position).getName());
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (setonclick != null) {
                    setonclick.onclick(holder.linearLayout, position, list.get(position).getCid() + "");
                }
            }
        });
    }


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


    class MVH extends RecyclerView.ViewHolder {
        private TextView sortname;
        private LinearLayout linearLayout;


        public MVH(View itemView) {
            super(itemView);


            sortname = (TextView) itemView.findViewById(R.id.sortname);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.sortleft);
        }
    }


}


//右侧适配器

public class SortRightAdapter extends RecyclerView.Adapter<SortRightAdapter.MVH> {


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


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


    @Override
    public MVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.sortright, null);
        MVH mvh = new MVH(view);
        return mvh;
    }


    @Override
    public void onBindViewHolder(MVH holder, int position) {
        holder.textView.setText(list.get(position).getName());
        final List<SortRithtChildBean.DataBean.ListBean> listBeanList = this.list.get(position).getList();
        ChildssAdapter childssAdapter = new ChildssAdapter(context, listBeanList);
        holder.gridview.setAdapter(childssAdapter);
        holder.gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SortRithtChildBean.DataBean.ListBean listBean = listBeanList.get(i);
                Intent intent = new Intent(context, GoodsDetailsActivity.class);
                intent.putExtra("listBean", listBean);
                context.startActivity(intent);
            }
        });


    }


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




    class MVH extends RecyclerView.ViewHolder {
        TextView textView;
        GridView gridview;


        public MVH(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.titless);
            gridview = (GridView) itemView.findViewById(R.id.childssgridview);
        }
    }


}

//商品子类适配器

public class ChildssAdapter extends BaseAdapter {


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


    public ChildssAdapter(Context context, List<SortRithtChildBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }


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


    @Override
    public Object getItem(int i) {
        return list.get(i);
    }


    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View view1;
        MyViewHolder holder;
        if (view == null) {
            holder = new MyViewHolder();
            view1 = View.inflate(context, R.layout.childssitem, null);
            holder.goodsname = (TextView) view1.findViewById(R.id.goodsname);
            view1.setTag(holder);
        } else {
            view1 = view;
            holder = (MyViewHolder) view1.getTag();
        }
        holder.goodsname.setText(list.get(i).getName());


        return view1;
    }




    class MyViewHolder {
        TextView goodsname;
    }


}


//点击右侧商品适配器

public class GoodsListAdapter extends XRecyclerView.Adapter<GoodsListAdapter.MVH> {


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




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


    @Override
    public MVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.goodslistitem, null);
        MVH mvh = new MVH(view);
        return mvh;
    }


    @Override
    public void onBindViewHolder(MVH holder, int position) {
        ImageLoader instance = ImageLoader.getInstance();
        instance.displayImage(list.get(position).getImages().split("//|")[0], holder.goodstupian);
        holder.titles.setText(list.get(position).getTitle());
        holder.subhead.setText(list.get(position).getSubhead());
        holder.newprice.setText(list.get(position).getBargainPrice() + "");
        holder.oldprice.setText(list.get(position).getPrice() + "");


    }


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


    class MVH extends XRecyclerView.ViewHolder {


        private ImageView goodstupian;
        private TextView titles;
        private TextView subhead;
        private TextView newprice;
        private TextView oldprice;


        public MVH(View itemView) {
            super(itemView);
            goodstupian = (ImageView) itemView.findViewById(R.id.goodstupian);
            titles = (TextView) itemView.findViewById(R.id.settitles);
            subhead = (TextView) itemView.findViewById(R.id.subhead);
            newprice = (TextView) itemView.findViewById(R.id.newprice);
            oldprice = (TextView) itemView.findViewById(R.id.oldprice);
        }
    }
}

//右侧点击商品跳转

public class GoodsDetailsActivity extends AppCompatActivity implements GoodsListIView {


    private SortRithtChildBean.DataBean.ListBean listbean;
    private TextView listtitles;
    private XRecyclerView goodslist;
    private Passer passer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_details);
        Intent intent = getIntent();
        listbean = (SortRithtChildBean.DataBean.ListBean) intent.getSerializableExtra("listBean");


        passer = new Passer();
        passer.setGoodsListIView(this);
        passer.onLoad(Url.GOODSDDETAILS + "?pscid=" + listbean.getPscid(), 2);


        //初始化控件
        initView();
    }


    //初始化控件
    private void initView() {
        listtitles = (TextView) this.findViewById(R.id.listtitles);
        listtitles.setText(listbean.getName() + "列表");
        goodslist = (XRecyclerView) this.findViewById(R.id.goodslist);
        goodslist.setLayoutManager(new LinearLayoutManager(this));
    }


    @Override
    public void getGoodsList(GoodsList goodsList) {


    }


    //获取商品列表数据


    /*public void getGoodsList(GoodsList goodsList) {
        if ()
            Toast.makeText(this, goodsList.getData().toString() + "", Toast.LENGTH_SHORT).show();


    }*/
}

//主类

public class SortFragment extends Fragment implements IView, SortLeftAdapter.setonclick {




    private View view;
    private RecyclerView sortleft;
    private RecyclerView sortright;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        view = inflater.inflate(R.layout.fragment_sort, null);
        initView();
        return view;
    }


    //初始化控件
    private void initView() {
        sortright = (RecyclerView) view.findViewById(R.id.sortright);
        sortright.setLayoutManager(new LinearLayoutManager(getActivity()));


        sortleft = (RecyclerView) view.findViewById(R.id.sortleft);
        sortleft.setLayoutManager(new LinearLayoutManager(getActivity()));
        new Passer(this).onLoad(Url.SORTGOODSURL, 0);
    }




    @Override
    public void onResume() {
        super.onResume();




    }


    //获取分类数据
    @Override
    public void getSortBean(SortBean sortBean) {
        List<SortBean.DataBean> data = sortBean.getData();
        SortLeftAdapter adapter = new SortLeftAdapter(data, getActivity(), this);
        sortleft.setAdapter(adapter);
    }


    //点击事件
    @Override
    public void onclick(View view, int positon, String str) {
        makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
        new Passer(this).onLoad(Url.SORTGOODSCHILDURL + "?cid=" + str, 1);
    }


    //获取子分类数据
    @Override
    public void getSortChildBean(SortRithtChildBean sortRithtChildBean) {
        if (sortRithtChildBean.getData().size() > 0) {
            SortRightAdapter sortRightAdapter = new SortRightAdapter(sortRithtChildBean.getData(), getActivity());
            sortright.setAdapter(sortRightAdapter);
            //刷新Right适配器
            sortRightAdapter.notifyDataSetChanged();


        }
    }
}

//主布局

<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="com.example.lenovo.taobaodemo.fragment.PageFragment">


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


    <android.support.v7.widget.RecyclerView
        android:id="@+id/sortright"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:background="#BABABA"></android.support.v7.widget.RecyclerView>

</LinearLayout>


//左侧布局

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


    <TextView
        android:id="@+id/sortname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="5dp"
        android:text="京东超市"
        android:textSize="30dp" />


</LinearLayout>

//右侧布局

<?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="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/titless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="20dp"
        android:textSize="25dp"
        android:textColor="#000000"
        />




    <GridView
        android:id="@+id/childssgridview"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="10dp"
        android:numColumns="3"></GridView>
</LinearLayout>



//商品子类

<?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="match_parent"
    android:orientation="horizontal">




    <ImageView
        android:id="@+id/goodstupian"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="5dp"
        android:src="@mipmap/logo" />


    <LinearLayout
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView
            android:id="@+id/settitles"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textSize="18dp" />


        <TextView
            android:id="@+id/subhead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
        />




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


            <TextView
                android:id="@+id/newprice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FBBE00"
                android:text="111111" />


            <TextView
                android:id="@+id/oldprice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="111111" />




        </LinearLayout>




    </LinearLayout>




</LinearLayout>



//标题

<?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="wrap_content"
    android:orientation="vertical">


    <TextView
        android:id="@+id/goodsname"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:gravity="center"
        android:textSize="20dp" />


</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值