android - Recyclerview drag and drop using ItemTouchHelper drops after one position -
i creating music player application. in creating playlists , in trying implement drag reorder functionality in recyclerview. first tried using itemtouchhelper
, third parties libraries of them facing same issue.
the issue start dragging item drops next item in list i.e swaps position next item no matter try drag it.
here adapter class recyclerview.
public class musicadapterrecyclerview extends recyclerview.adapter<musicadapterrecyclerview.viewholder>{ private list<music> songs; private context mcontext; public musicadapterrecyclerview( context mcontext, list<music> songs) { this.mcontext = mcontext; this.songs = songs; } @override public int getitemviewtype(int position) { return position; } @override public long getitemid(int position) { return position; } private context getcontext(){ return mcontext; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { context context = parent.getcontext(); layoutinflater inflater = layoutinflater.from(context); view musicview = inflater.inflate(r.layout.music_description, parent, false); viewholder viewholder = new viewholder(musicview); return viewholder; } @override public void onbindviewholder(viewholder holder, int position) { music song = songs.get(position); textview nametextview,artisttextview,timetextview; imageview albumartview; final imagebutton overflow; nametextview = holder.nametextview; artisttextview = holder.artisttextview; timetextview = holder.timetextview; albumartview = holder.albumartview; overflow = holder.overflow; nametextview.settext(song.getname()); artisttextview.settext(song.getartist()); /*float time = song.gettime(); decimalformat df = new decimalformat("#.##"); timetextview.settext(df.format(time)+"");*/ timetextview.settext(gettimestring(song.gettime())); // log.d("check",""+song.gettime()); try { picasso.with(this.getcontext()) .load(uri.parse(song.getalbumarturi())) .placeholder(r.drawable.default_album_art) .transform(new roundedtransformation(70,0)) .fit() .into(albumartview); } catch (exception e){ log.d("error", e.getmessage()); } } @override public int getitemcount() { if (songs != null) return songs.size(); else return 0; } public static class viewholder extends recyclerview.viewholder{ private textview nametextview; private textview artisttextview; private textview timetextview; private imageview albumartview; private imagebutton overflow; public viewholder(view itemview) { super(itemview); nametextview = (textview)itemview.findviewbyid(r.id.name); artisttextview = (textview)itemview.findviewbyid(r.id.artist); timetextview = (textview)itemview.findviewbyid(r.id.time); albumartview = (imageview)itemview.findviewbyid(r.id.albumart); } } private string gettimestring(long millis) { stringbuffer buf = new stringbuffer(); // int hours = (int) (millis / (1000 * 60 * 60)); int minutes = (int) ((millis % (1000 * 60 * 60)) / (1000 * 60)); int seconds = (int) (((millis % (1000 * 60 * 60)) % (1000 * 60)) / 1000); buf // .append(string.format("%02d", hours)) // .append(":") .append(string.format("%02d", minutes)) .append(":") .append(string.format("%02d", seconds)); return buf.tostring(); } public music removeitem(int position) { final music model = songs.remove(position); notifyitemremoved(position); return model; } public void additem(int position, music model) { songs.add(position, model); notifyiteminserted(position); } public void moveitem(int fromposition, int toposition) { final music model = songs.remove(fromposition); songs.add(toposition, model); notifyitemmoved(fromposition, toposition); } public void animateto(list<music> models) { applyandanimateremovals(models); applyandanimateadditions(models); applyandanimatemoveditems(models); } private void applyandanimateremovals(list<music> newmodels) { (int = songs.size() - 1; >= 0; i--) { final music model = songs.get(i); if (!newmodels.contains(model)) { removeitem(i); } } } private void applyandanimateadditions(list<music> newmodels) { (int = 0, count = newmodels.size(); < count; i++) { final music model = newmodels.get(i); if (!songs.contains(model)) { additem(i, model); } } } private void applyandanimatemoveditems(list<music> newmodels) { (int toposition = newmodels.size() - 1; toposition >= 0; toposition--) { final music model = newmodels.get(toposition); final int fromposition = songs.indexof(model); if (fromposition >= 0 && fromposition != toposition) { moveitem(fromposition, toposition); } } } public void remove(int position) { if (position < 0 || position >= songs.size()) { return; } songs.remove(position); notifydatasetchanged(); } public void swap(int firstposition, int secondposition){ collections.swap(songs, firstposition, secondposition); notifyitemmoved(firstposition, secondposition); } }
and following related activity code
public void setupitemtouchhelper(){ final itemtouchhelper.callback callback = new itemtouchhelper.callback() { @override public int getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) { adapterrecyclerview =(currentplaylistadapter) recyclerview.getadapter(); int dragflags = itemtouchhelper.up | itemtouchhelper.down; int swipeflags = itemtouchhelper.start | itemtouchhelper.end; return makemovementflags(dragflags, swipeflags); } @override public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { int fromposition = viewholder.getadapterposition(); int toposition = target.getadapterposition(); if (fromposition < toposition) { (int = fromposition; < toposition; i++) { collections.swap(mainlist, i, + 1); } } else { (int = fromposition; > toposition; i--) { collections.swap(mainlist, i, - 1); } } musicadapterrecyclerview.notifyitemmoved(fromposition, toposition); return true; } @override public void onswiped(recyclerview.viewholder viewholder, int direction) { final int pos = viewholder.getadapterposition(); if(pos<0 && pos>=mainlist.size()){ return; } mainlist.remove(pos); adapterrecyclerview.removeitem(pos); adapterrecyclerview.notifyitemremoved(pos); adapterrecyclerview.notifyitemrangechanged(pos,mainlist.size()); } @override public boolean islongpressdragenabled() { return true; } @override public boolean isitemviewswipeenabled() { return true; } }; itemtouchhelper itemtouchhelper = new itemtouchhelper(callback); // itemtouchhelper.attachtorecyclerview(playlistrecyclerview); }
callback method itemtouchhelper
final itemtouchhelper.callback callback = new itemtouchhelper.callback() { @override public int getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) { adapterrecyclerview =(currentplaylistadapter) recyclerview.getadapter(); int dragflags = itemtouchhelper.up | itemtouchhelper.down; int swipeflags = itemtouchhelper.start | itemtouchhelper.end; if(viewholder.getadapterposition()==playingindex) { swipeflags = 0; } return makemovementflags(dragflags, swipeflags); } @override public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { int fromposition = viewholder.getadapterposition(); int toposition = target.getadapterposition(); if (fromposition < toposition) { (int = fromposition; < toposition; i++) { collections.swap(mainlist, i, + 1); } } else { (int = fromposition; > toposition; i--) { collections.swap(mainlist, i, - 1); } } if(playingindex == fromposition) playingindex = toposition; adapterrecyclerview.notifyitemmoved(fromposition, toposition); // adapterrecyclerview.swap(fromposition,toposition); return true; } @override public void onswiped(recyclerview.viewholder viewholder, int direction) { final int pos = viewholder.getadapterposition(); if(pos<0 && pos>=mainlist.size()){ return; } if(pos == playingindex) return; mainlist.remove(pos); // adapterrecyclerview.removeitem(pos); adapterrecyclerview.notifyitemremoved(pos); adapterrecyclerview.notifyitemrangechanged(pos,mainlist.size()); } @override public boolean islongpressdragenabled() { return true; } @override public boolean isitemviewswipeenabled() { return true; } }; itemtouchhelper itemtouchhelper = new itemtouchhelper(callback);
edit:
i using umano sliding pane layout in same xml. can there case drag event event getting obstructed due umano slide event?
what problem here? or can suggest other working libraries helpful
Comments
Post a Comment