ImageUtil
package com.qizu.zhoukaocar; /** * Created by WangChang on 2016/4/3. */ public class ImageUtil { public final static String[] imageUrls = new String[]{ "https://img-my.csdn.net/uploads/201508/05/1438760758_3497.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760758_6667.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760757_3588.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760756_3304.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760755_6715.jpeg", "https://img-my.csdn.net/uploads/201508/05/1438760726_5120.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760726_8364.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760725_4031.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760724_9463.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760724_2371.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760707_4653.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760706_6864.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760706_9279.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760704_2341.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760704_5707.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760685_5091.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760685_4444.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760684_8827.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760683_3691.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760683_7315.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760663_7318.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760662_3454.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760662_5113.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760661_3305.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760661_7416.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760589_2946.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760589_1100.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760588_8297.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760587_2575.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760587_8906.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760550_2875.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760550_9517.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760549_7093.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760549_1352.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760548_2780.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760531_1776.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760531_1380.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760530_4944.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760530_5750.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760529_3289.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760500_7871.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760500_6063.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760499_6304.jpeg", "https://img-my.csdn.net/uploads/201508/05/1438760499_5081.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760498_7007.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760478_3128.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760478_6766.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760477_1358.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760477_3540.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760476_1240.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760446_7993.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760446_3641.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760445_3283.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760444_8623.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760444_6822.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760422_2224.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760421_2824.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760420_2660.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760420_7188.jpg", "https://img-my.csdn.net/uploads/201508/05/1438760419_4123.jpg", }; }
主方法
package com.qizu.zhoukaocar; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private List适配器mDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); } private void initData() { mDatas = new ArrayList (); for (int i = 0 ; i < ImageUtil.imageUrls.length ; i++){ String data = ImageUtil.imageUrls[i]; mDatas.add(data); } } private void initView() { recyclerView = (RecyclerView) findViewById(R.id.recyclerView); //添加布局管理 StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(manager); //设置适配器 recyclerView.setAdapter(new RecycleAdapter(this,mDatas)); //设置item添加和删除的动画 // recyclerView.setItemAnimator(); //设置分隔线 // recyclerView.addItemDecoration(new DividerGridItemDecoration(this)); } }
package com.qizu.zhoukaocar; import android.content.Context; import android.graphics.Bitmap; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import com.bumptech.glide.Glide; 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; /** * Created by jiuhao on 2017/11/18. * data 2017/11/18 * time 上午 10:30 */ public class RecycleAdapter extends RecyclerView.Adapter逐步局{ private List mDatas; private Context context; private LayoutInflater inflater; private SparseArray heightArray; public RecycleAdapter(Context context , List mDatas){ this.context = context; this.mDatas = mDatas; this.inflater = LayoutInflater.from(context); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { heightArray = new SparseArray (); View itemView = inflater.inflate(R.layout.item_recycle,parent,false); MyViewHolder viewHolder = new MyViewHolder(itemView); return viewHolder; } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { if (heightArray.get(position) == null){ Glide.with(context) .load(mDatas.get(position)) .asBitmap() .placeholder(R.mipmap.ic_launcher) .into(new SimpleTarget (Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) { @Override public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) { // Do something with bitmap here. int height = bitmap.getHeight(); //获取bitmap信息,可赋值给外部变量操作,也可在此时行操作。 bitmap.getWidth(); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.iv.getLayoutParams(); layoutParams.height = height; holder.iv.setLayoutParams(layoutParams); heightArray.put(position,height); } }); }else { int height = heightArray.get(position); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.iv.getLayoutParams(); layoutParams.height = height; holder.iv.setLayoutParams(layoutParams); } Glide.with(context) .load(mDatas.get(position)) .into(holder.iv); } @Override public int getItemCount() { return mDatas.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ ImageView iv; public MyViewHolder(View itemView) { super(itemView); iv = (ImageView) itemView.findViewById(R.id.ivImage); } } }
xml version="1.0" encoding="utf-8"?>item_recycle.xmlxmlns: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" tools:context="com.qizu.zhoukaocar.MainActivity"> android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent">
xml version="1.0" encoding="utf-8"?>dimensxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" xmlns:card_view="http://schemas.android.com/tools" android:layout_height="match_parent" android:background="#FFFFFF"> android:id="@+id/ivImage" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:padding="4dp"/>
依赖name="activity_horizontal_margin">16dp name="activity_vertical_margin">16dp
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' compile 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.jakewharton:butterknife:8.8.1' compile 'com.android.support:palette-v7:26.0.0-alpha1' compile 'com.android.support:cardview-v7:26.0.0-alpha1'