二级列表 用的是两个适配器,一个商家,一个商品;
效果图:
model
public class ClaModel implements IConstant.IModel {
@Override
public void clalist(HashMap<String, String> params, final ModelCallBack callBack) {
OkHttpUtils.getmInstance().doGet(Api.Shop_Api, new OkHttpCallBack() {
@Override
public void success(String result) {
callBack.success(result);
}
@Override
public void error(String msg) {
callBack.error(msg);
}
});
}
public interface ModelCallBack{
void success(String result);
void error(String msg);
}
}
constant
public interface IConstant {
abstract class IPresenter{
public abstract void clalist(HashMap<String,String>params);
}
interface IModel{
void clalist(HashMap<String, String> params, ClaModel.ModelCallBack callBack);
}
interface IView{
void success(String result);
void error(String msg);
}
}
presenter
public class ClaPresenter extends IConstant.IPresenter {
IConstant.IView iView;
ClaModel claModel;
public ClaPresenter(IConstant.IView iView) {
this.iView = iView;
claModel = new ClaModel();
}
@Override
public void clalist(HashMap<String, String> params) {
claModel.clalist(params, new ClaModel.ModelCallBack() {
@Override
public void success(String result) {
iView.success(result);
}
@Override
public void error(String msg) {
iView.error(msg);
}
});
}
}
OKhttp
public interface OkHttpCallBack {
void success(String result);
void error(String msg);
}
public class OkHttpUtils {
private static OkHttpUtils mInstance;
private OkHttpClient okHttpClient;
private Handler handler = new Handler();
public static OkHttpUtils getmInstance(){
if (mInstance == null){
synchronized (OkHttpUtils.class){
if (mInstance == null){
mInstance = new OkHttpUtils();
}
}
}
return mInstance;
}
public OkHttpUtils(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.connectTimeout(5,TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5,TimeUnit.SECONDS)
.build();
}
public void doGet(String url,final OkHttpCallBack callBack){
Request request = new Request.Builder().url(url).get().build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
callBack.error("网络失败");
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code()==200){
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
callBack.success(string);
}
});
}
}
});
}
}
商家适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Myvh> {
Context context;
List<Bean.DataBean>list;
private GoodsAdapter adapter;
public MyAdapter(Context context) {
this.context = context;
list = new ArrayList<>();
}
public void setList(List<Bean.DataBean> list) {
this.list = list;
notifyDataSetChanged();
}
@NonNull
@Override
public MyAdapter.Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.layout, parent, false);
return new Myvh(view);
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.Myvh holder, int position) {
holder.text_id.setText(list.get(position).getName());
//得到子类的数据
List<Bean.DataBean.SpusBean> spus = list.get(position).getSpus();
//设置方向
holder.rev_id.setLayoutManager(new LinearLayoutManager(context));
//设置适配器
adapter = new GoodsAdapter(context,spus);
holder.rev_id.setAdapter(adapter);
}
@Override
public int getItemCount() {
return list.size();
}
public class Myvh extends RecyclerView.ViewHolder {
private final TextView text_id;
private final RecyclerView rev_id;
public Myvh(View itemView) {
super(itemView);
text_id = itemView.findViewById(R.id.text_id);
rev_id = itemView.findViewById(R.id.rev_id);
}
}
}
商品适配器
public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.Myvh>{
private Context context;
private List<Bean.DataBean.SpusBean> goodslist;
public GoodsAdapter(Context context, List<Bean.DataBean.SpusBean> goodslist) {
this.context = context;
this.goodslist = goodslist;
}
@NonNull
@Override
public GoodsAdapter.Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.layout2, parent, false);
return new Myvh(view);
}
@Override
public void onBindViewHolder(@NonNull GoodsAdapter.Myvh holder, int position) {
Glide.with(context).load(goodslist.get(position).getPic_url()).into(holder.image_id);
holder.text_id.setText(goodslist.get(position).getName());
holder.price_id.setText("¥"+goodslist.get(position).getPraise_num()+"");
}
@Override
public int getItemCount() {
return goodslist.size();
}
public class Myvh extends RecyclerView.ViewHolder {
private final ImageView image_id;
private final TextView text_id;
private final TextView price_id;
public Myvh(View itemView) {
super(itemView);
image_id = itemView.findViewById(R.id.image_id);
text_id = itemView.findViewById(R.id.text_id);
price_id = itemView.findViewById(R.id.price_id);
}
}
}
MainActivity主页面
public class MainActivity extends AppCompatActivity implements IConstant.IView {
private XRecyclerView xrev_id;
private MyAdapter myAdapter;
private ClaPresenter claPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到控件
xrev_id = findViewById(R.id.xrev_id);
claPresenter = new ClaPresenter(this);
claPresenter.clalist(new HashMap<String, String>());
xrev_id.setLayoutManager(new LinearLayoutManager(this));
myAdapter = new MyAdapter(this);
}
@Override
public void success(String result) {
Bean bean = new Gson().fromJson(result, Bean.class);
myAdapter.setList(bean.getData());
xrev_id.setAdapter(myAdapter);
}
@Override
public void error(String msg) {
}
}