android - trying to change background of imageButton in List item -
objective
i want play sound on click of list item imagebutton (initially hidden, visible on click) showing play pause (2 images) resource. imagebutton show state of sound user.
problem
when scroll list, imagebutton again gone.
code
baseadapter
private class listadapter extends baseadapter { private context mcontext; private layoutinflater minflater; viewholder holder; listadapter(context context) { mcontext = context; this.minflater = layoutinflater.from(mcontext); } @override public int getcount() { return indextitles.size(); } @override public object getitem(int position) { return indextitles.get(position); } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = minflater.inflate(r.layout.dua_row, null); holder.mainbg = (relativelayout) convertview.findviewbyid(r.id.duabg); holder.title_text = (textview) convertview.findviewbyid(r.id.title_text); holder.buttonplaypause = (imagebutton) convertview.findviewbyid(r.id.imagebutton); holder.buttonplaypause.setfocusable(false); holder.buttonplaypause.setfocusableintouchmode(false); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } string leftremoved = indextitles.get(position).trim(); holder.title_text.settext(leftremoved); // /player selection if (playpos == position) { holder.buttonplaypause.setbackgroundresource(r.drawable.buttonpause); holder.mainbg.setbackgroundcolor(color.parsecolor("#000000")); holder.title_text.settextcolor(color.parsecolor("#d59d52")); } else { holder.buttonplaypause.setvisibility(view.gone); holder.title_text.settextcolor(color.parsecolor("#000000")); holder.mainbg.setbackgroundcolor(color.parsecolor("#d59d52")); } return convertview; }
list row
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/activity_horizontal_margin" android:id="@+id/duabg" android:background="#d59d52"> <textview android:layout_width="match_parent" android:layout_height="32dp" android:text="" android:textsize="18dp" android:id="@+id/title_text" android:ellipsize="end" android:maxlines="1" android:layout_alignparenttop="true" android:layout_marginleft="@dimen/activity_horizontal_margin" android:layout_margintop="@dimen/activity_horizontal_margin" android:layout_marginbottom="@dimen/activity_horizontal_margin" android:layout_toleftof="@+id/imagebutton" android:layout_tostartof="@+id/imagebutton" /> <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imagebutton" android:focusable="false" android:visibility="gone" android:background="@drawable/buttonpause" android:layout_centervertical="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="5dp" android:background="#000000" android:layout_alignparenttop="true" android:layout_centerhorizontal="true"></linearlayout>
onitemclicklistener
// indextitles arraylist<string> indextitles = getindextitles(); final listadapter adapter = new listadapter(this); listview.setadapter(adapter); listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { final view v = view.findviewbyid(r.id.imagebutton); v.setvisibility(view.visible); if (mediaplayer.isplaying() && playpos == position) { // pause mediaplayer mediaplayer.pause(); media_paused = true; v.setbackgroundresource(r.drawable.buttonplay); } else if (media_paused && !mediaplayer.isplaying() && playpos == position) { media_paused = false; mediaplayer.start(); v.setbackgroundresource(r.drawable.buttonpause); } else if (!mediaplayer.isplaying() || mediaplayer.isplaying() && playpos != position) { // release , start position mediaplayer.reset(); v.setvisibility(view.visible); v.setbackgroundresource(r.drawable.buttonpause); string s = string.format("%03d", position + 2); string path = sdcard_path + "/doadata/dd_" + s + ".mp3"; playpos = position; adapter.notifydatasetchanged(); file file = new file(path); uri uri = uri.fromfile(file); try { mediaplayer.setdatasource(context, uri); mediaplayer.prepare(); mediaplayer.start(); mediaplayer.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { v.setbackgroundresource(r.drawable.buttonplay); } }); } catch (ioexception e) { e.printstacktrace(); } } } });
what needed want imagebutton keep state being played or paused.
thanks
simply set visibility inside if statement this:
if (playpos == position) { holder.buttonplaypause.setvisibility(view.visible); // further implementation } else { holder.buttonplaypause.setvisibility(view.gone); // further implementation }
the problem occured, because list adapter recycles old views. when 1 view not visible , list creating new view using old one, have set every different property again, else properties previous view object recycled/reused.
Comments
Post a Comment