Android学习--Glide、RecyclerView加载网络图片

    Glide是GitHub上一款开源的图片加载框架,即可以加载本地图片,也可以加载网络图片。使用方法也比较简单

最简单的使用:
Glide.with(this)
     .load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
     .into(imageView);

具体用法可参考郭霖大神博客:

https://blog.csdn.net/sgiceleo/article/details/64440783

结合RecyclerView实现网络加载图片

1、build.gradle中添加包依赖

dependencies {
    ......
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:recyclerview-v7:28.0.0'

}

2、新建实体类Bird

package com.example.gliderecyclertest;

public class Bird {

    public Bird(){

    }

    private String name;

    private String imageUrl;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
}

3、新建数据适配器类DataAdapter 继承RecyclerView.Adapter

package com.example.gliderecyclertest;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;

import java.util.List;

import javax.sql.DataSource;

public class DataAdapter extends RecyclerView.Adapter {

    public Context context;
    private List birdList;

    public DataAdapter(Context context, List birdList){
        this.context = context;
        this.birdList = birdList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        viewHolder.textView.setText(birdList.get(i).getName());
        Glide.with(context)
                .load(birdList.get(i).getImageUrl())
                .placeholder(R.drawable.loading)
                .into(new SimpleTarget() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) {
                        viewHolder.imageView.setImageDrawable(resource);
                    }
                });
    }

    @Override
    public int getItemCount() {
        return birdList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView imageView;
        public TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.bird_image);
            textView = (TextView) itemView.findViewById(R.id.bird_name);
        }
    }
}

4、布局文件activity_main.xml添加一个RecyclerView控件




    

RecyclerView单Item布局文件item_layout.xml包含一个ImageView和TextView控件




    


        


        
    


5、主MainActivity中

package com.example.gliderecyclertest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ArrayList birdList;
    private DataAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);

        birdList = initData();
        adapter = new DataAdapter(getApplicationContext(), birdList);
        recyclerView.setAdapter(adapter);
    }

    private final String names[] = {
            "aa",
            "bb",
            "cc",
            "dd",
            "ee",
            "ff",
            "gg",
            "hh",
            "ii",
    };

    private final String imageUrls[] = {
            "http://hawksaloft.org/wp-content/uploads/2012/08/614612316_20090805-_mg_3411-rufous-hummingbird-5x7.jpg",
            "http://www.gregscott.com/gjs_2007_spring/hummingbird/20070311_1948_100_0560.rufous_humminbird.jpg",
            "http://hawksaloft.org/wp-content/uploads/2012/08/614612316_20090805-_mg_3411-rufous-hummingbird-5x7.jpg",
            "http://www.people.com.cn/img/2016wb/images/logo01.png",
            "http://pic.people.com.cn/NMediaFile/2019/0610/MAIN201906101515000281782550170.jpg",
            "http://pic.people.com.cn/NMediaFile/2019/0610/MAIN201906101623000225736784754.jpg",
            "http://pic.people.com.cn/NMediaFile/2019/0610/MAIN201906101644000185878125315.jpg",
            "http://world.people.com.cn/NMediaFile/2019/0610/MAIN201906100858000177823780005.jpg",
            "http://world.people.com.cn/NMediaFile/2018/0913/MAIN201809131000000359718834615.jpg",
};

    private ArrayList initData(){
        ArrayList birds = new ArrayList<>();
        for (int i = 0; i < names.length; i++ ){
            Bird bird = new Bird();
            bird.setName(names[i]);
            bird.setImageUrl(imageUrls[i]);
            birds.add(bird);
        }
        return birds;
    }

}

公司网络限制,对外网有限制,加载的部分鸟类图片显示不出来,囧?,用其他图片代替

6、运行效果

Android学习--Glide、RecyclerView加载网络图片_第1张图片      Android学习--Glide、RecyclerView加载网络图片_第2张图片

 

你可能感兴趣的:(Android学习)