Android 仿微信发动态九宫格拖拽、删除功能
程序员文章站
2023-10-31 08:33:34
1、完美1比1 仿照微信仿微信发动态 九宫格拖拽、删除
暴力拖拽ui有点问题,不影响使用,资源文件自己找个+号
2、微信发动态拖拽bug
当选择完图片,长按图片拖拽过程中按下屏幕...
1、完美1比1 仿照微信仿微信发动态 九宫格拖拽、删除
暴力拖拽ui有点问题,不影响使用,资源文件自己找个+号
2、微信发动态拖拽bug
当选择完图片,长按图片拖拽过程中按下屏幕home键盘,再次进入这时候就不能点击输入文字,点击输入文字的时候会触发选择相册事件
3、拖拽事件用的basequickadapter
implementation '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.java package 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, "translationy", 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仿微信视屏悬浮窗效果
下一篇: 白菜猪肉炖粉条,这样做好吃到你舔盘子