MVP+搜索+recyclerview+springview
来源:互联网 发布:下载读书软件 编辑:程序博客网 时间:2024/06/06 02:41
图如下:
记得要导okhttp的原生包即可;
导依赖:
compile 'com.google.code.gson:gson:2.8.2' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.github.liuguangqiang.SuperRecyclerView:super-recyclerview:0.1.2' compile 'com.liaoinstan.springview:library:1.2.6' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
代码如下:
首先是Mainactivity布局:
<RelativeLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="搜索商品" android:textSize="20dp"/> <ImageView android:id="@+id/image" android:layout_width="60dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/san"/> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#000000"/> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:orientation="horizontal"> <EditText android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:hint="请输入关键字"/> <Button android:id="@+id/sousuo" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="搜索"/> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#000000"/> <com.liaoinstan.springview.widget.SpringView android:id="@+id/springview" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"/> </com.liaoinstan.springview.widget.SpringView>
在是适配器的两个布局:
<ImageView android:id="@+id/image" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ssdsdsddfffddffdfd" android:textSize="20dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/price" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="原价:的颠三倒四"/> <TextView android:id="@+id/youhui" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="大的司法改革" android:textColor="#ff0000"/> </LinearLayout> </LinearLayout>
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
<ImageView android:id="@+id/image1" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/title1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ssdsdsddfffddffdfd" android:textSize="20dp"/> <TextView android:id="@+id/price1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="原价:的颠三倒四"/> <TextView android:id="@+id/youhui1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="大的司法改革" android:textColor="#ff0000"/>
MVP的model传给mymolder的参数:
public interface SouModel { public void get(String name, String page, Callback callback);}Mymodel
public class MySouModel implements SouModel { @Override public void get(String name, String page, Callback callback) { HashMap<String, String> map = new HashMap<>(); map.put("keywords",name); map.put("page",page); OkHttp3Utils.doPost("http://120.27.23.105/product/searchProducts?source=android",map,callback); }}
MVP的view:
public interface SouView { public void success(Bean bean); public void failuer(Exception e);}
MVP的persenter:
public class SouPersenter { SouView view; Context context; private final MySouModel model; public SouPersenter(SouView view, Context context) { this.view = view; this.context = context; model = new MySouModel(); } public void getData(String name,String page){ model.get(name, page, new OnUiCallback() { @Override public void onFailed(Call call, IOException e) { if (view != null){ view.failuer(e); } } @Override public void onSuccess(String result) { Gson gson = new Gson(); Bean bean = gson.fromJson(result, Bean.class); view.success(bean); } }); } public void sasa(){ this.view = null; }}
MainActivity页面:
public class MainActivity extends AppCompatActivity implements SouView { @BindView(R.id.image) ImageView image; @BindView(R.id.name) EditText name; @BindView(R.id.sousuo) Button sousuo; @BindView(R.id.recyclerview) RecyclerView recyclerview; @BindView(R.id.springview) SpringView springview; SouPersenter persenter = new SouPersenter(this, this); List<Bean.DataBean> list = new ArrayList<>(); private boolean flag = true; private MyAdapter adapter; private int page = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); springview.setHeader(new DefaultHeader(this)); springview.setFooter(new DefaultFooter(this)); //设置SpringView的刷新监听事件 springview.setListener(new SpringView.OnFreshListener() { @Override public void onRefresh() { springview.onFinishFreshAndLoad(); adapter.notifyDataSetChanged(); } @Override public void onLoadmore() { persenter.getData(name.getText().toString(), page + ""); springview.onFinishFreshAndLoad(); } }); } @OnClick({R.id.image, R.id.sousuo}) public void onClick(View view) { switch (view.getId()) { case R.id.image: if (flag == false) { flag = true; image.setImageResource(R.drawable.san); LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); recyclerview.setLayoutManager(manager); adapter = new MyAdapter(this, list, flag); recyclerview.setAdapter(adapter); setadapter(); } else if (flag == true) { flag = false; image.setImageResource(R.drawable.si); GridLayoutManager manager = new GridLayoutManager(this, 2); recyclerview.setLayoutManager(manager); adapter = new MyAdapter(this, list, flag); recyclerview.setAdapter(adapter); setadapter(); } break; case R.id.sousuo: if (flag == true) { LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); recyclerview.setLayoutManager(manager); adapter = new MyAdapter(this, list, flag); recyclerview.setAdapter(adapter); } else if (flag == false) { GridLayoutManager manager = new GridLayoutManager(this, 2); recyclerview.setLayoutManager(manager); adapter = new MyAdapter(this, list, flag); recyclerview.setAdapter(adapter); } persenter.getData(name.getText().toString(), "1"); break; } } private void setadapter() { if (adapter != null) { adapter = new MyAdapter(this, list, flag); recyclerview.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); } } @Override public void success(Bean bean) {// for (int i = 0; i <bean.getData().size() ; i++) {// list.add(bean.getData().get(i));// } list.addAll(bean.getData()); adapter = new MyAdapter(this, list, flag); recyclerview.setAdapter(adapter); } @Override public void failuer(Exception e) { Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); persenter.sasa(); }}
adapter适配器页面:
public class MyAdapter extends RecyclerView.Adapter { Context context; List<Bean.DataBean> list; boolean flag; public MyAdapter(Context context, List<Bean.DataBean> list, boolean flag) { this.flag = flag; this.context = context; this.list = list; ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context)); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (flag == true) { View view = LayoutInflater.from(context).inflate(R.layout.item_adapter, null); return new MyViewHolder(view); } else if (flag == false) { View view = LayoutInflater.from(context).inflate(R.layout.item_si, null); return new ViewHolder01(view); } return new MyViewHolder(parent); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof MyViewHolder) { MyViewHolder holder1 = (MyViewHolder) holder; holder1.title.setText(list.get(position).getTitle()); holder1.youhui.setText("优惠价:" + list.get(position).getPrice()); holder1.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线(删除线) holder1.price.setText("原价:¥" + list.get(position).getBargainPrice()); String[] split = list.get(position).getImages().split("\\|"); ImageLoader.getInstance().displayImage(split[0], holder1.image); }else if (holder instanceof ViewHolder01) { ViewHolder01 holder1 = (ViewHolder01) holder; holder1.title1.setText(list.get(position).getTitle()); holder1.youhui1.setText("优惠价:" + list.get(position).getPrice()); holder1.price1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线(删除线) holder1.price1.setText("原价:¥" + list.get(position).getBargainPrice()); String[] split = list.get(position).getImages().split("\\|"); ImageLoader.getInstance().displayImage(split[0], holder1.image1); } } @Override public int getItemCount() { return list == null ? 0 : list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.image) ImageView image; @BindView(R.id.title) TextView title; @BindView(R.id.price) TextView price; @BindView(R.id.youhui) TextView youhui; public MyViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } static class ViewHolder01 extends RecyclerView.ViewHolder { @BindView(R.id.image1) ImageView image1; @BindView(R.id.title1) TextView title1; @BindView(R.id.price1) TextView price1; @BindView(R.id.youhui1) TextView youhui1; ViewHolder01(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } }}
阅读全文
0 0
- MVP+搜索+recyclerview+springview
- mvp+Recyclerview+okhttp+Springview
- MVP简单实现OKHttp+recyclerview+springview
- MVP操作RecyclerView多条目SpringView,OKHttp封装请求网络
- MVP+OKHttp+Recyclerview+Springview下拉刷新上拉加载
- MVP okhttp+springview+RecyclerView 上拉下拉功能+多条目加载
- RecyclerView + SpringView 的混合使用
- 使用MVP,OKHttp封装类及其拦截器,RecyclerView,SpringView,ImageLoader实现数据的多条目加载
- MVP实现Recyclerview加Springview上拉加载下拉刷新点击条目可跳转到购物车
- 使用MVP注册登录模块+封装的OKhttp,拦截器+QQ第三方登录+RecyclerView+SpringView上拉加载下拉刷新网络数据
- RecyclerView条目跳转+SpringView数据刷新加载+MVP+OKhttp+拦截器+自定义view 实现请求网络数据的二级列表购物车
- MvP+recyclerview
- Mvp+RecyclerView
- MVP+RecyclerView
- springview
- SpringView
- springview
- Springview
- 如何清楚redis中所有数据库
- Excel中 VLOOKUP 函数的使用
- org.postgresql.util.PSQLException: 栏位索引超过许可范围:1,栏位数:0。
- 2017.12.18 关于前端的基本介绍
- 下载文件:中文名乱码问题
- MVP+搜索+recyclerview+springview
- 版本兼容解决方法
- function自调用后面的分号
- Quartz Spring与Spring Task总结
- 重磅 | 北京出台自动驾驶测试新规:车内须配备司机应急
- 现场 | 大众真的懂AI吗?专家为什么认为这是盲人在摸象?
- 现场 | 人工智能在金融、物流、教育、儿童领域究竟如何落地?
- 004
- 阿里云函数计算