实现商品列表展示

public class Fragment_two_Presenter extends AppDelegate {
    private RecyclerView left,right;
    private List list= new ArrayList<>();
    private int cid =1;
    private ChildApdater childApdater;
    private List data1;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_two;
    }
    @Override
    public void initData() {
        super.initData();

        left = (RecyclerView)get(R.id.left);
        right = (RecyclerView)get(R.id.rigth);

        new HttpHelper().get("http://www.zhaoapi.cn/product/getCatagory").result(new HttpHelper.Httplistenner() {
            @Override
            public void success(String data) {
                Gson gson = new Gson();
                Bean bean = gson.fromJson(data,Bean.class);
                data1 = bean.getData();

                //Toast.makeText(context,"da"+data,Toast.LENGTH_SHORT).show();
                list.clear();
                list.addAll(data1);
            }

            @Override
            public void error(String error) {

            }
        });
        rigth(1);
        MyApdater myApdater = new MyApdater(context,list);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        left.setLayoutManager(manager);

        left.setAdapter(myApdater);
        myApdater.click(new MyApdater.SetOnclick() {
            @Override
            public void getonclick(int id) {
                Toast.makeText(context,""+id,Toast.LENGTH_SHORT).show();
                int cid = data1.get(id).getCid();
                rigth(cid);
            }
        });


    }

    private void rigth(int cid) {

        new HttpHelper().get("http://www.zhaoapi.cn/product/getProductCatagory?cid="+cid).result(new HttpHelper.Httplistenner() {
            @Override
            public void success(String data) {
                Gson gson = new Gson();
                ChildBean childBean = gson.fromJson(data,ChildBean.class);
                List data2 = childBean.getData();
                //Toast.makeText(context,"dsfsd"+data2,Toast.LENGTH_SHORT).show();
                childApdater.setList(data2);
            }

            @Override
            public void error(String error) {

            }
        });
        childApdater = new ChildApdater(context);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        right.setLayoutManager(manager);
        right.setAdapter(childApdater);
    }

    private Context context;
    @Override
    public void getContext(Context context) {
        super.getContext(context);
        this.context=context;
    }

}

/*
* 
* LeftApdater   点击事件 回调
* 
* */
public class MyApdater extends RecyclerView.Adapter{
    private Context context;
    private List list;
    public MyApdater(Context context, List list) {
        this.context=context;
        this.list=list;
    }

    @NonNull
    @Override
    public MyApdater.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.left,null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyApdater.ViewHolder viewHolder, final int i) {
        viewHolder.text_left.setText(list.get(i).getName());
        viewHolder.text_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setOnclick.getonclick(list.get(i).getCid());
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView text_left;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text_left = (TextView)itemView.findViewById(R.id.text_left);
        }
    }
    private SetOnclick setOnclick;
    //接口回调
    public void click(SetOnclick setOnclick){
        this.setOnclick=setOnclick;
    }
    //定义接口
    public interface SetOnclick{
        void getonclick(int id);
    }
}

/**
展示右侧的数据
**/
public class ChildApdater extends RecyclerView.Adapter{
private Context context;
public ChildApdater(Context context) {
this.context=context;
}

@NonNull
@Override
public ChildApdater.MyApdater onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(context,R.layout.right,null);
    MyApdater apdater = new MyApdater(view);
    return apdater;
}

@Override
public void onBindViewHolder(@NonNull ChildApdater.MyApdater myApdater, int i) {
    myApdater.text_right.setText(list.get(i).getName());
    List list = this.list.get(i).getList();
    GridLayoutManager manager = new GridLayoutManager(context,3);

    ZiApdater ziApdater = new ZiApdater(context);
    myApdater.right_recyclerview.setLayoutManager(manager);
    myApdater.right_recyclerview.setAdapter(ziApdater);
    ziApdater.setList(list);
}

@Override
public int getItemCount() {
    return list.size();
}
private List list=new ArrayList<>();
public void setList(List list) {
    this.list = list;
    notifyDataSetChanged();
}


public class MyApdater extends RecyclerView.ViewHolder {
    RecyclerView right_recyclerview;
    TextView text_right;

    public MyApdater(@NonNull View itemView) {
        super(itemView);
        text_right = (TextView)itemView.findViewById(R.id.text_right);
        right_recyclerview = (RecyclerView)itemView.findViewById(R.id.right_recyclerview);
    }
}

}


``

/*
*展示右侧的子数据
**/
public class ZiApdater extends RecyclerView.Adapter{
private Context context;
public ZiApdater(Context context) {
this.context=context;
}

@NonNull
@Override
public ZiApdater.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(context, R.layout.right_child,null);
    MyViewHolder viewHolder = new MyViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ZiApdater.MyViewHolder myViewHolder, int i) {
    myViewHolder.text.setText(list.get(i).getName());
    Glide.with(context).load(list.get(i).getIcon()).into(myViewHolder.pic);
}

@Override
public int getItemCount() {
    return list.size();
}
private List list = new ArrayList<>();
public void setList(List list) {
    this.list = list;
    //刷新适配器
    notifyDataSetChanged();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    ImageView pic;
    TextView text;

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

}


你可能感兴趣的:(实现商品列表展示)