京东分类

一:生成bean类

1.LeftBean

接口:http://www.zhaoapi.cn/product/getCatagory

2.RightBean

接口:http://120.27.23.105/product/getProductCatagory  或http://120.27.23.105/product/getProductCatagory?cid=1一样

二:Api

public interface Api {

    //左边展示
    @GET("product/getCatagory")
    Observable<LeftBean> getleft();

    //右边展示
    @GET("product/getProductCatagory")
    Observable<RightBean> getright(@Query("cid") int cid);

}

三:网络请求Httputis

public class HttpUtils {
    private static final String BASE_URL = "http://www.zhaoapi.cn/";
    private Retrofit retrofit;

    private static final class SINGLE_INSTANCE{
        private static final HttpUtils _INSTANCE = new HttpUtils();
    }

    public static HttpUtils getInstance(){
        return SINGLE_INSTANCE._INSTANCE;
    }

    private HttpUtils(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(buildOkHttpClient())
                .build();
    }

    private OkHttpClient buildOkHttpClient() {
        return new OkHttpClient.Builder()
                .readTimeout(5000,TimeUnit.MILLISECONDS)
                .writeTimeout(5000,TimeUnit.MILLISECONDS)
                .build();
    }

    public <T> T create(Class<T> clazz){
        return retrofit.create(clazz);
    }
}

四:view层

1.左边view

public interface LeftView {
    void onSuccess(LeftBean leftBean);

    void onFailed(Throwable t);
}

2.右边view

public interface RightView {
    void onRight(RightBean rightBean);

    void onFailed(Throwable t);
}

五:model层

1.左边model

public class LeftModel {
    public Observable<LeftBean> getleft(){
        Api api = HttpUtils.getInstance().create(Api.class);
        Observable<LeftBean> getleft = api.getleft();
        return getleft;
    }
}

2.右边model

public class RightModel {
    public Observable<RightBean> getright(int cid){
        Api api = HttpUtils.getInstance().create(Api.class);
        Observable<RightBean> getright = api.getright(cid);
        return getright;
    }
}

六:presenter层

1.左边p

public class LeftPresenter {
    LeftView leftView;
    private final LeftModel leftModel;

    public LeftPresenter(LeftView leftView) {
        this.leftView = leftView;
        leftModel = new LeftModel();
    }

    public void getLeftData() {
        leftModel.getleft()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LeftBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LeftBean leftBean) {
                        leftView.onSuccess(leftBean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}

2.右边p

public class RighttPresenter {
    RightView rightView;
    private final RightModel rightModel;

    public RighttPresenter(RightView rightView) {
        this.rightView = rightView;
        rightModel = new RightModel();
    }

    public void getRightData(int cid) {
        rightModel.getright(cid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RightBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RightBean rightBean) {
                        rightView.onSuccess(rightBean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}

七:adapter

1.左边adapter

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

    private Context context;
    private List<ShouBean.DataBean.FenleiBean> list;

    public LeftAdapter(Context context, List<ShouBean.DataBean.FenleiBean> list) {
        this.context = context;
        this.list = list;
    }

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

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

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.left_layout, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.text_left.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 final TextView text_left;

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

            text_left = itemView.findViewById(R.id.text_left);
        }
    }
}

1.2.左边adapter布局 left_layout

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_left"
        android:text="呵呵"
        android:padding="5dp"
        android:textSize="15sp"/>

</LinearLayout>

 

2.右边adapter

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

    private Context context;
    private List<RightBean.DataBean> listBeans;

    public RightAdapter(Context context, List<RightBean.DataBean> listBeans) {
        this.context = context;
        this.listBeans = listBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.right_layout, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.title.setText(listBeans.get(position).getName());

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false);
        holder.rightrev.setLayoutManager(gridLayoutManager);

        ImageAdapter imageAdapter = new ImageAdapter(context, listBeans.get(position).getList());
        holder.rightrev.setAdapter(imageAdapter);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView title;
        private final RecyclerView rightrev;

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

            title = itemView.findViewById(R.id.right_title);
            rightrev = itemView.findViewById(R.id.right_rev);
        }
    }
}

2.2.右边adapter 布局 right_layout

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

    <TextView
        android:layout_marginTop="10dp"
        android:id="@+id/right_title"
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

3.右边adapter嵌套的imageadapter 

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

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

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

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.image_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.text_view.setText(list.get(position).getName());
        holder.simple_view.setImageURI(Uri.parse(list.get(position).getIcon().split("\\|")[0]));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = list.get(position).getName();
                //点击子条目跳转到列表页面
                Intent intent = new Intent(context, LieActivity.class);
                intent.putExtra("name",name);
                context.startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final SimpleDraweeView simple_view;
        private final TextView text_view;

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

            simple_view = itemView.findViewById(R.id.simple_view);
            text_view = itemView.findViewById(R.id.text_view);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            int layoutPosition = getLayoutPosition();
            onItemclickLinsten.onclick(layoutPosition);
        }
    }
    onItemclickLinsten onItemclickLinsten;

    public interface onItemclickLinsten{
        void onclick(int layoutPosition);
    }
    public void setonItemclickLinsten(onItemclickLinsten onItemclickLinsten){
        this.onItemclickLinsten= onItemclickLinsten;
    }

}

3.2.嵌套imageadapter的布局 image_item

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

    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="60dp"
        android:id="@+id/simple_view"
        android:layout_height="60dp" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="100dp"
        android:layout_height="20dp" />

</LinearLayout>

八:分类fragment的主代码

public class FenleiFragment extends Fragment implements LeftView, RightView{

    @BindView(R.id.rec_left)
    RecyclerView recLeft;
    @BindView(R.id.rec_right)
    RecyclerView recRight;
    Unbinder unbinder;
//    @BindView(R.id.simple_title)
    SimpleDraweeView simpleTitle;
    private LeftPresenter leftPresenter;
    private RighttPresenter righttPresenter;
    private int cid;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fenlei, container, false);

        unbinder = ButterKnife.bind(this, view);

        leftPresenter = new LeftPresenter(this);
        leftPresenter.getLeftData();

        righttPresenter = new RighttPresenter(this);
        righttPresenter.getRightData(1);

        initView();
        initData();

        return view;
    }

    private void initData() {

    }

    private void initView() {
        recLeft.setLayoutManager(new LinearLayoutManager(getActivity()));
        recRight.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void onSuccess(final LeftBean leftBean) {
        List<LeftBean.DataBean> left = leftBean.getData();
        LeftAdapter leftAdapter = new LeftAdapter(getActivity(), left);
        recleft.setAdapter(leftAdapter);
        leftAdapter.setOnItemClickListener(new LeftAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int posistion) {
                cid = leftBean.getData().get(posistion).getCid();
                righttPresenter.getRightData(cid);

                //simpleTitle.setImageURI(Uri.parse(leftBean.getData().get(posistion).getIcon().split("\\|")[0]));
            }
        });
    }

    @Override
    public void onSuccess(RightBean rightBean) {
        List<RightBean.DataBean> data = rightBean.getData();
        RightAdapter rightAdapter = new RightAdapter(getActivity(), data);
        recRight.setAdapter(rightAdapter);

    }

    @Override
    public void onFailed(String msg) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值