model包
GoodsAdapter
public class GoodsAdapter extends RecyclerView .Adapter {
public static final int TYPE_ONE_IMAGE = 0 ;
public static final int TYPE_TWO_IMAGE = 1 ;
private Context context;
private List<GoodsListBean.DataBean> data = new ArrayList<>();
public GoodsAdapter (Context context) {
this .context = context;
}
public void updateList (List<GoodsListBean.DataBean> data) {
this .data.clear();
addList(data);
notifyDataSetChanged();
}
public void addList (List<GoodsListBean.DataBean> data) {
this .data.addAll(data);
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder holder = null ;
switch (viewType) {
case 0 :
view = View.inflate(context, R.layout.item_goods_list, null );
holder = new GoodsViewHolder(view);
break ;
case 1 :
view = View.inflate(context, R.layout.item_goods_twolist, null );
holder = new GoodsTwoViewHolder(view);
}
return holder;
}
@Override
public void onBindViewHolder (RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case 0 :
GoodsViewHolder holderOne = (GoodsViewHolder) holder;
String image = data.get(position).getImages();
String pic = image.split("\\|" )[0 ];
holderOne.img.setImageURI(pic);
holderOne.title.setText(data.get(position).getTitle());
break ;
case 1 :
GoodsTwoViewHolder holdertwo = (GoodsTwoViewHolder) holder;
String images = data.get(position).getImages();
String pic_url = images.split("\\|" )[0 ];
holdertwo.img.setImageURI(pic_url);
holdertwo.title.setText(data.get(position).getTitle());
}
}
@Override
public int getItemViewType (int position) {
if (position%2 ==0 ) {
return TYPE_TWO_IMAGE;
} else {
return TYPE_ONE_IMAGE;
}
}
@Override
public int getItemCount () {
return data.size();
}
class GoodsViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public SimpleDraweeView img;
public GoodsViewHolder (View itemView) {
super (itemView);
title = itemView.findViewById(R.id.tv);
img = itemView.findViewById(R.id.img);
}
}
class GoodsTwoViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public SimpleDraweeView img;
public GoodsTwoViewHolder (View itemView) {
super (itemView);
title = itemView.findViewById(R.id.tv);
img = itemView.findViewById(R.id.img);
}
}
}
GoodsModel
public class GoodsModel implements IModel {
@Override
public void getData (String pscid,String page, final IPresenter iPresenter) {
Map<String,String> params=new HashMap<>();
params.put("pscid" ,pscid);
OkHttpUtils okHttpUtils= OkHttpUtils.getInstance();
okHttpUtils.doPost(HttpConfig.GoodsListUrl, params, new OnFinishListener() {
@Override
public void onSuccess (String json) {
iPresenter.onSuccess(json);
}
@Override
public void onFailed (String error) {
iPresenter.onFailed(error);
}
});
}
}
IModel
public interface IModel {
void getData(String pscid,String page, IPresenter iPresenter);
}
MyApp
public class MyApp extends Application {
@Override
public void onCreate () {
super .onCreate();
Fresco.initialize(this );
}
}
presenter包
GoodsPresenter
public class GoodsPresenter implements IPresenter {
private static final String TAG = "GoodsPresenter----" ;
private IView iView;
private GoodsModel goodsModel;
public GoodsPresenter (IView iView) {
this .iView = iView;
goodsModel=new GoodsModel();
}
@Override
public void getCarInfo (String pscid,String page) {
goodsModel.getData(pscid,page,this );
}
@Override
public void onSuccess (String json) {
Log.d(TAG, "onSuccess: " +json);
if (null !=iView){
Gson gson=new Gson();
GoodsListBean goodsListBean = gson.fromJson(json, GoodsListBean.class);
List<GoodsListBean.DataBean> data = goodsListBean.getData();
Log.d(TAG, "onSuccess: " +data);
iView.showDataToView(data);
}
}
@Override
public void onFailed (String error) {
if (null !=iView ){
Log.d(TAG, "onFailed: " +error);
}
}
@Override
public void onDestory () {
if (null !=iView){
iView=null ;
}
}
}
IPresenter
public