RecycleView 瀑布流 及glide網路載入圖片
//第一步
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.example.recycleview_jinjie.utils.Images;
import java.util.ArrayList;
import java.util.Random;
public class WaterWallActivity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water_wall);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//瀑布效果
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
recyclerView.setAdapter(new MyAdapter());
}
class MyViewHoler extends RecyclerView.ViewHolder {
public final ImageView iv_water_item;
public MyViewHoler(View itemView) {
super(itemView);
iv_water_item = (ImageView) itemView.findViewById(R.id.iv_water_item);
}
}
class MyAdapter extends RecyclerView.Adapter<MyViewHoler> {
private final ArrayList<Integer> heightList;
public MyAdapter() {
heightList = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < Images.imageUrls.length; i++) {
int height = 100 + random.nextInt(300);
heightList.add(height);
}
}
@Override
public MyViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(WaterWallActivity.this, R.layout.layout_waterwall, null);
MyViewHoler myViewHoler = new MyViewHoler(view);
return myViewHoler;
}
@Override
public void onBindViewHolder(MyViewHoler holder, int position) {
//獲取佈局引數
ViewGroup.LayoutParams layoutParams = holder.iv_water_item.getLayoutParams();
//重新設定高度
layoutParams.height=heightList.get(position);
Log.i("AAAA-----","-------"+heightList.get(position));
//重新設定佈局引數
holder.iv_water_item.setLayoutParams(layoutParams);
holder.iv_water_item.setScaleType(ImageView.ScaleType.CENTER_CROP);
// holder.iv_water_item.set
// Glide.with(WaterWallActivity.this).load(Images.imageUrls[position]).into(holder.iv_water_item);
//載入網路圖片可以Gif格式
Glide.with(WaterWallActivity.this).load(Images.imageUrls[position]).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(holder.iv_water_item);
@Override
public int getItemCount() {
return Images.imageUrls.length;
}
}
}
//Glide 的工具類
//第二步
package com.example.recycleview_jinjie.utils;
import android.content.Context;
import android.os.Environment;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.GlideModule;
import java.io.File;
/**
* Created by xujiajian on 16/12/27.
*/
public class MyModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
int maxMemory = (int) Runtime.getRuntime().maxMemory();//獲取系統分配給應用的總記憶體大小
int memoryCacheSize = maxMemory / 8;//設定圖片記憶體快取佔用八分之一
//設定記憶體快取大小
builder.setMemoryCache(new LruResourceCache(memoryCacheSize));
File cacheDir = Environment.getExternalStorageDirectory();//指定的是資料的快取地址
int diskCacheSize = 1024 * 1024 * 30;//最多可以快取多少位元組的資料
//設定磁碟快取大小
builder.setDiskCache(new DiskLruCacheFactory(cacheDir.getPath(), "glide", diskCacheSize));
}
@Override
public void registerComponents(Context context, Glide glide) {
}
}
//配置需要
// 第三步
Build.gradle 新增許可權
compile 'com.android.support:recyclerview-v7:24.0.0-alpha2' compile 'com.github.bumptech.glide:glide:3.7.0'
// 第四步
//許可權
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>