暴力拖拽ui有点问题,不影响使用,资源文件自己找个+号
2、微信发动态拖拽BUG当选择完图片,长按图片拖拽过程中按下屏幕home键盘,再次进入这时候就不能点击输入文字,点击输入文字的时候会触发选择相册事件
3、拖拽事件用的basequickadapterimplementation 'com.androID.support:recyclervIEw-v7:28.0.0' implementation "com.github.CymChad:BaseRecyclerVIEwAdapterHelper:2.9.50" implementation "com.github.bumptech.glIDe:glIDe:4.9.0"4、演示
5、快速入手
activity NineGrIDChooseImage nineGrIDChooseImage = findVIEwByID(R.ID.nineGrIDChooseImage); FrameLayout frameLayout= findVIEwByID(R.ID.frameLayout); nineGrIDChooseImage.init(frameLayout); int statusbarHeight = 0; int resourceID = getResources().getIDentifIEr("status_bar_height","dimen","androID"); if (resourceID > 0) { statusbarHeight = getResources().getDimensionPixelSize(resourceID); } nineGrIDChooseImage.setFixH(statusbarHeight);//自己对一下高度 nineGrIDChooseImage.setAddClick(new NineGrIDChooseImage.AddClick() { @OverrIDe public voID onAdd() { /* List<String> List = new linkedList<>(); for(int i = 0;i<5;i++){ List.add("https://xx.jpg"); }*/ nineGrIDChooseImage.addData("https://xxx.jpg"); } });
xml
<?xml version="1.0" enCoding="utf-8"?><FrameLayout 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:ID="@+ID/frameLayout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".Main11Activity"> <com.example.hotfix_01.vIEw.NineGrIDChooseImage androID:background="#A8A1A1" androID:layout_margintop="100dp" androID:ID="@+ID/nineGrIDChooseImage" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /></FrameLayout>5、实现过程
如果帮助到你请关注点赞一下
NineGrIDChooseImage.javapackage com.example.hotfix_01.vIEw;import androID.animation.Animator;import androID.animation.AnimatorListenerAdapter;import androID.animation.ObjectAnimator;import androID.app.Activity;import androID.content.Context;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Point;import androID.graphics.Rect;import androID.support.annotation.NonNull;import androID.support.annotation.Nullable;import androID.support.v7.Widget.GrIDLayoutManager;import androID.support.v7.Widget.RecyclerVIEw;import androID.support.v7.Widget.helper.itemtouchhelper;import androID.text.TextUtils;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.display;import androID.vIEw.Gravity;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.FrameLayout;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.TextVIEw;import com.bumptech.glIDe.GlIDe;import com.chad.library.adapter.base.BaseItemDraggableAdapter;import com.chad.library.adapter.base.BaseVIEwHolder;import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;import com.chad.library.adapter.base.Listener.OnItemDragListener;import java.util.linkedList;import java.util.List;import top.antaikeji.base.R;import top.antaikeji.foundation.utils.displayUtil;import static androID.support.v7.Widget.helper.itemtouchhelper.ACTION_STATE_IDLE;/** * Author: flyzhang */public class NineGrIDChooseImage extends RecyclerVIEw { private Rect lastRect = new Rect(); private Rect rect = new Rect(); private boolean isDel = false,tempImageDel = false; private int mSelectposition = 0; private int SCREEN_W,SCREEN_H; private int mImageWH; private int mImageCount = 4; private DraggableAdapter mDraggableAdapter; private linearLayout mBottomVIEw; private FrameLayout mFrameLayout; private AddClick mAddClick; private DragCallBack mDragCallBack; private ImageVIEw mTempImage; ; private int mFixH = 0; private int bottomVIEwH = 55; private ImageVIEw trash; private int margin = 2; public NineGrIDChooseImage(@NonNull Context context) { super(context); } public NineGrIDChooseImage(@NonNull Context context,@Nullable AttributeSet attrs) { super(context,attrs); } public voID init(FrameLayout f) { this.mFrameLayout = f; display defaultdisplay = ((Activity) getContext()).getwindowManager().getDefaultdisplay(); Point point = new Point(); defaultdisplay.getSize(point); SCREEN_H = point.y; SCREEN_W = point.x; mImageWH = SCREEN_W / 3 - 2 * displayUtil.dptopx(5) - 4 * displayUtil.pxToDp(20) - displayUtil.pxToDp(50); bottomVIEwH = displayUtil.dptopx(bottomVIEwH); margin = displayUtil.dptopx(margin); mTempImage = new ImageVIEw(getContext()); mTempImage.setScaleType(ImageVIEw.ScaleType.FIT_XY); //初始化底部vIEw mBottomVIEw = new linearLayout(getContext()); mBottomVIEw.setGravity(Gravity.CENTER); mBottomVIEw.setBackgroundcolor(color.RED); FrameLayout.LayoutParams bottomLayoutParams = new FrameLayout.LayoutParams(VIEwGroup.LayoutParams.MATCH_PARENT,bottomVIEwH); bottomLayoutParams.gravity = Gravity.BottOM; trash = new ImageVIEw(getContext()); int wh = displayUtil.dptopx(25); FrameLayout.LayoutParams trashParams = new FrameLayout.LayoutParams(wh,wh); trash.setLayoutParams(trashParams); mBottomVIEw.addVIEw(trash); TextVIEw tipTextVIEw = new TextVIEw(getContext()); tipTextVIEw.setTextcolor(color.WHITE); tipTextVIEw.setText("拖拽此处删除"); mBottomVIEw.addVIEw(tipTextVIEw); mBottomVIEw.setorIEntation(linearLayout.VERTICAL); mBottomVIEw.setLayoutParams(bottomLayoutParams); mBottomVIEw.setVisibility(GONE); out(); mFrameLayout.addVIEw(mBottomVIEw); GrIDLayoutManager grIDLayoutManager = new GrIDLayoutManager(getContext(),3); setLayoutManager(grIDLayoutManager); List<String> data = new linkedList<>(); data.add(""); mDraggableAdapter = new DraggableAdapter(data); DragAndSwipeCallback mItemDragAndSwipeCallback = new DragAndSwipeCallback(mDraggableAdapter); itemtouchhelper itemtouchhelper = new itemtouchhelper(mItemDragAndSwipeCallback); itemtouchhelper.attachToRecyclerVIEw(this); mDraggableAdapter.enableDragItem(itemtouchhelper,R.ID.image,true); mItemDragAndSwipeCallback.setSwipeMoveFlags(itemtouchhelper.START | itemtouchhelper.END); mDraggableAdapter.setonItemDragListener(new OnItemDragListener() { @OverrIDe public voID onItemDragStart(RecyclerVIEw.VIEwHolder vIEwHolder,int pos) { if(null!=mDragCallBack){ mDragCallBack.onStart(); } mSelectposition = pos; mFrameLayout.removeVIEw(mTempImage); tempImageDel = false; isDel = false; in(); ImageVIEw imageVIEw = vIEwHolder.itemVIEw.findVIEwByID(R.ID.image); mTempImage.setimageDrawable(imageVIEw.getDrawable()); VIEwGroup.LayoutParams l = new VIEwGroup.LayoutParams(imageVIEw.getWIDth(),imageVIEw.getHeight()); mTempImage.setLayoutParams(l); imageVIEw.getGlobalVisibleRect(lastRect); mTempImage.setX(lastRect.left); mTempImage.setY(lastRect.top - mFixH); mTempImage.setScaleX(1.2f); mTempImage.setScaleY(1.2f); mFrameLayout.addVIEw(mTempImage); imageVIEw.setAlpha(0f); } @OverrIDe public voID onItemDragMoving(RecyclerVIEw.VIEwHolder source,int from,RecyclerVIEw.VIEwHolder target,int to) { target.itemVIEw.findVIEwByID(R.ID.image).getGlobalVisibleRect(lastRect); mSelectposition = to; } @OverrIDe public voID onItemDragEnd(RecyclerVIEw.VIEwHolder vIEwHolder,int pos) { if(null!=mDragCallBack){ mDragCallBack.onEnd(); } ImageVIEw imageVIEw = vIEwHolder.itemVIEw.findVIEwByID(R.ID.image); imageVIEw.setScaleX(1f); imageVIEw.setScaleY(1f); if (tempImageDel) { mDraggableAdapter.remove(mSelectposition); } else { imageVIEw.setAlpha(1f); mFrameLayout.removeVIEw(mTempImage); } out(); } }); setAdapter(mDraggableAdapter); } public voID setFixH(int mFixH) { this.mFixH = mFixH; } /** * 拖住adapter */ private class DraggableAdapter extends BaseItemDraggableAdapter<String,BaseVIEwHolder> { private DraggableAdapter(List<String> data) { super(R.layout.base_iamge_item,data); } @OverrIDe protected voID convert(@NonNull BaseVIEwHolder helper,String item) { ImageVIEw imageVIEw = helper.getVIEw(R.ID.image);//图片 linearLayout.LayoutParams l = (linearLayout.LayoutParams) imageVIEw.getLayoutParams(); l.height = mImageWH; l.wIDth = mImageWH; l.rightmargin = margin; l.bottommargin = margin; l.topmargin = margin; imageVIEw.setLayoutParams(l); ImageVIEw imageVIEw2 = helper.getVIEw(R.ID.image2);//加号 linearLayout.LayoutParams l2 = (linearLayout.LayoutParams) imageVIEw.getLayoutParams(); l2.height = mImageWH; l2.wIDth = mImageWH; l2.rightmargin = margin; l2.bottommargin = margin; l2.topmargin = margin; imageVIEw2.setLayoutParams(l2); /** * 如果List的长度等于mImageCount 最后一张最后一张显示正常图片 * */ if(TextUtils.isEmpty(item)){ if(mImageCount <= helper.getAdapterposition()){ imageVIEw.setVisibility(VIEw.GONE); imageVIEw2.setVisibility(VIEw.GONE); }else{ imageVIEw.setVisibility(VIEw.GONE); imageVIEw2.setVisibility(VIEw.VISIBLE); imageVIEw2.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (null != mAddClick) { mAddClick.onAdd(); } } }); } }else{ imageVIEw.setVisibility(VIEw.VISIBLE); imageVIEw2.setVisibility(VIEw.GONE); GlIDe.with(mContext).load(item).into(imageVIEw); } imageVIEw.setAlpha(1f); } } /** * 拖拽监听 */ private class DragAndSwipeCallback extends ItemDragAndSwipeCallback { private DragAndSwipeCallback(BaseItemDraggableAdapter adapter) { super(adapter); } @OverrIDe public boolean canDropOver(@NonNull RecyclerVIEw recyclerVIEw,@NonNull RecyclerVIEw.VIEwHolder current,@NonNull RecyclerVIEw.VIEwHolder target) { if (target.getAdapterposition() == mDraggableAdapter.getData().size() - 1 || current.getAdapterposition() == mDraggableAdapter.getData().size() - 1) { return false; } return super.canDropOver(recyclerVIEw,current,target); } @OverrIDe public voID onSelectedChanged(RecyclerVIEw.VIEwHolder vIEwHolder,int actionState) { super.onSelectedChanged(vIEwHolder,actionState); if (actionState == ACTION_STATE_IDLE) { //空闲状态 if (isDel) { isDel = false; tempImageDel = true; mFrameLayout.removeVIEw(mTempImage); } } } @OverrIDe public voID onChildDrawOver(@NonNull Canvas c,@NonNull RecyclerVIEw recyclerVIEw,@NonNull RecyclerVIEw.VIEwHolder vIEwHolder,float dX,float dY,int actionState,boolean isCurrentlyActive) { super.onChildDrawOver(c,recyclerVIEw,vIEwHolder,dX,dY,actionState,isCurrentlyActive); if (tempImageDel) { return; } ImageVIEw tempImageVIEw = vIEwHolder.itemVIEw.findVIEwByID(R.ID.image); boolean b = tempImageVIEw.getGlobalVisibleRect(rect); Log.e("TAG","dX:" + dX); Log.e("TAG","dY:" + dY); if (b && dX > 0 && dY > 0) { mTempImage.setX(rect.left); mTempImage.setY(rect.top - mFixH); } else { mTempImage.setX(lastRect.left + dX); mTempImage.setY(lastRect.top - mFixH + dY); } mTempImage.getGlobalVisibleRect(rect); if (SCREEN_H - bottomVIEwH < rect.bottom) { isDel = true; trash.setBackgroundResource(R.drawable.base_trash_open); } else { isDel = false; trash.setBackgroundResource(R.drawable.base_trash_close); } } } public voID addData(String path) { mDraggableAdapter.addData(0,path); mDraggableAdapter.notifyItemChanged(mDraggableAdapter.getData().size()-1); } public voID addData(List<String> path){ int size = mDraggableAdapter.getData().size(); int offset = mImageCount - size + 1; if(offset > path.size()){ offset = path.size(); } mDraggableAdapter.addData(0,path.subList(0,offset)); mDraggableAdapter.notifyItemChanged(mDraggableAdapter.getData().size() -1); } public voID setimageCount(int mImageCount) { this.mImageCount = mImageCount; } public voID setAddClick(AddClick mAddClick) { this.mAddClick = mAddClick; } public voID setDragCallBack(DragCallBack mDragCallBack) { this.mDragCallBack = mDragCallBack; } /** * 获取列表 * * @return */ public List<String> getData() { List<String> res = new linkedList<>(); for (int i = 0; i < mDraggableAdapter.getData().size() - 1; i++) { res.add(mDraggableAdapter.getData().get(i)); } return res; } /** * 底部进入 */ private voID in() { mBottomVIEw.setVisibility(VIEw.VISIBLE); ObjectAnimator animator = ObjectAnimator.offloat(mBottomVIEw,"translationY",0) .setDuration(200); animator.start(); } /** * 底部移除屏幕外部 */ private voID out() { final int moveX = displayUtil.dptopx(55); ObjectAnimator animator = ObjectAnimator.offloat(mBottomVIEw,moveX) .setDuration(200); animator.addListener(new AnimatorListenerAdapter() { @OverrIDe public voID onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); mBottomVIEw.setVisibility(VIEw.GONE); trash.setBackgroundResource(R.drawable.base_trash_close); } }); animator.start(); } public interface AddClick { voID onAdd(); } public interface DragCallBack{ voID onStart(); voID onEnd(); }}
iamge_item.xml
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/l" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" > <ImageVIEw androID:scaleType="fitXY" androID:ID="@+ID/image" androID:layout_wIDth="149dp" androID:layout_height="149dp" /> <ImageVIEw androID:ID="@+ID/image2" androID:visibility="gone" androID:layout_wIDth="149dp" androID:layout_height="149dp" /></linearLayout>
总结
以上所述是小编给大家介绍的AndroID 仿微信发动态九宫格拖拽、删除功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
以上是内存溢出为你收集整理的Android 仿微信发动态九宫格拖拽、删除功能全部内容,希望文章能够帮你解决Android 仿微信发动态九宫格拖拽、删除功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)