Mvp刷新数据

来源:互联网 发布:数据库怎么查重复数据 编辑:程序博客网 时间:2024/06/06 00:18

model层

public class ShopModel {    public void shopping(int page, final IShopModel iShopModel){        HashMap<String,String> map=new HashMap<>();        map.put("pscid",39+"");        map.put("page",page+"");        OkHttpUtil.getInstance().doGet(Contanst.SHOP, map, new CallBack() {            @Override            public void onFailure(String error) {                iShopModel.onFailure(error);            }            @Override            public void onSuccess(String json) {                Gson gson = new Gson();                Shopbean shopbean = gson.fromJson(json, Shopbean.class);                iShopModel.onSuccess(shopbean);            }        });    }}
//接口

public interface IShopModel {    void onFailure(String error);    void onSuccess(Shopbean shopbean);}
present层

public class ShopPresenter {    private IShopView iShopView;    private ShopModel shopModel;    public ShopPresenter(IShopView iShopView){        this.iShopView=iShopView;    }    public void shooping(int page){        shopModel = new ShopModel();        shopModel.shopping(page, new IShopModel() {            @Override            public void onFailure(String error) {                iShopView.onFailure(error);            }            @Override            public void onSuccess(Shopbean shopbean) {                iShopView.onSuccess(shopbean);            }        });    }}
view层

//布局

<com.jcodecraeer.xrecyclerview.XRecyclerView    android:id="@+id/recycler"    android:layout_width="match_parent"    android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>

导入依赖

compile 'com.jcodecraeer:xrecyclerview:1.3.2'
//代码

public class Main3Activity extends AppCompatActivity implements IShopView{    private int page=0;    private List<Shopbean.DataBean> list=new ArrayList<>();    private XRecyclerView recycler;    private ShopPresenter shopPresenter;    private LinearLayoutManager inearLayoutManager;    private MyAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main3);        recycler=(XRecyclerView)findViewById(R.id.recycler);        //上拉刷新        recycler.setPullRefreshEnabled(true);        //下拉加载        recycler.setLoadingMoreEnabled(true);        shopPresenter = new ShopPresenter(this);        shopPresenter.shooping(0);        //设置监听        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {            @Override            public void onRefresh() {                list.clear();                page=0;                shopPresenter.shooping(page);                recycler.refreshComplete();                adapter.notifyDataSetChanged();            }            @Override            public void onLoadMore() {                page++;                shopPresenter.shooping(page);                recycler.refreshComplete();                adapter.notifyDataSetChanged();            }        });    }    @Override    public void onFailure(String error) {        Toast.makeText(Main3Activity.this,error,Toast.LENGTH_LONG).show();    }    @Override    public void onSuccess(Shopbean shopbean) {        List<Shopbean.DataBean> data = shopbean.getData();        list.addAll(data);        //创建布局管理器        inearLayoutManager = new LinearLayoutManager(Main3Activity.this,LinearLayoutManager.VERTICAL,false);        recycler.setLayoutManager(inearLayoutManager);         adapter = new MyAdapter(this, list);         recycler.setAdapter(adapter);    }}
//创建适配器

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{    private Context context;    private List<Shopbean.DataBean> list;    private MyViewHolder vh;    public MyAdapter(Context context, List<Shopbean.DataBean> list) {        this.context=context;        this.list=list;    }    @Override    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View inflate = View.inflate(context, R.layout.iteam_view, null);        vh=new MyViewHolder(inflate);        return vh;    }    @Override    public void onBindViewHolder(MyViewHolder holder, int position) {        String images = list.get(position).getImages();        String[] split = images.split("\\|");        Glide.with(context).load(split[0]).into(holder.img);        holder.t1.setText(list.get(position).getTitle());        holder.t2.setText("原价:¥"+list.get(position).getPrice()+"");        holder.t2.setPaintFlags(holder.t2.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);        holder.t3.setText("优惠价:¥"+list.get(position).getBargainPrice()+"");    }    @Override    public int getItemCount() {        return list.size();    }    class MyViewHolder extends RecyclerView.ViewHolder{        private TextView t1;        private TextView t2;        private TextView t3;        private ImageView img;        public MyViewHolder(View itemView) {            super(itemView);            img = itemView.findViewById(R.id.img);            t1 = itemView.findViewById(R.id.t1);            t2 = itemView.findViewById(R.id.t2);            t3 = itemView.findViewById(R.id.t3);        }    }}
//布局

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">    <ImageView        android:id="@+id/img"        android:layout_width="150dp"        android:layout_height="120dp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/t1"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/t2"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/t3"            android:textColor="#ff0000"            />    </LinearLayout></LinearLayout>