三级列表

分类布局

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

    <crq.bwei.com.app1.custom.Serch
        android:id="@+id/serch"
        android:layout_width="match_parent"
        android:layout_height="60dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"></android.support.v7.widget.RecyclerView>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView2"
            android:layout_width="0dp"
            android:layout_weight="7"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>


    </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="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="哈哈"
        android:layout_marginTop="15dp"
        />

</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/right_txt_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="XXXXXXXXXXX"
            android:textSize="25dp"
            />

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:id="@+id/right_rv"/>



</LinearLayout>

右侧recycleview的条目

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

        <ImageView
            android:id="@+id/img"
            android:layout_width="100dp"
            android:layout_height="100dp"
            />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="XXXXX"
            android:textSize="25sp" />



</LinearLayout>

分类代码

public class FenLei extends Fragment implements LeftView,RightView {

    private crq.bwei.com.app1.custom.Serch serch;
    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;
    private LeftPresenter goodsPresenter;
    private RightPresenter rightPresenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fenlei, null);
        //初始化
        initView(view);
        //自定义搜索栏
        getSerch();

        return view;
    }
    //搜索
    private void getSerch() {


        //自定义回调点击事件geta
        serch.setOnClickListener(new Serch.OnClickListener() {
            @Override
            public void onResult(String trim) {
                Toast.makeText(getActivity(), trim, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRoClick(View v) {
                Toast.makeText(getActivity(), "小机器人", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onXiaoClick(View v) {
                Toast.makeText(getActivity(), "消息", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onSaoClick(View v) {
                QRCodeManager.getInstance()
                        .with(getActivity())
                        .setReqeustType(1)//可以不设置,默认是0
                        .scanningQRCode(new OnQRCodeScanCallback() {
                            @Override
                            public void onCompleted(String result) {//扫描成功之后回调,result就是扫描的结果
                                //    Log.append("\n\n(结果)" + result);
                            }

                            @Override
                            public void onError(Throwable errorMsg) {//扫描出错的时候回调
                                //    controlLog.append("\n\n(错误)" + errorMsg.toString());
                            }

                            @Override
                            public void onCancel() {//取消扫描的时候回调
                                //   controlLog.append("\n\n(取消)扫描任务取消了");
                            }
                        });

            }
        });


    }

    private void initView(View view) {
        //控件
        serch = view.findViewById(R.id.serch);
        recyclerView1 = view.findViewById(R.id.recyclerView1);
        recyclerView2 =  view.findViewById(R.id.recyclerView2);
        //分割线
        recyclerView1.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
          //设置布局
        recyclerView1.setLayoutManager(new LinearLayoutManager(getActivity()));
       //创建p层
        goodsPresenter = new LeftPresenter(this);
        //调用关联商品的方法
        goodsPresenter.related();
       //调用内存泄漏的方法
        goodsPresenter.attchView(this);

        //分割线
        recyclerView2.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        //设置布局
        recyclerView2.setLayoutManager(new LinearLayoutManager(getActivity()));


        rightPresenter = new RightPresenter(this);


    }

    //得到左边回调数据
    @Override
    public void onGoods1Result(final List<LeftJson.ResultBean> result) {

        //设置适配器
        MyLeftAdapter goods1Adapter = new MyLeftAdapter(getActivity(), result);
        recyclerView1.setAdapter(goods1Adapter);
        //单击事件和长按事件
        goods1Adapter.setOnItemClickListener(new MyLeftAdapter.OnItemClickListener() {

            private String id;

            @Override
            public void onRecycleClick(int i) {

                id = result.get(i).getId();
                rightPresenter.related(id);
                rightPresenter.attchView(this);
            }

            @Override
            public void onRecycleLongClick(int i) {
                Toast.makeText(getActivity(), "长按"+i, Toast.LENGTH_SHORT).show();

            }


        });




    }

    //内存泄漏

    @Override
    public void onDestroy() {
        super.onDestroy();
        //调用内存泄漏解绑的方法
        goodsPresenter.dettchview();
        rightPresenter.dettchview();
        Log.d("aaa","销毁了");

    }
    //得到右边回调的标题数据
    @Override
    public void onRightGoods1Result(List<RightJson.ResultBean> result) {
        MyRightAdapter myRightAdapter = new MyRightAdapter(getActivity(),result);
        recyclerView2.setAdapter(myRightAdapter);
    }
}

MVP走二遍得到每一级的id和name

//左边的p层
public class LeftPresenter<T> {

    private final LeftModel goodsModel;
    private final LeftView goodsView;
    private Reference<T> reference;

    public LeftPresenter(LeftView view) {
        goodsModel = new LeftModel();
        goodsView = view;

    }

    public void related() {
        goodsModel.getGoods();
        goodsModel.setOnGetGoods1Listener(new LeftModel.OnGetGoods1Listener() {
            @Override
            public void onGoods1Result(List<LeftJson.ResultBean> result) {
                 goodsView.onGoods1Result(result);
            }
        });
    }

    //创建弱引用
    public void attchView(T t){
        reference = new WeakReference<>(t);
    }

    //解绑
    public void dettchview(){
        if(reference.get()!=null){
            reference.clear();
            reference=null;
        }
    }

}
//左边的m层
public class LeftModel {
    //接口回调
    public interface OnGetGoods1Listener{
        void onGoods1Result( List<LeftJson.ResultBean> result);
    }

    private OnGetGoods1Listener onGetGoods1Listener;

    public void setOnGetGoods1Listener(OnGetGoods1Listener onGetGoods1Listener) {
        this.onGetGoods1Listener = onGetGoods1Listener;
    }
    //获得一级商品信息
    public void getGoods() {
        String  url="http://172.17.8.100/small/commodity/v1/findFirstCategory";
        OkHttp.getInstence().getData(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                //存消息
                Message message=new Message();
                message.what=0;
                message.obj=string;
                handler.sendMessage(message);

            }
        });


    }

    @SuppressLint("HandlerLeak")
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String obj= (String) msg.obj;
                    Gson gson = new Gson();
                    LeftJson goods1Json = gson.fromJson(obj, LeftJson.class);
                    List<LeftJson.ResultBean> result = goods1Json.getResult();
                    if(onGetGoods1Listener!=null){
                        onGetGoods1Listener.onGoods1Result(result);
                    }

                    break;



            }
        }
    };


}

