android - How to use seek bar in each row of recyclerview -
i have situation need show seekbar in every row of recylerview download media(audio). file downlaoding everytime click downlaod button , last seekbar of recylerview work not seekbar associated particular row.
this adapter downloading audio
public class listentestadapter extends recyclerview.adapter<listentestadapter.myviewholder> implements adapterview.onitemclicklistener, view.onclicklistener, view.ontouchlistener, mediaplayer.oncompletionlistener, mediaplayer.onbufferingupdatelistener { private static final int download_thread_pool_size = 1; private final handler handler = new handler(); public textview testnametextview, downloadtextview, seekbartextview; mydownloaddownloadstatuslistenerv1 mydownloadstatuslistener = new mydownloaddownloadstatuslistenerv1(); int downloadid1; private list<listen> list = new arraylist<>(); private activity context; private mediaplayer mediaplayer; private seekbar seekbarprogress; private int mediafilelengthinmilliseconds; private string mp3url; private handler durationhandler = new handler(); private textview duration; private int timeelapsed; private thindownloadmanager downloadmanager; private string filename; private retrypolicy retrypolicy ; private file filesdir ; private uri downloaduri ; private seekbar seekbar ; //handler change seekbartime private runnable updateseekbartime = new runnable() { public void run() { //get current position timeelapsed = mediaplayer.getcurrentposition(); //set seekbar progress //seekbar.setprogress((int) timeelapsed); //set time remaing double timeremaining = mediafilelengthinmilliseconds - timeelapsed; duration = (textview) context.findviewbyid(r.id.duration); //222067.0 duration.settext(string.format("%d min, %d sec", timeunit.milliseconds.tominutes((long) timeremaining), timeunit.milliseconds.toseconds((long) timeremaining) - timeunit.minutes.toseconds(timeunit.milliseconds.tominutes((long) timeremaining)))); //repeat again in 100 miliseconds durationhandler.postdelayed(this, 100); } }; public listentestadapter(activity context, list<listen> list) { this.list = list; this.context = context; } @override public void onitemclick(adapterview<?> adapterview, view view, int position, long l) { toast.maketext(context, "you clciked " + position, toast.length_short).show(); } @override public void onbufferingupdate(mediaplayer mp, int percent) { // buttonplaypause.setimageresource(r.drawable.button_pause); durationhandler.postdelayed(updateseekbartime, 100); mediafilelengthinmilliseconds = mediaplayer.getduration(); primaryseekbarprogressupdater(); } @override public void onclick(view v) { } @override public void oncompletion(mediaplayer mp) { context.findviewbyid(r.id.mediaplayerlayout).setvisibility(view.gone); } @override public boolean ontouch(view v, motionevent event) { return false; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { // create new view view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.listview_listen_item, parent, false); // set view's size, margins, paddings , layout parameters myviewholder vh = new myviewholder(v); return vh; } @override public void onbindviewholder(final myviewholder holder, final int position) { testnametextview.settext(list.get(position).getpassages()); seekbar.setmax(100); seekbar.setprogress(0); downloadmanager = new thindownloadmanager(download_thread_pool_size); retrypolicy = new defaultretrypolicy(); filesdir = context.getexternalfilesdir(environment.directory_music); downloadtextview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { mp3url = list.get(position).getquestiongroupfile(); mp3url = mp3url.replace(" ", "%20"); if(!"".equals(mp3url)){ downloaduri = uri.parse(mp3url); filename = mp3url.substring(mp3url.lastindexof('/') + 1); //download audio downloadaudio(); }else{ toast.maketext(context, "audio not avialable", toast.length_short).show(); } } }); } private void downloadaudio() { uri destinationuri = uri.parse(filesdir + "/" + filename); final downloadrequest downloadrequest = new downloadrequest(downloaduri) .setdestinationuri(destinationuri).setpriority(downloadrequest.priority.low) .setretrypolicy(retrypolicy) .setdownloadcontext("download1") .setstatuslistener(mydownloadstatuslistener); if (downloadmanager.query(downloadid1) == downloadmanager.status_not_found) { filename = filename.replace("%20", " "); downloadid1 = downloadmanager.add(downloadrequest); } else { toast.maketext(context, "please wait....downloading in progress", toast.length_short).show(); } } /** * method updates seekbar primary progress current song playing position */ private void primaryseekbarprogressupdater() { seekbarprogress.setprogress((int) (((float) mediaplayer.getcurrentposition() / mediafilelengthinmilliseconds) * 100)); // math construction give percentage of "was playing"/"song length" if (mediaplayer.isplaying()) { runnable notification = new runnable() { public void run() { primaryseekbarprogressupdater(); } }; handler.postdelayed(notification, 1000); } } @override public int getitemcount() { return list.size(); } private string getbytesdownloaded(int progress, long totalbytes) { //greater 1 mb long bytescompleted = (progress * totalbytes) / 100; if (totalbytes >= 1000000) { return ("" + (string.format("%.1f", (float) bytescompleted / 1000000)) + "/" + (string.format("%.1f", (float) totalbytes / 1000000)) + "mb"); } if (totalbytes >= 1000) { return ("" + (string.format("%.1f", (float) bytescompleted / 1000)) + "/" + (string.format("%.1f", (float) totalbytes / 1000)) + "kb"); } else { return ("" + bytescompleted + "/" + totalbytes); } } public class myviewholder extends recyclerview.viewholder { public myviewholder(view view) { super(view); testnametextview = (textview) view.findviewbyid(r.id.testnametextview); downloadtextview = (textview) view.findviewbyid(r.id.downloadtextview); seekbartextview = (textview) context.findviewbyid(r.id.duration); seekbar = (seekbar) view.findviewbyid(r.id.seekbar); } } class mydownloaddownloadstatuslistenerv1 implements downloadstatuslistenerv1 { @override public void ondownloadcomplete(downloadrequest request) { final int id = request.getdownloadid(); if (id == downloadid1) { seekbartextview.settext("downaloaded" + " audio: " + filename + " completed"); //when download complete encryption start encryptdownloadedaudio.encrypt(context,mp3url,filename); } } @override public void ondownloadfailed(downloadrequest request, final int errorcode, final string errormessage) { final int id = request.getdownloadid(); if (id == downloadid1) { context.runonuithread(new runnable() { @override public void run() { seekbartextview.settext("failed download: " + filename + " failed: errorcode " + errorcode + ", " + errormessage); seekbar.setprogress(0); } }); } } @override public void onprogress(downloadrequest request, long totalbytes, long downloadedbytes, int progress) { int id = request.getdownloadid(); system.out.println("######## onprogress ###### " + id + " : " + totalbytes + " : " + downloadedbytes + " : " + progress); if (id == downloadid1) { seekbartextview.settext("downloading: " + filename + ", " + progress + "%" + " " + getbytesdownloaded(progress, totalbytes)); seekbar.setprogress(progress); } } } }
check concept of view tags, out. seekbar.settag("unique id every view"); while updating seekbar use findviewwithtag("unique id particular view")
since fetching view id give last view id.
Comments
Post a Comment