分类实现

 compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.youth.banner:banner:+'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
    compile 'com.facebook.fresco:fresco:0.11.0'
    compile 'io.reactivex:rxjava:1.0.14'
    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'com.squareup.retrofit2:converter-gson:2.0.1'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.squareup.okhttp3:okhttp:3.1.2'
    compile 'com.squareup.retrofit2:retrofit:2.0.1'

封装类

import okhttp3.OkHttpClient;  
import retrofit2.Retrofit;  
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;  
import retrofit2.converter.gson.GsonConverterFactory;  
/**  
 * Retrofit+Rxjava+OkHttp封装,单例  
 */  
public class RetrofitUnitl {  
    private Retrofit mRetrofit;  
    private String baseUrl;  
    OkHttpClient client;  
    private static RetrofitUnitl mRetrofitManager;  
    private RetrofitUnitl(String baseUrl,OkHttpClient client){  
        this.baseUrl=baseUrl;  
        this.client=client;  
        initRetrofit();  
    }  
    public static synchronized RetrofitUnitl getInstance(String baseUrl,OkHttpClient client){  
        if (mRetrofitManager == null){  
            mRetrofitManager = new RetrofitUnitl(baseUrl,client);  
        }  
        return mRetrofitManager;  
    }  
    private void initRetrofit() {  
        mRetrofit = new Retrofit.Builder()  
                .baseUrl(baseUrl)  
                .addConverterFactory(GsonConverterFactory.create())  
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())  
                .client(client)  
                .build();  
    }  
    public  T setCreate(Class reqServer) {  
        return mRetrofit.create(reqServer);  
    }  
}  

IFenView

public interface IFenView {
    void onZuo(Zean zean);//左侧请求成功
    void onYou(Yean yean);//右侧请求成功

}

FenModel

public class FenModel {

    ScuMod scuMod;

    public void setScuMod(ScuMod scuMod) {
        this.scuMod = scuMod;
    }

    //左面请求
    public void ZuoChen(){
        //OkHttp里面可以添加拦截器
        OkHttpClient ok = new OkHttpClient.Builder()
                .build();
        //请求数据
        RetrofitUnitl.getInstance("https://www.zhaoapi.cn/",ok)
                .setCreate(Inters.class)
                .Zuo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Zean bean) {
                        scuMod.Zc(bean);
                    }
                });
    }

    //右面请求
    public void YuoChen(int cont){
        //OkHttp里面可以添加拦截器
        OkHttpClient ok = new OkHttpClient.Builder()
                .build();
        //请求数据
        RetrofitUnitl.getInstance("https://www.zhaoapi.cn/",ok)
                .setCreate(Inters.class)
                .You(cont)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Yean bean) {
                        scuMod.Yc(bean);
                    }
                });
    }

    //定义一个接口
    public interface ScuMod{
        void Zc(Zean bean);
        void Yc(Yean yean);

    }
}

FenPresenter

public class FenPresenter implements FenModel.ScuMod {

    IFenView view;
    FenModel model;

    public FenPresenter(IFenView view) {
        this.view = view;
        model = new FenModel();
        model.setScuMod(this);
    }

    @Override
    public void Zc(Zean bean) {
        view.onZuo(bean);
    }

    @Override
    public void Yc(Yean yean) {
        view.onYou(yean);
    }


    //左侧调用
    public void  ShowPer(){
        model.ZuoChen();
    }
    // 右侧调用
    public void FlShowYou(int cont){
        model.YuoChen(cont);
    }

}

Inters

public interface Inters {
    @GET("product/getCatagory")
    Observable Zuo();
    /**
     * 右面接口
     */
    @GET("product/getProductCatagory")
    Observable You(@Query("cid") int cid);




}

ErAdapter 二级列表的适配器

public class ErAdapter extends BaseExpandableListAdapter {
    private Context context;
    private Yean yean;

    public ErAdapter(Context context, Yean yean) {
        this.context = context;
        this.yean = yean;
    }

    @Override
    public int getGroupCount() {
        return yean.getData().size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }

    @Override
    public Object getGroup(int i) {
        return yean.getData().get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return yean.getData().get(i).getList().get(i);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        //父级
        View v = View.inflate(context, R.layout.you_litem, null);
        TextView expand_tv = v.findViewById(R.id.you_you);
        expand_tv.setText(yean.getData().get(i).getName());
        return v;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        //嵌套RecyclerView的布局
        View v = View.inflate(context,R.layout.youhai_litem,null);
        RecyclerView expand_recycler =  v.findViewById(R.id.rvClass);
        //RecyclerView网格格式
        GridLayoutManager gridlayout = new GridLayoutManager(context,3);
        expand_recycler.setLayoutManager(gridlayout);
        //RecyclerView的适配器
        setchild_Aapterer chapter = new setchild_Aapterer(context,yean,i,i1);
        expand_recycler.setAdapter(chapter);
        return v;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}

setchild_Aapterer 右侧RecyclerView

public class setchild_Aapterer extends RecyclerView.Adapter{
    private Context context;
    private Yean yean;
    private int i,i1;
    private OnItemClickListener mOnItemClickListener;

