xrecyclerView分页加载,多条目显示

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 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值