java - how to get the right Element from recyclerView -
we developing android application we're walking problem.
we have recyclerview filled cardview's based on database results. inside cardview have button.
now try create onclick effect on button (this works).
but when onclick button in cardview want code return specefic id cardview.
we tried use
getparent()
but returns same cardview if press button on cardview. possible without creating onclick on recyclerview itself?
we want unique id of cardview
edit:
we want button on card extract information on card
fragment:
package info.androidhive.materialnavbar.fragment; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmenttransaction; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import java.util.arraylist; import java.util.list; import java.util.map; import info.androidhive.materialnavbar.activity.mainactivity; import info.androidhive.materialnavbar.carditem; import info.androidhive.materialnavbar.r; import info.androidhive.materialnavbar.viewadapters.rvadapter; /** * created bart on 10-6-2015 * 2015 - 06 * superclass information fragments * class handles inflation of recyclerview , xml files */ public class informationfragment extends fragment { private recyclerview recyclerview; private rvadapter mrecyclerviewadapter; private view containerview; private static list<carditem> cardentry; private map<integer, string> mfragmenttags; public static boolean refresh = false; //haalt list leeg public void refreshfragment(){ // zet fragment op null fragment frg = null; frg = getfragmentmanager().findfragmentbyid(2131296339); final fragmenttransaction ft = getfragmentmanager().begintransaction(); ft.detach(frg); ft.attach(frg); ft.commit(); } // recycler list public list<carditem> getcarddata(int type) { cardentry = new arraylist<>(); // tijdelijk knop voor testing int reporticon = r.drawable.reporttemp; switch (type) { case 0: // vull kaartjes met volgende items : cardentry.add(new carditem("today", "string a", r.drawable.ic_facts,reporticon)); cardentry.add(new carditem("today", "string a", r.drawable.ic_facts,reporticon)); cardentry.add(new carditem("today", "string a", r.drawable.ic_facts,reporticon)); cardentry.add(new carditem("today", "string a", r.drawable.ic_facts,reporticon)); cardentry.add(new carditem("today", "string a", r.drawable.ic_facts,reporticon)); cardentry.add(new carditem("today", "string a", r.drawable.ic_facts,reporticon)); //workaround //zet case type om naar cardcounter > inflater > refresh mainactivity.cardcounter = 0; if (!refresh){ refreshfragment(); refresh = true; } break; case 1: // v title vcontent v img cardentry.add(new carditem("facts", "string a", r.drawable.ic_birthdays,reporticon)); //workaround mainactivity.cardcounter = 1; if (!refresh){ refreshfragment(); refresh = true; } break; case 2: cardentry.add(new carditem("history", "string a", r.drawable.ic_history,reporticon)); //workaround mainactivity.cardcounter = 2; if (!refresh){ refreshfragment(); refresh = true; } break; case 3: cardentry.add(new carditem("birthday", "string a", r.drawable.ic_lifehacks,reporticon)); //workaround mainactivity.cardcounter = 3; if (!refresh){ refreshfragment(); refresh = true; } break; case 4: cardentry.add(new carditem("lifehacks", "string a", r.drawable.ic_facts,reporticon)); //workaround mainactivity.cardcounter = 4; if (!refresh){ refreshfragment(); refresh = true; } break; case 5: cardentry.add(new carditem("quotes", "string a", r.drawable.ic_facts,reporticon)); //workaround mainactivity.cardcounter = 5; if (!refresh){ refreshfragment(); refresh = true; } break; case 6: cardentry.add(new carditem("favorites", "string a", r.drawable.ic_facts,reporticon)); //workaround mainactivity.cardcounter = 6; if (!refresh){ refreshfragment(); refresh = true; } break; } return cardentry; } public list<carditem> getcardentry() { return cardentry; } // end recycler @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { // inflate layout fragment * iets aangepast, return onderaan voor recycler drawer view layout = inflater.inflate(r.layout.fragment_content, container, false); // pak het id van de recycler uit xml recyclerview = (recyclerview) layout.findviewbyid(r.id.recyclercardlist); // haal de data op uit de return/switch hier boven // mrecyclerviewadapter = new rvadapter(getcarddata(mainactivity.cardcounter)); // recyclerview.setadapter(mrecyclerviewadapter); // zet hem als dit op deze activity recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); return layout; } }
adapter:
package info.androidhive.materialnavbar.viewadapters; import android.content.context; import android.support.v7.widget.cardview; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.imagebutton; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import java.util.list; import info.androidhive.materialnavbar.activity.downloadimagetask; import info.androidhive.materialnavbar.animations; import info.androidhive.materialnavbar.carditem; import info.androidhive.materialnavbar.r; public class rvadapter extends recyclerview.adapter<rvadapter.personviewholder> { // recycler view adapter public static class personviewholder extends recyclerview.viewholder { cardview cv; textview personname; textview personage; imageview personphoto; imagebutton favbut; imagebutton reportbut; personviewholder(view itemview) { super(itemview); cv = (cardview) itemview.findviewbyid(r.id.cv); personname = (textview) itemview.findviewbyid(r.id.person_name); personage = (textview) itemview.findviewbyid(r.id.person_age); personphoto = (imageview) itemview.findviewbyid(r.id.person_photo); favbut = (imagebutton) itemview.findviewbyid(r.id.favbut); reportbut = (imagebutton) itemview.findviewbyid(r.id.reportbut); } } list<carditem> carditems; public rvadapter(list<carditem> carditems) { this.carditems = carditems; } public object getcard(int position) { return carditems.get(position); } public list<carditem> getcarditems() { return carditems; } @override public void onattachedtorecyclerview(recyclerview recyclerview) { super.onattachedtorecyclerview(recyclerview); } @override public personviewholder oncreateviewholder(viewgroup viewgroup, int i) { view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.carditem, viewgroup, false); personviewholder pvh = new personviewholder(v); return pvh; } // de 'oncreate' @override public void onbindviewholder(personviewholder personviewholder, int i) { // vult het object/element met personviewholder.personname.settext(carditems.get(i).name); personviewholder.personage.settext(carditems.get(i).age); personviewholder.personphoto.setimageresource(carditems.get(i).photoid); //fav personviewholder.favbut.setimageresource(carditems.get(i).favid); //report personviewholder.reportbut.setimageresource(carditems.get(i).reportid); //zet animation op dit object/element animations.animatescatter(personviewholder, true); } @override public int getitemcount() { return carditems.size(); } }
create interface this
public static interface itemclicklistener<t>{ void onitemclick(t itemdata); }
create feild in adapter , initialize in constructor this
itemclicklistener<carditem> mitemclicklistener; public clientsadapter(itemclicklistener<carditem> itemclicklistener) { mitemclicklistener = itemclicklistener; }
and in bind
@override public void onbindviewholder(personviewholder personviewholder, int i) { final carditem item = mcarditems.get(i); personviewholder.favbut.setonclicklistener(new onclicklistener(){ public void onclick(view v){ mitemclicklistener.onitemclick(item); } });
and in fragment implement itemclicklistener interface , pass adapter
Comments
Post a Comment