1. 程式人生 > >京東的列表展示

京東的列表展示

//設定佈局管理器
public class Frament01Presenter extends AppcreteIml{
    private RecyclerView recycler_01,recycler_02;
    private String url="http://www.zhaoapi.cn/product/getCatagory";
    private String url2="http://www.zhaoapi.cn/product/getProductCatagory?cid=";
    private List<LeftBean.DataBean> list=new ArrayList<>();
    private LeftAdapter leftAdapter;
    private RightAdapter rightAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.frament_01;
    }

    @Override
    public void initData() {
        //初始化控制元件
        recycler_01=(RecyclerView)get(R.id.recycler_01);
        recycler_02=(RecyclerView)get(R.id.recycler_02);
        //設定介面卡
        leftAdapter = new LeftAdapter(context);
        //設定佈局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_01.setLayoutManager(linearLayoutManager);
        //把介面卡設定給recycler
        recycler_01.setAdapter(leftAdapter);
        //設定聯網請求
        doHttp();



        //設定介面卡
        rightAdapter = new RightAdapter(context);
        //設定佈局管理器
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(context);
        linearLayoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_02.setLayoutManager(linearLayoutManager1);
        //把介面卡設定給recycler
        recycler_02.setAdapter(rightAdapter);
        doHttp2(1);
        leftAdapter.result(new LeftAdapter.HttpLister() {
            @Override
            public void success(int postion) {
                int cid = list.get(postion).getCid();
                doHttp2(cid);
                leftAdapter.notifyDataSetChanged();
            }
        });
    }

    private void doHttp2(int cid) {
        new OkHttp1().get(url2+cid).result(new OkHttp1.HttpLister() {
            @Override
            public void success(String data) {
                List<RightBean.DataBean> rightlist = new Gson().fromJson(data, RightBean.class).getData();
                rightAdapter.setList(rightlist);
            }
        });
    }


    private void doHttp() {
        new OkHttp1().get(url).result(new OkHttp1.HttpLister() {
            @Override
            public void success(String data) {
                list = new Gson().fromJson(data, LeftBean.class).getData();
                leftAdapter.setList(list);
            }
        });
    }
    private Context context;
    @Override
    public void getContext(Context context) {
        this.context=context;
    }
}


//左邊的介面卡
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.MyViewHolder>{

    private Context context;
    private List<LeftBean.DataBean> list=new ArrayList<>();

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

    @Override
    public LeftAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context, R.layout.left_layout,null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(LeftAdapter.MyViewHolder holder, final int position) {
        holder.left_txt.setText(list.get(position).getName());
        //條目點選事件
        holder.left_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lister.success(position);
            }
        });
    }

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

    public void setList(List<LeftBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView left_txt;

        public MyViewHolder(View itemView) {
            super(itemView);
            //初始化佈局中的控制元件
            left_txt=(TextView)itemView.findViewById(R.id.left_txt);
        }
    }
    //點選的介面回撥
    private HttpLister lister;
    public void result(HttpLister lister){
        this.lister=lister;
    }
    public interface HttpLister{
        void success(int postion);
    }
}

//右邊的商家名稱介面卡
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.MyViewHolder>{
    private Context context;
    private List<RightBean.DataBean> list=new ArrayList<>();

    public RightAdapter(Context context) {
        this.context=context;
    }

    @Override
    public RightAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context, R.layout.right_layout,null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(RightAdapter.MyViewHolder holder, int position) {
        //設定商家名稱
        holder.right_txt.setText(list.get(position).getName());
        //設定商品的集合
        List<RightBean.DataBean.ListBean> listAll = list.get(position).getList();
        //建立商品的介面卡
        RightChildAdapter rightChildAdapter = new RightChildAdapter(context,listAll);

        //建立佈局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        holder.recycle_right_child.setLayoutManager(gridLayoutManager);
        //把介面卡設定給佈局
        holder.recycle_right_child.setAdapter(rightChildAdapter);

    }

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

    public void setList(List<RightBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView right_txt;
        RecyclerView recycle_right_child;

        public MyViewHolder(View itemView) {
            super(itemView);
            //初始化佈局中的控制元件
            recycle_right_child=(RecyclerView)itemView.findViewById(R.id.recycle_right_child);
            right_txt=(TextView)itemView.findViewById(R.id.right_txt);
        }
    }
}

//右邊的商品展示介面卡
public class RightChildAdapter extends RecyclerView.Adapter<RightChildAdapter.MyViewHolder>{
    private Context context;
    private List<RightBean.DataBean.ListBean> listAll=new ArrayList<>();
    public RightChildAdapter(Context context, List<RightBean.DataBean.ListBean> listAll) {
        this.context=context;
        this.listAll=listAll;
    }

    @Override
    public RightChildAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      //找到佈局
       View view=View.inflate(context, R.layout.child_layout,null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(RightChildAdapter.MyViewHolder holder, int position) {
        //設定圖片和商品名
        holder.child_txt.setText(listAll.get(position).getName());
        Picasso.with(context).load(listAll.get(position).getIcon()).fit().into(holder.child_img);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView child_img;
        TextView child_txt;

        public MyViewHolder(View itemView) {
            super(itemView);
            //初始化佈局中的控制元件
            child_txt=(TextView)itemView.findViewById(R.id.child_txt);
            child_img=(ImageView)itemView.findViewById(R.id.child_img);
        }
    }
}