首先看一看实现的效果图
之后就是实现的代码,首先看布局。
这个是用了scrollView的布局,具体看代码。
private List mList;
private Context mContext;
public MAdpter(List list, Context context) {
mList = list;
mContext = context;
}
//绑定布局
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext, R.layout.item01, null);
return new MyHolder(view);
}
//给控件设置值
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
final TypeSp.DataBean dataBean = mList.get(i);
myHolder.text.setText(dataBean.getName());
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mWuek.DianJi(dataBean.getCid()+"");
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
//获取控件
public class MyHolder extends RecyclerView.ViewHolder {
TextView text;
public MyHolder(@NonNull View itemView) {
super(itemView);
text=itemView.findViewById(R.id.it_text);
}
}
//接口回调
public interface Wuek{
void DianJi(String cid);
}
private Wuek mWuek;
public void setWuek(Wuek wuek) {
mWuek = wuek;
}
}
public class YAdapter extends RecyclerView.Adapter {
private List mList;
private Context mContext;
public YAdapter(List data, Context applicationContext) {
this.mList=data;
this.mContext=applicationContext;
}
//绑定布局
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext, R.layout.item02, null);
return new MyHolder(view);
}
//给控件设置值
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
Commodity.DataBean.ListBean listBean = mList.get(i);
Glide.with(mContext).load(listBean.getIcon()).into(myHolder.imageView);
myHolder.t1.setText(listBean.getName());
}
//获取控件
@Override
public int getItemCount() {
return mList.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
TextView t1,t2;
ImageView imageView;
public MyHolder(@NonNull View itemView) {
super(itemView);
imageView= itemView.findViewById(R.id.it2_image);
t1=itemView.findViewById(R.id.it2_text);
}
}
}
这里写了两个适配器,因为分左右嘛。
最后就是MainActivity的代码
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
import bwie.com.myclassify.adapter.MAdpter;
import bwie.com.myclassify.adapter.YAdapter;
import bwie.com.myclassify.bean.Commodity;
import bwie.com.myclassify.bean.TypeSp;
import bwie.com.myclassify.presenter.CommodityQin;
import bwie.com.myclassify.presenter.FenLei;
public class MainActivity extends AppCompatActivity {
private String Request="http://www.zhaoapi.cn/product/getCatagory";
private String Qing="http://www.zhaoapi.cn/product/getProductCatagory";
private RecyclerView recy_zhu;
private LinearLayout recy_zi;
private FenLei mFenLei;
private CommodityQin mQin;
private MAdpter mMAdpter;
private String cid1="1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//获取控件
recy_zhu = findViewById(R.id.recy_zhu);
recy_zi = findViewById(R.id.recy_zi);
//写一个线性布局
LinearLayoutManager Liner = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//添加
recy_zhu.setLayoutManager(Liner);
//左面的
zhuLei();
//右面的
ziLei();
}
private void zhuLei(){
//网络请求
mFenLei = new FenLei();
mFenLei.Diao(Request);
//请求成功
mFenLei.setKuai(new FenLei.Kuai() {
@Override
public void success(List data) {
if (data!=null){
//获取适配器对象
mMAdpter = new MAdpter(data, getApplicationContext());
//添加适配器
recy_zhu.setAdapter(mMAdpter);
//接口回调
mMAdpter.setWuek(new MAdpter.Wuek() {
@Override
public void DianJi(String cid) {
cid1=cid;
ziLei();
}
});
}
}
});
}
private void ziLei(){
//网络请求post
mQin = new CommodityQin();
//新建一个hashMap
HashMap hashMap= new HashMap<>();
hashMap.put("cid",cid1);
mQin.Diao(Qing,hashMap);
mQin.setKuai(new CommodityQin.Mautio() {
private YAdapter mYAdapter;
//请求成功
@Override
public void success(List data) {
recy_zi.removeAllViews();
if (data!=null){
//循环所有的data
for (int i = 0; i list = dataBean.getList();
//获取适配器对象
mYAdapter = new YAdapter(list, getApplicationContext());
//添加适配器
recyclerView.setAdapter(mYAdapter);
//刷新
mYAdapter.notifyDataSetChanged();
//添加TextView
recy_zi.addView(textView);
//添加recyclerView
recy_zi.addView(recyclerView);
}
}
}
});
}
//释放内存
@Override
protected void onDestroy() {
super.onDestroy();
mFenLei=null;
mQin=null;
}
}