public class SendImageAdapter extends RecyclerView.Adapter implements ItemTouchHelperAdapter{
private Activity mContext;
private List mDatas;
private LayoutInflater mLayoutInflater;
public SendImageAdapter(Activity context,List datas){
this.mContext = context;
this.mDatas = datas;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public SendImageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.send_grid_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final SendImageAdapter.ViewHolder viewHolder, final int position) {
Object image = mDatas.get(position);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) viewHolder.id_rl_img.getLayoutParams();
int w = params.width = (int) ((ScreenUtil.getScreenWidth(mContext) - DensityUtil.dp2px(mContext,38)) / 3.0);
params.height = w;
params.bottomMargin = DensityUtil.dp2px(mContext,7);
params.rightMargin = DensityUtil.dp2px(mContext,7);
viewHolder.id_rl_img.setLayoutParams(params);
if (image instanceof String) {
viewHolder.im_delete.setVisibility(View.VISIBLE);
viewHolder.iv_add.setVisibility(View.GONE);
viewHolder.im_img.setVisibility(View.VISIBLE);
//这里最好使用glide Picasso加载本地图片 bitmap容易造成内存溢出
Bitmap bitmap = BitmapFactory.decodeFile(image.toString());
viewHolder.im_img.setImageBitmap(bitmap);
LogUtil.e("手机照片", image.toString());
} else {
viewHolder.im_delete.setVisibility(View.INVISIBLE);
viewHolder.iv_add.setVisibility(View.VISIBLE);
viewHolder.im_img.setVisibility(View.GONE);
viewHolder.iv_add.setImageResource(R.drawable.tupiantianjia2x);
LogUtil.e("添加照片", ""+image);
}
viewHolder.iv_add.setOnClickListener(new MyClickListener(viewHolder));
viewHolder.im_img.setOnClickListener(new MyClickListener(viewHolder));
viewHolder.im_delete.setOnClickListener(new MyClickListener(viewHolder));
}
@Override
public int getItemCount() {
if(mDatas == null){
return 0;
}
return mDatas.size() >= 9 ? 9 : mDatas.size();
}
public List getDatas() {
return mDatas;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
private final ImageView im_img;
private final ImageView iv_add;
private final ImageView im_delete;
private final FrameLayout id_rl_img;
public ViewHolder(View itemView) {
super(itemView);
im_img = (ImageView) itemView.findViewById(R.id.iv_img);
iv_add = (ImageView) itemView.findViewById(R.id.iv_add);
im_delete = (ImageView) itemView.findViewById(R.id.im_delete);
id_rl_img = (FrameLayout) itemView.findViewById(R.id.id_rl_img);
}
}
//拖拽排序相关
@Override
public void onItemMove(int fromPos, int toPos) {
if(fromPos == mDatas.size()-1 || toPos == mDatas.size()-1 )
return;
Collections.swap(mDatas,fromPos,toPos);
notifyItemMoved(fromPos,toPos);
}
//滑动删除相关
@Override
public void onItemDel(int pos) {
if(pos == mDatas.size() - 1)
return;
mDatas.remove(pos);
notifyItemRemoved(pos);
}
public interface OnClickListener{
void onClick(View v, int position);
}
private static OnClickListener onClickListener;
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public static class MyClickListener implements View.OnClickListener{
private ViewHolder mHolder;
public MyClickListener(ViewHolder holder ){
this.mHolder = holder;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.iv_add:
case R.id.im_delete:
case R.id.iv_img:
if(onClickListener!=null) {
int pos = mHolder.getAdapterPosition();
onClickListener.onClick(view, pos);
}
break;
}
}
}
}
4. 拖拽排序
(1) RecycleView通过ItemTouchHelper 实现
MyItemTouchHelperCallback callBack = new MyItemTouchHelperCallback(mPhotoAdapter);
//实现拖拽排序
final ItemTouchHelper touchHelper = new ItemTouchHelper(callBack);
//调用ItemTouchHelper的attachToRecyclerView方法建立联系
touchHelper.attachToRecyclerView(mRecycleView);