1. 程式人生 > >仿京東分類

仿京東分類

僅作備忘。

分類頁面:

public class ClassActivity extends AppCompatActivity {

    private String childrenURL = "http://www.zhaoapi.cn/product/getProductCatagory";
    private String classURL = "http://www.zhaoapi.cn/product/getCatagory";
    private static final String TAG = "ClassActivity";
    private RecyclerView recycler_class;
    private RecyclerView recycler_children;
    private List<ChildrenBean.DataBean> data1;

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

        initView();
        initData();

    }

    private void initView() {
        recycler_class = findViewById(R.id.recycler_class);
        recycler_children = findViewById(R.id.recycler_children);
    }

    private void initData() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final HttpUtil instance = HttpUtil.getInstance();
                instance.get(classURL, new Callback() {

                    private ClassAdapter adapter;

                    @Override
                    public void onFailure(Call call, IOException e) {
                        String message = e.getMessage();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String string = response.body().string();
                        Gson gson = new Gson();
                        ClassBean classBean = gson.fromJson(string, ClassBean.class);
                        final List<ClassBean.DataBean> data = classBean.getData();

                        adapter = new ClassAdapter(ClassActivity.this, data);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                final LinearLayoutManager manager = new LinearLayoutManager(ClassActivity.this);
                                manager.setOrientation(LinearLayoutManager.VERTICAL);
                                recycler_class.setLayoutManager(manager);

                                recycler_class.setAdapter(adapter);

                                adapter.setOnItemClickListener(new ClassAdapter.OnItemClickListener() {
                                    @Override
                                    public void getcid(int cid, int position) {
                                        //拼接子類商品Cid
                                        String url = childrenURL + "?cid=" + cid;
                                        Log.d(TAG, "getcid: " + url);

                                        HttpUtil instance1 = HttpUtil.getInstance();
                                        instance1.get(url, new Callback() {
                                            @Override
                                            public void onFailure(Call call, IOException e) {
                                                String message = e.getMessage();
                                            }

                                            @Override
                                            public void onResponse(Call call, Response response) throws IOException {
                                                final String string1 = response.body().string();
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Gson gson = new Gson();
                                                        ChildrenBean childrenBean = gson.fromJson(string1, ChildrenBean.class);
                                                        data1 = childrenBean.getData();
                                                        ChildrenAdapter adapter = new ChildrenAdapter(ClassActivity.this, data1);


                                                        LinearLayoutManager manager1 = new LinearLayoutManager(ClassActivity.this);
                                                        manager1.setOrientation(LinearLayoutManager.VERTICAL);
                                                        recycler_children.setLayoutManager(manager1);
                                                        recycler_children.setAdapter(adapter);


                                                    }
                                                });

                                            }
                                        });


                                    }
                                });
                            }
                        });


                    }
                });
            }
        });


    }

}

分類的介面回撥,將商品id傳至Activity頁面

public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.Myholder> {

    Context context;
    List<ClassBean.DataBean> list;
    private int cid = 1;

    public ClassAdapter(Context context, List<ClassBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    public interface OnItemClickListener {
        void getcid(int cid,int position);
    }
    private OnItemClickListener  listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ClassAdapter.Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.class_item, parent, false);
        Myholder myholder = new Myholder(inflate);
        return myholder;
    }

    @Override
    public void onBindViewHolder(@NonNull ClassAdapter.Myholder holder, final int position) {
        holder.class_title.setText(list.get(position).getName());


        holder.class_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cid = list.get(position).getCid();
                listener.getcid(cid,position);
                Toast.makeText(context, "ss"+cid, Toast.LENGTH_SHORT).show();
            }
        });

    }

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

    public class Myholder extends RecyclerView.ViewHolder {

        private final TextView class_title;
        private final RecyclerView recycler_children;

        public Myholder(View itemView) {
            super(itemView);
            class_title = itemView.findViewById(R.id.class_title);
            recycler_children = itemView.findViewById(R.id.recycler_children);
        }
    }


}