Sort

1.SortFragment

 

package com.example.lenovo.jdapp.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.Toast;

import com.example.lenovo.jdapp.R;

import com.example.lenovo.jdapp.sort.adapter.SortLeftAdapter;

import com.example.lenovo.jdapp.sort.adapter.SortRightAdapter;
import com.example.lenovo.jdapp.sort.bean.ChildBean;
import com.example.lenovo.jdapp.sort.bean.ShopBean;
import com.example.lenovo.jdapp.sort.presenter.SortPresenter;
import com.example.lenovo.jdapp.sort.view.IView;

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

/**
 *
 */

public class SortFragment extends Fragment implements IView {

    private RecyclerView left;
    private RecyclerView right;
    private SortPresenter presenter;
    private List<ShopBean.DataBean> list;
    private List<ChildBean.DataBean> list1;
    private String urll = "https://www.zhaoapi.cn/product/getProductCatagory";
    private SortRightAdapter rightAdapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_sort,container,false);
        initView(v);
        return v;
    }

    private void initView(View v) {
        left = v.findViewById(R.id.recyclerleft);
        right = v.findViewById(R.id.recyclerright);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();

    }

    private void initData() {
        presenter = new SortPresenter();
        presenter.attach(this);
        presenter.getleftshop();
        presenter.setView(1,"https://www.zhaoapi.cn/product/getProductCatagory");

        list = new ArrayList<>();
        //添加适配器
        SortLeftAdapter adapter = new SortLeftAdapter(getActivity(),list);
           //线性布局管理器
           RecyclerView.LayoutManager layoutManager =  new LinearLayoutManager(getActivity());
          //添加分割线
        left.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));

        left.setLayoutManager(layoutManager);

        left.setAdapter(adapter);
        adapter.setOnItemClickListener(new SortLeftAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View itemView, int position) {
                   int cid = list.get(position).getCid();
                   presenter.setView(cid,urll);
            }
        });


//线性布局管理器
        RecyclerView.LayoutManager layoutManager1 =  new LinearLayoutManager(getActivity());
        list1= new ArrayList<>();
        right.setLayoutManager(layoutManager1);
        rightAdapter = new SortRightAdapter(list1,getActivity());
        right.setAdapter(rightAdapter);

 


    }

    @Override
    public void success(List<ShopBean.DataBean> data) {

        for (int i=0; i <data.size();i++){
            list.add(data.get(i));
        }

    }

    @Override
    public void failed(Exception e) {
        Toast.makeText(getActivity(), "网络异常" + e.getMessage(), Toast.LENGTH_SHORT).show();
    }

 

 

    @Override
    public void success1(List<ChildBean.DataBean> childBean) {

        if (childBean!=null){
            list1.clear();
            list1.addAll(childBean);
            Toast.makeText(getActivity(), "shuju"+list1.size(), Toast.LENGTH_SHORT).show();
            rightAdapter.notifyDataSetChanged();
        }
    }


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

 

2. CallBack

public interface ICallBack {

    void success(Object obj);

    void failed(Exception e);

}

 

3.HttpUtils

package com.example.lenovo.jdapp.utils;

import android.os.Handler;

import com.example.lenovo.jdapp.inter.ICallBack;
import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 *
 */

public class HttpUtils {

    private static volatile HttpUtils instance;

    private OkHttpClient client;

    private Handler handler = new Handler();

    private  HttpUtils(){
          client = new OkHttpClient();
    }

    public static HttpUtils getInstance(){

        if (instance == null){
            synchronized (HttpUtils.class){
                if (null == instance){
                    instance= new HttpUtils();
                }
            }
        }

        return instance;

    }


    public void get(String url, final ICallBack callBack, final Type type){
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.failed(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String result = response.body().string();
                Gson gson = new Gson();
                final Object o = gson.fromJson(result, type);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.success(o);
                    }
                });

            }
        });

    }

}

 

 

4.  MVP:   V

public interface IView {

     void success(List<ShopBean.DataBean> data);

     void failed(Exception e);

    void success1(List<ChildBean.DataBean> childBean);

}

 

5.    M

public class SortModel {

    public void getData(String url, ICallBack callBack, Type type){

        HttpUtils.getInstance().get(url,callBack,type);
    }

}


6.  P

package com.example.lenovo.jdapp.sort.presenter;

import com.example.lenovo.jdapp.fragment.SortFragment;
import com.example.lenovo.jdapp.inter.ICallBack;
import com.example.lenovo.jdapp.sort.bean.ChildBean;
import com.example.lenovo.jdapp.sort.view.IView;
import com.example.lenovo.jdapp.sort.bean.ShopBean;
import com.example.lenovo.jdapp.sort.model.SortModel;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
 *
 */