//左边的v层
public interface LeftView {
    void onGoods1Result( List<LeftJson.ResultBean> result);
}
//右边的p
public class RightPresenter<T> {

    private Reference<T> reference;
    private final RightModel rightModel;
    private final RightView rightView;

    public RightPresenter(RightView view) {
        rightModel = new RightModel();
        rightView = view;

    }

    public void related(String id) {
        rightModel.getGoods(id);
        rightModel.setOnGetGoods1Listener(new RightModel.OnGetGoods1Listener() {
            @Override
            public void onGoods1Result(List<RightJson.ResultBean> result) {
                rightView.onRightGoods1Result(result);
            }
        });
    }

    //创建弱引用
    public void attchView(T t){
        reference = new WeakReference<>(t);
    }

    //解绑
    public void dettchview(){
        if(reference.get()!=null){
            reference.clear();
            reference=null;
        }
    }

}

//右边的m
public class RightModel {
    //接口回调
    public interface OnGetGoods1Listener{
        void onGoods1Result(List<RightJson.ResultBean> result);
    }

    private OnGetGoods1Listener onGetGoods1Listener;

    public void setOnGetGoods1Listener(OnGetGoods1Listener onGetGoods1Listener) {
        this.onGetGoods1Listener = onGetGoods1Listener;
    }
    //获得一级商品信息
    public void getGoods(String id) {
        //http://172.17.8.100/small/commodity/v1/findSecondCategory?firstCategoryId=1001002
        String  url="http://172.17.8.100/small/commodity/v1/findSecondCategory?firstCategoryId="+id;
        OkHttp.getInstence().getData(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                //存消息
                Message message=new Message();
                message.what=0;
                message.obj=string;
                handler.sendMessage(message);

            }
        });


    }

    @SuppressLint("HandlerLeak")
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String obj= (String) msg.obj;
                    Gson gson = new Gson();
                    RightJson rightJson = gson.fromJson(obj, RightJson.class);
                    List<RightJson.ResultBean> result = rightJson.getResult();
                    if(onGetGoods1Listener!=null){
                        onGetGoods1Listener.onGoods1Result(result);
                    }

                    break;



            }
        }
    };


}
//右边的v
public interface RightView {
    void onRightGoods1Result(List<RightJson.ResultBean> result);
}




//网络请求工具类
public class OkHttpUtils {
    //声明
    private static OkHttpUtils okHttpUtils = null;

    public OkHttpUtils() {
    }

    //返回实例的方法
    public static OkHttpUtils getInstence() {
        if (okHttpUtils == null) {
            synchronized (OkHttpUtils.class) {
                okHttpUtils = new OkHttpUtils();
            }
        }
        return okHttpUtils;
    }

    //创建Okshi
    private static OkHttpClient okHttpClient = null;

