RecyclerView和Glide的結合實現載入圖片
阿新 • • 發佈:2018-11-10
前言
這篇文章的程式已經測試能執行
首先這一篇文章的存在是因為在第一次學習Android的時候出現了許多的坑,這些小問題導致我的學習進度變得緩慢
這些或許對於很多人來說都基本不是問題,可是我卻出現了,這可以給大家一些出現問題解決的辦法。第一次寫文章,如果出現內容錯誤請諒解,並提醒下我,Thanks♪(・ω・)ノ
參考文章(學習)
https://www.jianshu.com/p/be7c12936149
https://www.jianshu.com/p/4fc6164e4709
環境
因為網上許多資料都有些差異而導致一些程式碼不能實現,本文章使用的版本
Android studio 3.2
jre1.8
SdkVersion 27
引用
(一)匯入
我們首先新增許可權AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在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程式碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> </LinearLayout>
Item的佈局檔案view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<ImageView
android:id="@+id/item_iv"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
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<String> getData() {
ArrayList<String> 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<MyAdapter.ViewHolder> {
private ArrayList<String> mData;
private LayoutInflater mLayoutInflater;
private Context mContext;
public MyAdapter(Context context,ArrayList<String> data) {
this.mData = data;
this.mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
public void updateData(ArrayList<String> 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);
}
}
}