public class SortPresenter {

    private IView iv;
    private SortModel model;


    public void attach(IView iv){
        this.iv =  iv;
        model = new SortModel();
    }


    public void getleftshop(){
        Type type = new TypeToken<ShopBean>(){}.getType();

        model.getData("https://www.zhaoapi.cn/product/getCatagory", new ICallBack() {
            @Override
            public void success(Object obj) {
               ShopBean shopBean = (ShopBean) obj;
               if (shopBean!=null){
                   List<ShopBean.DataBean> data = shopBean.getData();
                   iv.success(data);
               }

            }

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

    }


    public void setView(int cid,String url){
        String url2 = url+"?cid="+cid;

        Type type = new TypeToken<ChildBean>(){}.getType();

        model.getData(url2, new ICallBack() {
            @Override
            public void success(Object obj) {

                ChildBean childBean = (ChildBean) obj;

                    List<ChildBean.DataBean> data = childBean.getData();
                    iv.success1(data);

                
            }

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

    }


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

}

 

7 Adapter  l e f t a d a p t e r

package com.example.lenovo.jdapp.sort.adapter;

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

import com.example.lenovo.jdapp.R;
import com.example.lenovo.jdapp.sort.bean.ShopBean;

import java.util.List;

/**
 *
 */

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

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

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

    //接口回调
    public interface OnItemClickListener{
        void OnItemClick(View itemView,int position);
    }

    private OnItemClickListener itemClickListener;

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

 

    @NonNull
    @Override
    public SortLeftAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = View.inflate(context,R.layout.item_sort_left,null);

        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull SortLeftAdapter.Holder holder, final int position) {

        holder.txtLeft.setText(list.get(position).getName());


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.OnItemClick(view,position);
            }
        });

    }

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

    class Holder extends RecyclerView.ViewHolder{

        private final TextView txtLeft;

        public Holder(View itemView) {
            super(itemView);
            txtLeft = itemView.findViewById(R.id.txt_left_title);
        }
    }

}

 

 

8  RightAdapter

package com.example.lenovo.jdapp.sort.adapter;


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

import com.example.lenovo.jdapp.R;
import com.example.lenovo.jdapp.sort.bean.ChildBean;

import java.util.List;

/**
 *
 */

public class SortRightAdapter extends RecyclerView.Adapter<SortRightAdapter.ViewHolder> {
    private List<ChildBean.DataBean> list;
    private Context context;

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

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = View.inflate(context, R.layout.item_sort_right, null);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.sort_rightTitle.setText(list.get(position).getName());
        List<ChildBean.DataBean.ListBean> lists = list.get(position).getList();
        MyAdapter myAdapter = new MyAdapter(context,lists);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,5);
        holder.sort_rightRecyc.setLayoutManager(gridLayoutManager);
        holder.sort_rightRecyc.setAdapter(myAdapter);

    }

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

    class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView sort_rightTitle;
        private final RecyclerView sort_rightRecyc;

        public ViewHolder(View itemView) {
            super(itemView);
            sort_rightTitle = itemView.findViewById(R.id.sort_right_title);
            sort_rightRecyc = itemView.findViewById(R.id.sort_right_recyclerview);
        }
    }

}

 

9. myadapter

package com.example.lenovo.jdapp.sort.adapter;

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

import com.bumptech.glide.Glide;
import com.example.lenovo.jdapp.R;
import com.example.lenovo.jdapp.sort.bean.ChildBean;

import java.util.List;

/**
 *
 */

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

    private Context mContext;
    private List<ChildBean.DataBean.ListBean> lists;

    public MyAdapter(Context mContext, List<ChildBean.DataBean.ListBean> lists) {
        this.mContext = mContext;
        this.lists = lists;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = View.inflate(mContext, R.layout.sort_right_child,null);
        ViewHolder holder = new ViewHolder(v);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
          holder.content.setText(lists.get(position).getName());
Glide.with(mContext).load(lists.get(position).getIcon()).into(holder.img1);
    }

 

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


    class ViewHolder extends RecyclerView.ViewHolder{

        private final ImageView img1;
        private final TextView content;

        public ViewHolder(View itemView) {
            super(itemView);
            img1 = itemView.findViewById(R.id.img1);
            content = itemView.findViewById(R.id.content);
        }
    }

}

 

 

/ / Layout

sort

<?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">


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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/ic_launcher" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入要搜索的内容" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

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

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

    </LinearLayout>
</LinearLayout>

 

right

<?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/sort_right_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/sort_right_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"></android.support.v7.widget.RecyclerView>


</LinearLayout>

 

 

rightChild

<?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:layout_marginTop="20dp"
    android:orientation="vertical">

 

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="50dp" />

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

</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值