图片增加、删除
利用RecyclerView实现图片新增、删除
删除** 新增
思路:
- 由于新增跟删除只是一个添加按钮跟CheckBox的区别,所以我们可以将两种情况分别用Adapter表示。
- 先定义删除的Adapter , 重写getItemViewType方法,返回该Adapter的标识。
- 定义新增的Adapter,继承删除的Adapter,其布局只有一个ImageView(添加按钮)。
- 然后通过是否编辑模式,决定设置那种Adapter。
下面给出部分代码
public class BaseHolder extends RecyclerView.ViewHolder {
public View rootView;
public BaseHolder(View itemView) {
super(itemView);
rootView = itemView;
}
}
class PicAdapter extends RecyclerView.Adapter<BaseHolder> {
private final int TYPE_ITEM = 0x00; //定义标识,标识是普通Item
@Override
public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pic_album_item_view, parent, false);
return new PicHolder(view);
}
@Override
public void onBindViewHolder(final BaseHolder holder, int position) {
holder.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里实现的是点击Item的处理逻辑
Functions.toast(getApplicationContext(), holder.getLayoutPosition() + "");
}
});
String image = images.get(position).getPicurl();
PicHolder picHolder = (PicHolder) holder;
picHolder.ivPicture.setImageResource(Functions.getResourceIdByString(image));
if (isEditMode) {
picHolder.cbSelectImg.setVisibility(View.VISIBLE);
} else {
picHolder.cbSelectImg.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return images.size(); //返回集合size
}
@Override
public int getItemViewType(int position) {
return TYPE_ITEM; //返回普通Item的类型
}
class PicHolder extends BaseHolder {
private ImageView ivPicture;
private View view;
private CheckBox cbSelectImg;
public PicHolder(View itemView) {
super(itemView);
view = itemView;
ivPicture = (ImageView) view.findViewById(R.id.ivPicture);
cbSelectImg = (CheckBox) view.findViewById(R.id.cbSelectImg);
}
}
}
class AddPicAdapter extends PicAdapter {
public final static int TYPE_ADD = 0x11; //定义添加Item的标识
@Override
public BaseHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
BaseHolder holder;
//通过viewType判断加载那种布局
if (viewType == TYPE_ADD) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pic_album_add_item_view, parent, false);
view.setOnClickListener(new View.OnClickListener() {
//点击添加按钮的处理逻辑
@Override
public void onClick(View v) {
MovieMsgInfo info = new MovieMsgInfo();
Random random = new Random();
int result = random.nextInt(11) + 1;
info.setPicurl(PREIFX_DRAWABLE + result);
images.add(info);
mAdapter.notifyDataSetChanged();
}
});
holder = new AddPicHolder(view);
return holder;
} else {
return super.onCreateViewHolder(parent, viewType);
}
}
@Override
public void onBindViewHolder(BaseHolder holder, int position) {
//通过判断是否点击最后一个Item
if (position == getItemCount() - 1) {
holder.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击最后添加按钮的处理逻辑
}
});
} else {
super.onBindViewHolder(holder, position);
}
}
@Override
public int getItemViewType(int position) {
//根据position判断加载类型
if (position == getItemCount() - 1) {
return TYPE_ADD;
}
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
//重中之重,返回Item数量为父类总数+1(+1是添加按钮)
return super.getItemCount() + 1;
}
public class AddPicHolder extends BaseHolder {
private ImageView ivPicture;
public AddPicHolder(View itemView) {
super(itemView);
ivPicture = (ImageView) itemView.findViewById(R.id.ivPicture);
}
}
}
HashSet控制集合选中以及取消
- 选中:点击的时候,将id加入到集合中
- 取消选择:判断是否包含该元素,是则删除,没有则添加。
onlick(){
if(set.contain(user.getID())){
set.remove(user.getID());
}else{
set.add(user.getId());
}
}