Wednesday, 15 May 2013

android - ActionBar 2 tabs, lists in each tab, row onClick not always firing -



android - ActionBar 2 tabs, lists in each tab, row onClick not always firing -

this might duplicate question, i've looked through lot of questions, didn't find helped.

i have app has 2 tabs using actionbar. each listfragment , unable select of items on them. populate fine, onlistitemclick won't fire. won't fire if utilize setonitemclicklistener straight listview. however, if extend fragment , not listfragment, if attach onclicklistener each row, onclick fire. allow multiple rows clickable , not want happen. can help me figure out i'm doing wrong? in advance.

this mainactivity:

public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // action bar drawable drawable = getresources().getdrawable(r.drawable.action_bar); actionbar bar = getactionbar(); bar.setbackgrounddrawable(drawable); bar.addtab(bar.newtab().settext("tab 1").settablistener(new tabhandler())); bar.addtab(bar.newtab().settext("tab 2").settablistener(new tabhandler())); bar.setdisplayoptions(actionbar.display_show_custom | actionbar.display_use_logo); bar.setnavigationmode(actionbar.navigation_mode_tabs); bar.setdisplayshowhomeenabled(true); bar.setdisplayhomeasupenabled(true); bar.setdisplayshowtitleenabled(false); } }

this tabhandler:

public class tabhandler implements actionbar.tablistener { public fragment mfragment; public tabhandler() { } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { switch (tab.getposition()) { case 0: if(mfragment == null) { mfragment = new listfragmenta(); fragmenttransaction.add(android.r.id.content, mfragment, null); } else { fragmenttransaction.show(mfragment); //looking detach , attach } break; case 1: if(mfragment == null) { mfragment = new listfragmentb(); fragmenttransaction.add(android.r.id.content, mfragment, null); } else { fragmenttransaction.show(mfragment); } break; default: break; } } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { if (mfragment != null) { mfragment.getfragmentmanager().popbackstackimmediate(); fragmenttransaction.hide(mfragment); } } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { mfragment.getfragmentmanager().popbackstackimmediate(); } }

this illustration of 1 of fragments:

public class listfragmenta extends listfragment { mycustomadapter mycustomadapter; listview mylistview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { homecoming inflater.inflate(r.layout.my_list_background,container,false); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); if(getactivity() != null) { mylistview = (listview)getactivity().findviewbyid(android.r.id.list); if(mylistview != null) { crudservice itemservice = new crudservice<testitem>(testitem.class); arraylist<testitem> myitems = (arraylist<testitem>) itemservice.findall(); mycustomadapter = new mycustomadapter(getactivity(), myitems); mylistview.setadapter(mycustomadapter); mylistview.setchoicemode(listview.choice_mode_single); mylistview.setonitemclicklistener(new onitemclicklistener(){ @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { log.i("listfragmenta", "mylistview onitemclick"); } }); } } } //this isn't getting fired @override public void onlistitemclick(listview l, view v, int position, long id) { log.i("listfragmenta", "onlistitemclick @ position " + position); } public class mycustomadapter extends arrayadapter<testitem> { context mcontext; arraylist<testitem> mitems; public mycustomadapter(context context, arraylist<testitem> items) { super(context, android.r.id.list, items); mcontext = context; mitems = items; } public int getcount() { homecoming mitems.size(); } public testitem getitem(int position) { homecoming mitems.get(position); } public long getitemid(int position) { homecoming mitems.get(position).getid(); } public view getview(final int position, view convertview, viewgroup arg2) { view rowview = convertview; if(rowview == null) { layoutinflater inflater = (layoutinflater)mcontext.getsystemservice(context.layout_inflater_service); rowview = inflater.inflate(r.layout.single_item, null); testitemviewholder viewholder = new testitemviewholder(); viewholder.name = (textview)rowview.findviewbyid(r.id.txt_testitem_name); //i have few more things rowview.settag(viewholder); } testitemlistviewholder holder = (testitemlistviewholder)rowview.gettag(); testitem gottenitem = getitem(position); holder.name.settext(gottenitem.getname()); //i have few more things //however, fire if not listfragment rowview.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { log.i("listfragmenta", "rowview onclick @ position " + position); } }); homecoming rowview; } } }

here illustration of "my_list_background" xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab_layout_a" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp" android:background="@color/white"> <!-- listview title layout --> <linearlayout android:id="@+id/ll_table_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_marginbottom="-15dp" android:layout_margintop="7dp" android:layout_marginleft="11dip" android:layout_marginright="11dip" android:orientation="horizontal" android:weightsum="100" android:background="@color/black"> <textview android:id="@+id/txt_testitem_name" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/name" android:textcolor="@color/white" android:textstyle="bold" /> <!--i have omitted other columns--> </linearlayout> <linearlayout android:id="@+id/message_list_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white"> <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" > </listview> </linearlayout> </linearlayout>

and illustration of "single_item" xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/single_row" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" android:weightsum="100"> <textview android:id="@+id/txt_testitem_name" android:layout_width="0dip" android:layout_height="wrap_content" android:padding="2dip" android:layout_weight="25" android:textcolor="@color/black"/> <imagebutton android:id="@+id/btn_next" android:layout_width="0dip" android:layout_height="wrap_content" android:padding="2dip" android:layout_weight="2" android:background="@drawable/disclosure_button" android:contentdescription="@string/next_button"/> </linearlayout>

ok, found problem. in "single_item" xml file, had imagebutton stealing focus. swapped imageview , works fine.

android android-listview android-actionbar android-listfragment

No comments:

Post a Comment