加载数据(glide)
来源:互联网 发布:csol死神辅助源码 编辑:程序博客网 时间:2024/06/02 01:01
//activity_main
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.shuaxin.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:text="商品列表" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></LinearLayout>//item
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/book_img" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/book_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="120dp" android:layout_marginTop="20dp" android:text="123" android:textColor="#000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="120dp" android:layout_marginTop="60dp" android:text="原价:" android:textColor="#000" /> <TextView android:id="@+id/text_oldprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="150dp" android:layout_marginTop="60dp" android:text="456" android:textColor="#000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="120dp" android:layout_marginTop="100dp" android:text="优惠价:" android:textColor="#F23707" /> <TextView android:id="@+id/text_newprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="170dp" android:layout_marginTop="100dp" android:text="234555" android:textColor="#F23707" /></RelativeLayout>//MainActivity
package com.example.shuaxin;import android.content.Context;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import com.example.shuaxin.adapter.MyAdapter;import com.example.shuaxin.bean.Bijiben;import com.example.shuaxin.pre.BookPre;import com.example.shuaxin.view.BView;import java.util.List;public class MainActivity extends AppCompatActivity implements BView { private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); sendEmptyMessageDelayed(0, 1000); } }; private RecyclerView rv; private int q = 1; private boolean flag = true; private MyAdapter adapter; private Context mContext; private BookPre bookPre; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rv = findViewById(R.id.rv); bookPre = new BookPre(this); bookPre.getBijiuben(); } @Override public void onSuccess(final List<Bijiben> list) { //Log.i("111",list.toString()+""); //LinearLayout LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); //GridLayoutL //LinearLayoutManager.VERTICAL LinearLayoutManager.HORIZONTAL横向 //GridLayoutManager manager = new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false); rv.setLayoutManager(manager); adapter = new MyAdapter(this, list, 2); rv.setAdapter(adapter); flag = false; } @Override public void onFailed() { }}//Adapter
package com.example.shuaxin.adapter;import android.content.Context;import android.graphics.Paint;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import com.example.shuaxin.R;import com.example.shuaxin.bean.Bijiben;import java.util.List;public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private Context context; private List<Bijiben> list; private int q; public MyAdapter(Context context, List<Bijiben> list, int q) { this.context = context; this.list = list; this.q = q; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item, null); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) { holder.text.setText(list.get(position).getTitle()); holder.newprice.setText(list.get(position).getNewprice() + ""); holder.oldprice.setText(list.get(position).getOldprice() + ""); String img = list.get(position).getImg(); String[] split = img.split("\\|"); //list.get(position).getImg() //图片加载 Glide.with(context).load(split[0]).into(holder.img); //ImageLoader.getInstance().displayImage(split[0], holder.img); } @Override public int getItemCount() { if (list != null) { return list.size(); } else { Log.i("12", list.size() + "放松放松"); } return 0; } class ViewHolder extends RecyclerView.ViewHolder { ImageView img; TextView text; TextView oldprice; TextView newprice; public ViewHolder(View itemView) { super(itemView); img = (ImageView) itemView.findViewById(R.id.book_img); text = (TextView) itemView.findViewById(R.id.book_text); oldprice = (TextView) itemView.findViewById(R.id.text_oldprice); oldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); newprice = (TextView) itemView.findViewById(R.id.text_newprice); } }}//BookModel
package com.example.shuaxin.model;import android.os.Handler;import com.example.shuaxin.bean.Bijiben;import com.example.shuaxin.bean.Books;import com.example.shuaxin.interfaces.BookPresenter;import com.google.gson.Gson;import java.io.IOException;import java.util.ArrayList;import java.util.List;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class BookModel { //定义一个数据集合 private static List<Bijiben> list = new ArrayList<>(); //定义一个handler private static Handler handler = new Handler(); //请求数据的方法 public void getBijiuben(final BookPresenter bookPresenter) { new Thread() { @Override public void run() { super.run(); try { //OK请求数据 OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .get() .url("http://120.27.23.105/product/searchProducts?source=android&keywords=笔记本&page=1") .build(); //准备发送网络请求 okhttp3.Call call = okHttpClient.newCall(request); //执行请求 Response response = call.execute(); //得到请求的结果 String result = response.body().string(); //然后解析数据 Gson gson = new Gson(); Books books = gson.fromJson(result.toString(), Books.class); List<Books.DataBean> data = books.getData(); for (int i = 0; i < data.size(); i++) { String title = data.get(i).getTitle(); double newprice = data.get(i).getPrice(); double bargainPrice = data.get(i).getBargainPrice(); String images = data.get(i).getImages(); list.add(new Bijiben(images, title, bargainPrice, newprice)); } handler.post(new Runnable() { @Override public void run() { bookPresenter.onSuccess(list); } }); } catch (IOException e) { e.printStackTrace(); } } }.start(); }}//BookPre
package com.example.shuaxin.pre;import com.example.shuaxin.bean.Bijiben;import com.example.shuaxin.interfaces.BookPresenter;import com.example.shuaxin.model.BookModel;import com.example.shuaxin.view.BView;import java.util.List;public class BookPre implements BookPresenter { private BView bView; private BookModel bookModel; public BookPre(BView bView) { this.bView = bView; bookModel = new BookModel(); } public void getBijiuben() { bookModel.getBijiuben(this); } @Override public void onSuccess(List<Bijiben> list) { bView.onSuccess(list); } @Override public void onFailed() { }}//BookPresenter
package com.example.shuaxin.interfaces;import com.example.shuaxin.bean.Bijiben;import java.util.List;public interface BookPresenter { void onSuccess(List<Bijiben> list); void onFailed();}//BView
package com.example.shuaxin.view;import com.example.shuaxin.bean.Bijiben;import java.util.List;public interface BView { void onSuccess(List<Bijiben> list); void onFailed();}//bean
package com.example.shuaxin.bean;public class Bijiben { private String img; private String title; private double oldprice; private double newprice; public String getImg() { return img; } public void setImg(String img) { this.img = img; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getOldprice() { return oldprice; } public void setOldprice(double oldprice) { this.oldprice = oldprice; } public double getNewprice() { return newprice; } public void setNewprice(double newprice) { this.newprice = newprice; } @Override public String toString() { return "Bijiben{" + "img='" + img + '\'' + ", title='" + title + '\'' + ", oldprice=" + oldprice + ", newprice=" + newprice + '}'; } public Bijiben(String img, String title, double oldprice, double newprice) { this.img = img; this.title = title; this.oldprice = oldprice; this.newprice = newprice; }}//Books
package com.example.shuaxin.bean;import java.util.List;public class Books { private String msg; private String code; private String page; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }}//清单
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />//依赖
compile 'com.android.support:recyclerview-v7:26.1.0'compile 'com.squareup.okhttp3:okhttp:3.9.1'compile 'org.xutils:xutils:3.1.24'implementation 'com.google.code.gson:gson:2.2.4'compile 'com.github.bumptech.glide:glide:3.7.0'
阅读全文
0 0
- 加载数据(glide)
- 使用okhttp加载服务器数据,Glide加载图片
- OkHttp解析数据,RecyclerView和Glide的加载图片
- 图片加载库Glide
- Android Glide 图片加载
- Glide加载图片
- 图片加载库Glide
- 图片加载库Glide
- Glide动画加载
- glide 加载大图
- 图片加载框架Glide
- Glide图片加载
- Glide高级加载
- Glide图片加载
- Glide加载bitmap
- Glide 加载图片变形
- 使用Glide加载gif
- Glide加载图片
- Android开发JDK配置环境变量
- Java实现邮箱验证码
- 使用Microsoft的ClickOnce发布版本及网页更新版本
- Java四种线程池的用法分析
- android EditText输入四位数字密码明文显示
- 加载数据(glide)
- tensorflow 批标准化Batch_normalization
- SpringBoot入门-2(HelloWorld)
- HAWQ安装方式之RPM包安装
- java keytool证书工具使用小结
- 网站引导页插件intro.js 的用法
- 安装好Protege4.3后 无法启动,什么鬼
- vue购物车总价计算
- AIX 系统介绍