    public setchild_Aapterer(Context context,Yean list,int i,int i1) {
        this.context = context;
        this.yean = list;
        this.i = i;
        this.i1 = i1;
    }
    //点击事件
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }
    @Override
    public setchild_Aapterer.ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = View.inflate(context, R.layout.child_item,null);
        ViewHodler hodler = new ViewHodler(v);
        hodler.img_child = v.findViewById(R.id.img_child);
        hodler.tv_child = v.findViewById(R.id.tv_child);
        return hodler;
    }

    @Override
    public void onBindViewHolder(final setchild_Aapterer.ViewHodler holder, int position) {

        String[] str =yean.getData().get(i).getList().get(position).getIcon().split("\\|");
        String s = str[0];
        Glide.with(context).load(s).into(holder.img_child);
        holder.tv_child.setText(yean.getData().get(i).getList().get(position).getName());


        if (mOnItemClickListener != null){
            holder.img_child.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   int position =holder.getLayoutPosition();
                   mOnItemClickListener.onItemClick(holder.itemView,position); //

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return yean.getData().get(i).getList().size();
    }

    public class ViewHodler extends RecyclerView.ViewHolder {
        private ImageView img_child;
        private TextView tv_child;
        public ViewHodler(View itemView) {
            super(itemView);
        }
    }
}

ZuoAdapter 左侧RecyclerView适配器

public class ZuoAdapter  extends RecyclerView.Adapter<ZuoAdapter.MyHolder> {
    private Context context;
    private Zean zean;
    private OnItemClickListener mOnItemClickListener;

    public ZuoAdapter(Context context, Zean list) {
        this.context = context;
        this.zean = list;
    }
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHolder holder = new MyHolder(LayoutInflater.from(context).inflate(R.layout.zuo_litem, parent, false));
        return holder;
    }
    @Override
    public void onBindViewHolder(final MyHolder holder, int position) {
        holder.zuo_mz.setText(zean.getData().get(position).getName());
        if (mOnItemClickListener != null){
            holder.zuo_mz.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = holder.getLayoutPosition(); // 1

                    mOnItemClickListener.onItemClick(holder.itemView,position); // 2
                }
            });
        }
    }

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

    class MyHolder extends RecyclerView.ViewHolder {
        TextView zuo_mz;
        public MyHolder(View itemView) {
            super(itemView);
            zuo_mz = itemView.findViewById(R.id.zuo_mz);
        }
    }
}

TwoFragment

public class TwoFragment extends Fragment implements IFenView {

    private View view;
    private RecyclerView rec;
    public FenPresenter presenter;
    public RecyclerView zrv;
    public ExpandableListView yev;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fenlie, null);
        presenter = new FenPresenter(this);
        presenter.ShowPer();
        //默认展示第一条
        presenter.FlShowYou(1);
        //找控件
        zrv = view.findViewById(R.id.zrv);
        yev = view.findViewById(R.id.yev);


        return view;
    }


    @Override
    public void onZuo(final Zean zean) {
        zrv.setLayoutManager(new LinearLayoutManager(getActivity()));
        //拿到适配器
        ZuoAdapter adapter = new ZuoAdapter(getActivity(), zean);
        zrv.setAdapter(adapter);
        //调用点击事件
        adapter.setOnItemClickListener(new ZuoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                int id = zean.getData().get(position).getCid();
                presenter.FlShowYou(id);

            }
        });
    }

    @Override
    public void onYou(Yean yean) {
        ErAdapter adapter = new ErAdapter(getActivity(), yean);
        yev.setAdapter(adapter);
        //父级列表默认全部展开
        int groupCount = yev.getCount();
        for (int i = 0; i < groupCount; i++) {
            yev.expandGroup(i);


        }


    }
}

fenlei.XML

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

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

    <ExpandableListView
        android:id="@+id/yev"
        android:layout_width="match_parent"
        android:layout_height="match_parent">ExpandableListView>

LinearLayout>

child_itme.XML


<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_child"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        />
    <TextView
        android:id="@+id/tv_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="名字"
        android:layout_gravity="center"
        />


LinearLayout>

you_litem


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:id="@+id/you_you"
    />
LinearLayout>

youhai_litem


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

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

LinearLayout>

zuo_litem


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

    <TextView
        android:id="@+id/zuo_mz"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="名字"
        android:layout_gravity="center"
        />LinearLayout>

你可能感兴趣的:(分类)