欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

列表

程序员文章站 2022-07-09 15:46:28
...

oneAdapter

package com.example.yougou.adapter;

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

import com.example.yougou.R;
import com.example.yougou.bean.ClassifyOne;


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

    private Context context;
    private ClassifyOne classifyOne;

    public ClassifyOneAdapter(Context context, ClassifyOne classifyOne) {
        this.context = context;
        this.classifyOne = classifyOne;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.classifone, null, false);
        OneHolder oneHolder = new OneHolder(view);
        return oneHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        final OneHolder oneHolder = (OneHolder) viewHolder;
        oneHolder.text.setText(classifyOne.getResult().get(i).getName());
        oneHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = classifyOne.getResult().get(i).getId();
                onClassifyOneItemClick.OneItemData(id);
            }
        });
    }

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

    private class OneHolder extends RecyclerView.ViewHolder {

        private final TextView text;

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

    public interface OnClassifyOneItemClick {
        void OneItemData(String id);
    }

    public OnClassifyOneItemClick onClassifyOneItemClick;

    public void setOnClassifyOneItemClick(OnClassifyOneItemClick onClassifyOneItemClick) {
        this.onClassifyOneItemClick = onClassifyOneItemClick;
    }
}

twoAdapter

package com.example.yougou.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.yougou.R;
import com.example.yougou.bean.ClassifyThreeBean;
import com.example.yougou.bean.ClassifyTwoBean;
import com.example.yougou.utils.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


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

    private Context context;
    private List<ClassifyTwoBean.ResultBean> list;
    private String classifyThreeUrl = "http://172.17.8.100/small/commodity/v1/findCommodityByCategory?page=1&count=6&categoryId=";
    private Handler handler = new Handler();

    public ClassifyTwoAdapter(Context context, List<ClassifyTwoBean.ResultBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.classiftwo, null, false);
        TwoHolder twoHolder = new TwoHolder(view);
        return twoHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        final TwoHolder holder = (TwoHolder) viewHolder;
        holder.text.setText(list.get(i).getName());
        //设置样式
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        holder.recycler.setLayoutManager(gridLayoutManager);
        //请求数据
        OkHttpUtils.getokHttpUtils().doGet(classifyThreeUrl + list.get(i).getId(), 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();
                Log.i("three", string);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        ClassifyThreeBean classifyThreeBean = gson.fromJson(string, ClassifyThreeBean.class);
                        //设置适配器
                        ClassifyThreeAdapter classifyThreeAdapter = new ClassifyThreeAdapter(context, classifyThreeBean);
                        holder.recycler.setAdapter(classifyThreeAdapter);
                        classifyThreeAdapter.setOnClassifyThreeItemClick(new ClassifyThreeAdapter.OnClassifyThreeItemClick() {
                            @Override
                            public void ThreeItemData(String id) {
                                onClassifyTwoItemClick.TwoItemData(id);
                            }
                        });
                    }
                });
            }
        });

    }

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

    private class TwoHolder extends RecyclerView.ViewHolder {

        private final TextView text;
        private final RecyclerView recycler;

        public TwoHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.two_text);
            recycler = itemView.findViewById(R.id.two_recycler);
        }
    }

    public interface OnClassifyTwoItemClick {
        void TwoItemData(String id);
    }

    public OnClassifyTwoItemClick onClassifyTwoItemClick;

    public void setOnClassifyOneItemClick(OnClassifyTwoItemClick onClassifyTwoItemClick) {
        this.onClassifyTwoItemClick = onClassifyTwoItemClick;
    }
}

threeAdapter

package com.example.yougou.adapter;

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

import com.bumptech.glide.Glide;
import com.example.yougou.R;
import com.example.yougou.bean.ClassifyThreeBean;

/**
 * @Auther: 不懂
 * @Date: 2019/3/4 18:55:42
 * @Description:
 */
public class ClassifyThreeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ClassifyThreeBean classifyThreeBean;

    public ClassifyThreeAdapter(Context context, ClassifyThreeBean classifyThreeBean) {
        this.context = context;
        this.classifyThreeBean = classifyThreeBean;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.classifythree, null, false);
        ThreeHolder threeHolder = new ThreeHolder(view);
        return threeHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        final ThreeHolder holder = (ThreeHolder) viewHolder;
        holder.text.setText(classifyThreeBean.getResult().get(i).getCommodityName());
        Glide.with(context).load(classifyThreeBean.getResult().get(i).getMasterPic()).into(holder.image);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String commodityName = classifyThreeBean.getResult().get(i).getCommodityName();
                onClassifyThreeItemClick.ThreeItemData(commodityName);
            }
        });
    }

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

    private class ThreeHolder extends RecyclerView.ViewHolder {

        private final ImageView image;
        private final TextView text;

        public ThreeHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.three_image);
            text = itemView.findViewById(R.id.three_text);
        }
    }

    public interface OnClassifyThreeItemClick {
        void ThreeItemData(String id);
    }

    public OnClassifyThreeItemClick onClassifyThreeItemClick;

    public void setOnClassifyThreeItemClick(OnClassifyThreeItemClick onClassifyThreeItemClick) {
        this.onClassifyThreeItemClick = onClassifyThreeItemClick;
    }
}

twoitem

<?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/two_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ff00"
        android:gravity="center"
        android:textSize="20sp" />

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

    </android.support.v7.widget.RecyclerView>


</LinearLayout>