仿京东分类页面

activity_main.xml:




    
    

    

        

        
    



左侧竖向的RecyclerView条目——left_rv_item.xml:




    

 

ExpandableListView的父布局——elv_parent_item.xml: 




    

 

ExpandableListView的子布局(其实就是一个RecyclerView)——elv_child_item.xml: 




    

 

ExpandableListView的子布局的条目 —— elv_child_rv_item.xml:




    

    

 

首先是model层:

package com.example.dell.dianshang7.mvp.model;

import com.example.dell.dianshang7.OkhttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by DELL on 2018/6/28.
 */

public class Model {

    private OkhttpUtils instance;

    public void startListView(int cid, final IModelInterface iModelInterface){

        instance = OkhttpUtils.getInstance();

        instance.doGet("https://www.zhaoapi.cn/product/getCatagory", new OkhttpUtils.OkCallback() {
            @Override
            public void onResponse(String json) {

                if(iModelInterface != null){

                    iModelInterface.onLeftResponse(json);

                }

            }

            @Override
            public void onFailure(Exception e) {

                if(iModelInterface != null){

                    iModelInterface.onLeftFailure(e);

                }

            }
        });

        Map map = new HashMap<>();
        map.put("cid",cid+"");
        instance.doPost("https://www.zhaoapi.cn/product/getProductCatagory", map, new OkhttpUtils.OkCallback() {
            @Override
            public void onResponse(String json) {
                if(iModelInterface != null){

                    iModelInterface.onRightResponse(json);

                }
            }

            @Override
            public void onFailure(Exception e) {

                if(iModelInterface != null){

                    iModelInterface.onRightFailure(e);

                }

            }
        });
    }

    public interface IModelInterface{

        void onLeftResponse(String json);
        void onLeftFailure(Exception e);

        void onRightResponse(String json);
        void onRightFailure(Exception e);

    }

}

presenter层:

package com.example.dell.dianshang7.mvp.presenter;

import com.example.dell.dianshang7.mvp.model.Model;
import com.example.dell.dianshang7.mvp.view.iview.IViewInterface;

/**
 * Created by DELL on 2018/6/28.
 */

public class Presenter {

    private Model model;
    IViewInterface mIViewInterface;

    public Presenter(IViewInterface iViewInterface){

        mIViewInterface = iViewInterface;
        model = new Model();

    }

    public void startListView(int cid){

        model.startListView(cid, new Model.IModelInterface() {
            @Override
            public void onLeftResponse(String json) {
                if(mIViewInterface != null){

                    mIViewInterface.onLeftResponse(json);

                }
            }

            @Override
            public void onLeftFailure(Exception e) {

                if(mIViewInterface != null){

                    mIViewInterface.onLeftFailure(e);

                }

            }

            @Override
            public void onRightResponse(String json) {

                if(mIViewInterface != null){

                    mIViewInterface.onRightResponse(json);

                }

            }

            @Override
            public void onRightFailure(Exception e) {

                if(mIViewInterface != null){

                    mIViewInterface.onRightFailure(e);

                }

            }
        });

    }

}

view层:

package com.example.dell.dianshang7.mvp.view.iview;

/**
 * Created by DELL on 2018/6/28.
 */

public interface IViewInterface {

    void onLeftResponse(String json);
    void onLeftFailure(Exception e);

    void onRightResponse(String json);
    void onRightFailure(Exception e);

}

适配器adapter:

最左边RecyclerView的适配器 —— LeftAdapter

package com.example.dell.dianshang7.adapter;

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

import com.example.dell.dianshang7.Catagory;
import com.example.dell.dianshang7.R;

import java.util.List;

/**
 * Created by DELL on 2018/6/28.
 */

public class LeftAdapter extends RecyclerView.Adapter{

    private Context context;
    private List data;

    public LeftAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
    }

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

        View view = View.inflate(context, R.layout.left_rv_item, null);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

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

        holder.left_rv_tv.setText(data.get(position).getName());

        if(onitemClickListener != null){

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int cid = data.get(position).getCid();
                    onitemClickListener.onItemClick(cid);
                }
            });

        }

    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView left_rv_tv;

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

    //自定义条目点击事件接口
    private OnitemClickListener onitemClickListener;

    public void setOnitemClickListener(OnitemClickListener onitemClickListener) {
        this.onitemClickListener = onitemClickListener;
    }

    public interface OnitemClickListener{

        void onItemClick(int cid);

    }
}

二级列表子条目 —— RecyclerViewAdapter

package com.example.dell.dianshang7.adapter;

import android.content.Context;
import android.graphics.Bitmap;
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.example.dell.dianshang7.ProductCatagory;
import com.example.dell.dianshang7.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

