RecyclerView和Glide的结合实现加载图片

前言

这篇文章的程序已经测试能运行

首先这一篇文章的存在是因为在第一次学习Android的时候出现了许多的坑,这些小问题导致我的学习进度变得缓慢
这些或许对于很多人来说都基本不是问题,可是我却出现了,这可以给大家一些出现问题解决的办法。第一次写文章,如果出现内容错误请谅解,并提醒下我,Thanks♪(・ω・)ノ

参考文章(学习)

https://www.jianshu.com/p/be7c12936149
https://www.jianshu.com/p/4fc6164e4709

环境

因为网上许多资料都有些差异而导致一些代码不能实现,本文章使用的版本

Android studio 3.2
jre1.8
SdkVersion 27

引用

(一)导入

我们首先添加权限AndroidManifest.xml



在build.gradle文件的dependencies中引入该类

compile 'com.github.bumptech.glide:glide:4.6.1'
compile 'com.android.support:recyclerview-v7:27.1.1'

代码

Activity布局文件activity_main.xml代码






Item的布局文件view_item.xml




    

MainActivity代码

package com.example.a85387.glidetoadapter;

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 mRecyclerView;
    private MyAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

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

    public static String[] eatFoodyImages = {
            "http://i.imgur.com/rFLNqWI.jpg",
            "http://i.imgur.com/C9pBVt7.jpg",
            "http://i.imgur.com/rT5vXE1.jpg",
            "http://i.imgur.com/aIy5R2k.jpg",
            "http://i.imgur.com/MoJs9pT.jpg",
            "http://i.imgur.com/S963yEM.jpg",
            "http://i.imgur.com/rLR2cyc.jpg",
            "http://i.imgur.com/SEPdUIx.jpg",
            "http://i.imgur.com/aC9OjaM.jpg",
            "http://i.imgur.com/76Jfv9b.jpg",
            "http://i.imgur.com/fUX7EIB.jpg",
            "http://i.imgur.com/syELajx.jpg",
            "http://i.imgur.com/COzBnru.jpg",
            "http://i.imgur.com/Z3QjilA.jpg",
    };

    private void initData() {
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mAdapter = new MyAdapter(MainActivity.this, getData());
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        // 设置布局管理器
        mRecyclerView.setLayoutManager(mLayoutManager);
        // 设置adapter
        mRecyclerView.setAdapter(mAdapter);
    }

    private ArrayList getData() {
        ArrayList data = new ArrayList<>();
        for (String next : eatFoodyImages) {
//            Log.i("RetrofitLog", "message = " + next);
            data.add(next);
        }
        return data;
    }
}

MyAdapter代码

import android.content.Context;
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 com.bumptech.glide.Glide;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter {
    private ArrayList mData;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    public MyAdapter(Context context,ArrayList data) {
        this.mData = data;
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void updateData(ArrayList data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // 绑定数据
        Log.i("RetrofitLog", "message = " + mData.get(position));
        Glide.with(mContext)
                .load(mData.get(position))
                .into(holder.mImageView);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mImageView;
        public ViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.item_iv);
        }
    }
}

RecyclerView和Glide的结合实现加载图片_第1张图片

你可能感兴趣的:(Android)