adapter 用自己定义的 image 讲解
package com.zsch.forestinventory.adapter;
import android.content.Context;
import android.net.Uri;
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.ImageButton;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.zsch.forestinventory.R;
import java.util.List;
/**
* Created by 11827 on 2017/8/7.
* 显示照片的适配器
*/
//ImageAdapter extends RecyclerView.Adapter 需要 重写3个方法下面有详解
public class ImageAdapter extends RecyclerView.Adapter implements View.OnClickListener, View.OnLongClickListener {
private OnItemLongClickListener onItemLongClickListener;
private OnItemClickListener onItemClickListener;
private OnItemMapClickListener onItemMapClickListener; //新建平面图的事件监听
private Context mContext;
private LayoutInflater inflater; //当XML布局资源被解析并转换成View对象时会用到。
private List<Uri> uris;
private int imageWidth;
private boolean isShowDeleteBtn = false;
public ImageAdapter(Context mContext, List<Uri> uris, int screenWidth, int cols) {
this.mContext = mContext;
inflater = LayoutInflater.from(mContext);
this.uris = uris;
imageWidth = screenWidth/cols;
imageWidth-=imageWidth/10;
}
@Override //RecyclerViewAdapter.class :继承RecyclerView.Adapter后,会重写三个方法:1.onCreateViewHolder2.onBindViewHolder3.getItemCount
//onCreateViewHolder():负责承载每个子项的布局。它有两个参数,其中一个是“ int viewType”。
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder holder = new ViewHolder(inflater.inflate(R.layout.item_image, null));
return holder; //代表所有控件
}
@Override //
//1.第一种点击事件写法 根据点击位置绑定数据 在 Adapter 里面直接对控件做点击事件
//负责将每个子项holder绑定数据
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewHolder mHolder = (ViewHolder) holder; //为的是listview滚动的时候快速设置值,而不必每次都重新创建很多对象,从而提升性能
mHolder.itemView.setTag(mHolder);//Android开发的view中已经有api接口可以完成一定量数据的存储 View.setTag()以及View.getTag() setTag()使用的最多的场景基本就是ListView,GridView,RecycleView
mHolder.itemView.setOnClickListener(this);
mHolder.itemView.setOnLongClickListener(this);
mHolder.ib.setTag(mHolder);
mHolder.ib.setOnClickListener(this); // holder.控件名.setOnClickListener(
if (isShowDeleteBtn) { //进行删除按钮的展示 或隐藏
mHolder.ib.setVisibility(View.VISIBLE); //View类的setVisibility()方法设置是否显示组件 View.VISIBLE:组件正常显示
} else {
mHolder.ib.setVisibility(View.INVISIBLE); //View.INVISIBLE:组件看不到,但占据着布局空间。
}
Glide.with(mContext)
.load(uris.get(position)) //资源的位置
.asBitmap() //只加载静态图片,如果是git图片则只加载第一帧。
.placeholder(R.mipmap.ic_place_holder)//加载带有占位图 占位图目的为在目的图片还未加载出来的时候,提前展示给用户的一张图片
.error(R.mipmap.ic_place_holder)//加载失败 放置占位符
//.asGif()//加载动态图片,若现有图片为非gif图片,则直接加载错误占位图。
.override(imageWidth*3, imageWidth*2) //指定图片大小 宽再宽点 长再长点 (width,height)
.centerCrop()
.into(mHolder.iv); //将数据加载到哪
}
public void setShowDeleteBtn(boolean isShowCheckBox) {
this.isShowDeleteBtn = isShowCheckBox;
}
public boolean isShowDeleteBtn() {
return isShowDeleteBtn;
}
@Override
//得到总条数
public int getItemCount() {
return uris.size();
}
@Override
public void onClick(View view) {
//第一次创建itemView的时候,完成对控件的绑定,同时吧控件作为一个object--holder,把它通过setTag()存到itemView中,
// 再第二次使用的时候就可以通过getTag() 把holder取出来直接使用
//list中itemView相同的情况下,我们只进行了一次的控件资源绑定
ViewHolder holder = (ViewHolder) view.getTag();
if (onItemClickListener != null) { //点击的是照片
switch (view.getId()) {
case R.id.imageItem: //点击放大
onItemClickListener.onItemClick(this, holder, holder.getAdapterPosition(), holder.getItemId());
break;
case R.id.ib: //删除
onItemClickListener.onItemButtonClick(this, holder, holder.getAdapterPosition(), holder.getItemId());
break;
}
}else if(onItemMapClickListener != null){ //点击的是平面图
switch (view.getId()) {
case R.id.imageItem: //点击放大
onItemMapClickListener.onItemMapClick(this, holder, holder.getAdapterPosition(), holder.getItemId());
break;
case R.id.ib: //删除
onItemMapClickListener.onItemButtonMapClick(this, holder, holder.getAdapterPosition(), holder.getItemId());
break;
}
}
}
@Override
public boolean onLongClick(View view) {
ViewHolder holder = (ViewHolder) view.getTag();
if (onItemLongClickListener != null) {
return onItemLongClickListener.onItemLongClick(this, holder, holder.getAdapterPosition(), holder.getItemId());
}
return false;
}
//自定义viewhodler 算是 自定义 内部类
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView iv;
public ImageButton ib;
public ViewHolder(View itemView) {
super(itemView);
iv = (ImageView) itemView.findViewById(R.id.iv);
ib = (ImageButton) itemView.findViewById(R.id.ib);
}
}
public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
this.onItemLongClickListener = onItemLongClickListener;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public void setOnItemMapClickListener(OnItemMapClickListener onItemMapClickListener) {
this.onItemMapClickListener = onItemMapClickListener;
}
//1.第二种点击事件写法 在 Adapter 里写接口
public interface OnItemLongClickListener {
boolean onItemLongClick(ImageAdapter adapter, ViewHolder holder, int position, long id);
}
public interface OnItemClickListener {
void onItemClick(ImageAdapter adapter, ViewHolder holder, int position, long id); //点击单张
void onItemButtonClick(ImageAdapter adapter, ViewHolder holder, int position, long id); ///删除照片
}
//新增平面图的点击 删除事件
public interface OnItemMapClickListener {
void onItemMapClick(ImageAdapter adapter, ViewHolder holder, int position, long id);//点击单张
void onItemButtonMapClick(ImageAdapter adapter, ViewHolder holder, int position, long id);///删除照片
}
}