加载数据(glide)

//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'

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:使用 JavaScript 编写的杀死幽灵游戏(附源代码) 杀死鬼魂游戏是使用 Vanilla JavaScript、CSS 和 HTML 画布开发的简单项目。这款游戏很有趣。玩家必须触摸/杀死游荡的鬼魂才能得分。您必须将鼠标悬停在鬼魂上 - 尽量得分。鬼魂在眨眼间不断从一个地方移动到另一个地方。您必须在 1 分钟内尽可能多地杀死鬼魂。 游戏制作 这个游戏项目只是用 HTML 画布、CSS 和 JavaScript 编写的。说到这个游戏的特点,用户必须触摸/杀死游荡的幽灵才能得分。游戏会根据你杀死的幽灵数量来记录你的总分。你必须将鼠标悬停在幽灵上——尽量得分。你必须在 1 分钟内尽可能多地杀死幽灵。游戏还会显示最高排名分数,如果你成功击败它,该分数会在游戏结束屏幕上更新。 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox。要玩游戏,首先,单击 index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值