添加依賴
implementation 'com.github.bumptech.glide:glide:4.3.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
在RecyclerView中使用:
activity_recycle_view_image布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.pixel.myglide.RecycleViewImageActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="0dp"
android:layout_height="0dp"/>
</android.support.constraint.ConstraintLayout>
item_re布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/imge"
android:background="#000000"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="0dp"
android:layout_height="0dp" android:contentDescription="@string/tp" />
</android.support.constraint.ConstraintLayout>
adapter:
class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.ViewHolder>{
private List<String> list;
private Context context;
private LayoutInflater inflater;
public MyRecycleAdapter(Context context,List<String> list){
this.list=list;
this.context=context;
this.inflater=LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(inflater.inflate(R.layout.item_re,parent,false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(context)
.load(list.get(position))
.into(holder.imge);
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView imge;
public ViewHolder(View itemView) {
super(itemView);
imge=itemView.findViewById(R.id.imge);
}
}
}
在listview中使用:
activity_list_view_image布局:
<?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="com.example.pixel.myglide.ListViewImageActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
item布局
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/imge"
android:background="@mipmap/pd"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
adapter:
class MyImageAdapter extends BaseAdapter{
private List<String> list;
private LayoutInflater inflater;
private Context context;
public MyImageAdapter(Context context,List<String> list){
this.list=list;
this.inflater=LayoutInflater.from(context);
this.context=context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public String getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
if (convertView==null){
convertView=inflater.inflate(R.layout.item,parent,false);
viewHolder=new ViewHolder();
viewHolder.tp =convertView.findViewById(R.id.imge);
convertView.setTag(viewHolder);
} else{
viewHolder= (ViewHolder) convertView.getTag();
}
Log.i(TAG, "getView: url==="+list.get(position));
Glide.with(context)
.load(list.get(position))
.into(viewHolder.tp);
// viewHolder.tp.setBackgroundResource(R.mipmap.ic_launcher_round);
return convertView;
}
class ViewHolder{
ImageView tp;
}
}
在listview中會(huì)出現(xiàn)圖片不顯示問(wèn)題,解決方法:
http://www.reibang.com/p/f6b933dd8b98