/**
 * Created by DELL on 2018/6/28.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter{

    private Context context;
    private List list;
    private final DisplayImageOptions options;

    public RecyclerViewAdapter(Context context, List list) {
        this.context = context;
        this.list = list;


        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//使用内存缓存
                .cacheOnDisk(true)//使用磁盘缓存
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式
                .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
                .build();
    }

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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.elv_child_rv_item,parent,false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

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

        ImageLoader.getInstance().displayImage(list.get(position).getIcon(),holder.img,options);

        holder.text.setText(list.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView img;
        private final TextView text;

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

二级列表的适配器 —— ExpandableListViewAdapter

package com.example.dell.dianshang7.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.dell.dianshang7.ProductCatagory;
import com.example.dell.dianshang7.R;

import java.util.List;

/**
 * Created by DELL on 2018/6/28.
 */

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List list;
    private RecyclerViewAdapter rvadapter;
    private GridLayoutManager gridLayoutManager;

    public ExpandableListViewAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getGroupCount() {
        return list == null ? 0 : list.size();
    }

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

    @Override
    public Object getGroup(int i) {
        return null;
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

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

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

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ParentViewHolder parentholder = null;
        if (view == null) {

            view = View.inflate(context, R.layout.elv_parent_item, null);

            parentholder = new ParentViewHolder();

            parentholder.elv_parent_tv = (TextView) view.findViewById(R.id.elv_parent_tv);

            view.setTag(parentholder);

        } else {

            parentholder = (ParentViewHolder) view.getTag();

        }

        parentholder.elv_parent_tv.setText(list.get(i).getName());


        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ChildViewHolder childholder;
        if (view == null) {
            view = View.inflate(context, R.layout.elv_child_item, null);
            childholder = new ChildViewHolder();
            childholder.rvadapter1 = (RecyclerView) view.findViewById(R.id.elv_child_item_rv11);
            view.setTag(childholder);
        } else {
            childholder = (ChildViewHolder) view.getTag();
        }

        List list = this.list.get(i).getList();

        rvadapter = new RecyclerViewAdapter(viewGroup.getContext(), list);

        gridLayoutManager = new GridLayoutManager(viewGroup.getContext(), 2);

        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        childholder.rvadapter1.setLayoutManager(gridLayoutManager);

        childholder.rvadapter1.setAdapter(rvadapter);

        return view;
    }

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

    class ParentViewHolder {

        public TextView elv_parent_tv;

    }

    class ChildViewHolder {

        public RecyclerView rvadapter1;

    }
}

MainActivity:

package com.example.dell.dianshang7;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.example.dell.dianshang7.adapter.ExpandableListViewAdapter;
import com.example.dell.dianshang7.adapter.LeftAdapter;
import com.example.dell.dianshang7.mvp.presenter.Presenter;
import com.example.dell.dianshang7.mvp.view.iview.IViewInterface;
import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity implements IViewInterface {

    //    private int cid = 1;
//    private String url = "https://www.zhaoapi.cn/product/getProductCatagory?cid="+cid;
    private RecyclerView left_rv;
    private int cid = 1;
    private ExpandableListView elv;
    private Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        left_rv = findViewById(R.id.left_rv);
        elv = findViewById(R.id.elv);

        presenter = new Presenter(this);
        presenter.startListView(cid);
    }

    @Override
    public void onLeftResponse(String json) {

        Gson gson = new Gson();

        Catagory catagory = gson.fromJson(json, Catagory.class);

        List data = catagory.getData();

        LeftAdapter adapter = new LeftAdapter(MainActivity.this, data);

        left_rv.setAdapter(adapter);

        //给RecyclerView添加分割线
        left_rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));


        left_rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter.setOnitemClickListener(new LeftAdapter.OnitemClickListener() {
            @Override
            public void onItemClick(int cid) {
                presenter.startListView(cid);
                //Toast.makeText(MainActivity.this, "第" + cid + "个", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onLeftFailure(Exception e) {

    }

    @Override
    public void onRightResponse(String json) {

        Gson gson = new Gson();
        ProductCatagory productCatagory = gson.fromJson(json, ProductCatagory.class);
        List list = productCatagory.getData();

        ExpandableListViewAdapter elvadapter = new ExpandableListViewAdapter(MainActivity.this, list);
        elv.setAdapter(elvadapter);

        //展开二级列表
        for (int i = 0; i < list.size(); i++) {
            elv.expandGroup(i);
        }
    }

    @Override
    public void onRightFailure(Exception e) {

    }
}

 

你可能感兴趣的:(仿京东分类页面)