    public static synchronized OkHttpClient getOkHttpClient() {
        //拦截器
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("aaa", message);
            }
        });
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);


        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient.Builder()
                    //添加日志拦截器
                    .addInterceptor(httpLoggingInterceptor).build();
        }
        return okHttpClient;
    }


    //get请求数据
    public static void getData(String url,Callback callback) {
        //得到ok对象
        OkHttpClient okHttpClient = getOkHttpClient();
        //得到请求
        Request request = new Request.Builder().url(url).build();
        //请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }


    //post请求数据
    public static void postData(String url, Map<String,String> params, Callback callback) {
        //1得到ok对象
        OkHttpClient okHttpClient = getOkHttpClient();
        //2得到求体
        FormBody.Builder builder=new FormBody.Builder();
        //遍历集合,封装参数
        for (String key:params.keySet()){
            builder.add(key,params.get(key));
            Log.d("aaaaa",key+"**"+params.get(key));
        }

        FormBody formBody= builder.build();
       //3得到请求
        Request request = new Request.Builder().post(formBody).url(url).build();
        //请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }


}


适配器

//左边适配器
public class MyLeftAdapter extends RecyclerView.Adapter<MyLeftAdapter.MyViewHodelr> {

    private Context context;
    private List<LeftJson.ResultBean> result;

    public MyLeftAdapter(Context context, List<LeftJson.ResultBean> result) {
        this.context = context;
        this.result = result;
    }

    @NonNull
    @Override
    public MyViewHodelr onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.left_goods_item, null);
        final MyViewHodelr myViewHodelr = new MyViewHodelr(view);
        //单击 长按
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获得下标
                int layoutPosition = myViewHodelr.getLayoutPosition();
                onItemClickListener.onRecycleClick(layoutPosition);
                onItemClickListener.onRecycleLongClick(layoutPosition);
            }
        });
        return myViewHodelr;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHodelr myViewHodelr, int i) {
        //数据
        myViewHodelr.text.setText(result.get(i).getName());
    }

    @Override
    public int getItemCount() {
        return result.size();
    }
    //自定义存储控件
    class MyViewHodelr extends RecyclerView.ViewHolder{

        private final TextView text;

        public MyViewHodelr(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
        }

    }

    //接口回调
    public interface OnItemClickListener{
         void  onRecycleClick(int i);
         void  onRecycleLongClick(int i);
    }

    private OnItemClickListener onItemClickListener;

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

//右边的适配器
public class MyRightAdapter extends RecyclerView.Adapter<MyRightAdapter.MyViewHodelr> {

    private Context context;
    private List<RightJson.ResultBean> result;
    private MyGoodsAdapter myGoodsAdapter;
    private android.os.Handler handler=new android.os.Handler();

    public MyRightAdapter(Context context, List<RightJson.ResultBean> result) {
        this.context = context;
        this.result = result;
    }

    @NonNull
    @Override
    public MyViewHodelr onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.right_layout_view, null);
        final MyViewHodelr myViewHodelr = new MyViewHodelr(view);

        return myViewHodelr;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHodelr myViewHodelr, int i) {


        //数据
        myViewHodelr.text.setText(result.get(i).getName());
        //获得第三个接口的id
        String id = result.get(i).getId();

        //请求数据给第三个适配器
        OkHttp.getInstence().getData("http://172.17.8.100/small/commodity/v1/findCommodityByCategory?categoryId=" + id + "&page=1&count=5", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        Gson gson = new Gson();
                        GoodsBean goodsBean = gson.fromJson(string, GoodsBean.class);
                        List<GoodsBean.ResultBean> result = goodsBean.getResult();
                        //得到子布局的recycleview
                        myViewHodelr.right_rv.setLayoutManager(new GridLayoutManager(context,3));

                        //添加子布局的适配器
                        myGoodsAdapter=new MyGoodsAdapter(context,result);

                        myViewHodelr.right_rv.setAdapter(myGoodsAdapter);
                    }
                });

            //    handler.sendMessage(message);
            }
        });


    }

    @Override
    public int getItemCount() {
        return result.size();
    }
    //自定义存储控件
    class MyViewHodelr extends RecyclerView.ViewHolder{

        private final TextView text;
        private final RecyclerView right_rv;

        public MyViewHodelr(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.right_txt_title);
            right_rv = itemView.findViewById(R.id.right_rv);
        }

    }
  
}

//右边适配器通过工具类得到数据调用右边的recycleview的适配器
public class MyGoodsAdapter extends RecyclerView.Adapter<MyGoodsAdapter.MyViewHolder> {

    private Context context;
    private List<GoodsBean.ResultBean> result;

    public MyGoodsAdapter(Context context, List<GoodsBean.ResultBean> result) {
        this.context = context;
        this.result = result;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.right_goods_view, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        Glide.with(context).load(result.get(i).getMasterPic()).into(myViewHolder.img);
        myViewHolder.text.setText(result.get(i).getCommodityName());
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{

        private final ImageView img;
        private final TextView text;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            text = itemView.findViewById(R.id.text);
        }


    }

}


©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值