GridView加载本地图片很easy,加载网络图片咋各种问题呢?什么主线程访问网络异常啦等等。
那是没有结合网络框架的使用,比如Glide
1moudle的gardle先依赖glide
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
2不忘记联网,读写权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
3
主类
package com.blzt.test.mygridview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.blzt.test.R;
import java.util.ArrayList;
import java.util.HashMap;
public class GlideGridViewActivity extends Activity {
private GridView mCircleBabyGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glide_grid_view);
mCircleBabyGridView = (GridView) findViewById(R.id.circle_baby_grid_view);
//下面对GridView进行加载
//下面的list是保存我们的图片组URL的
ArrayList<HashMap<String,String>> list=new ArrayList<>();
for(int i=0;i<100;i++)
{
//单个的Map保存我们的URL
HashMap<String,String> map=new HashMap<>();
map.put("ItemImage","http://192.168.2.161:8080/huashangservice/skuimg/3.png");
list.add(map);
}
//下面是SimpleAdapter的构建过程(参数依次是:1:上下文,2:数据源,3:子项布局,4:我们的子项布局的键值对的键 5:子项布局中键值对的值)
SimpleAdapter adapter = new SimpleAdapter(GlideGridViewActivity.this,list,R.layout.circle_grid_view,new String[]{"ItemImage"},new int[]{R.id.ItemImage});
//判断是否为我们要处理的对象,在这里adapter自动为子项赋值
adapter.setViewBinder(new SimpleAdapter.ViewBinder(){
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if(view instanceof ImageView){
ImageView iv = (ImageView)view;
// Glide.with(GlideGridViewActivity.this)
// .load("http://192.168.2.161:8080/huashangservice/skuimg/2.png")
// .into(iv);
GlideUtil.GlideWithPlaceHolder(GlideGridViewActivity.this, data.toString()).into(iv);
//给子项布局加个点击事件
iv.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(GlideGridViewActivity.this, "image"+1, Toast.LENGTH_SHORT).show();
}
});
return true;
}else
return false;
}
});
mCircleBabyGridView.setAdapter(adapter);
}
}
activity_glide_grid_view.xml
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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=".mygridview.GlideGridViewActivity">
<GridView
android:id="@+id/circle_baby_grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="@color/white" />
LinearLayout>
circle_grid_view.xml
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/ItemImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"/>
LinearLayout>
GlideUtil.java
public class GlideUtil {
public static RequestBuilder<Drawable> GlideWithPlaceHolder(Context context, Object object) {
return Glide.with(context).load(object).apply(new RequestOptions().placeholder(R.drawable.camera).dontAnimate());
}
}