android - Onclick() in a Parent Fragment does not get called -
i have fragmentpager implementing 3 tabs. inside 1 of tabs need this: if user clicks on button want load fragment inside tab , if user clicks on button b want load fragment b in page.
but i'm stuck @ first phase. when click on button onclick method not called. following steps given in slidenerd.com video #10 fragments.
i have implemented onclickslistener , added onclick method example. toast message not fire.
below code mainactivity , parent fragment (the mentioned tab) , xml.
thanks in advance.
mainactivity.java
public class mainactivity extends actionbaractivity implements actionbar.tablistener { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use {@link fragmentpageradapter} * derivative, keep every loaded fragment in memory. if * becomes memory intensive, may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ sectionspageradapter msectionspageradapter; viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); settitle(farsi.convert(getstring(r.string.app_name_farsi))); setcontentview(r.layout.activity_main); final actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // create adapter return fragment each of 3 primary sections of activity. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); // when swiping between different sections, select corresponding tab. can use actionbar.tab#select() if have reference tab. mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); // each of sections in app, add tab action bar. (int = 0; < msectionspageradapter.getcount(); i++) { // create tab text corresponding page title defined adapter. specify activity object, implements tablistener interface, callback (listener) when tab selected. actionbar.addtab(actionbar.newtab().settext(msectionspageradapter.getpagetitle(i)).settablistener(this)); } //ff code: make right tab selected @ beginning. mviewpager.setcurrentitem(msectionspageradapter.getcount()-1); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar automatically handle clicks on home/up button, long specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { // when given tab selected, switch corresponding page in viewpager. mviewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } //******************************************************************** inner class /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { if (position == 0) return quickcreatepropertylistingfragment.newinstance(position+1); else if (position ==1) return searchforrentfragment.newinstance(position+1); else return searchforsalefragment.newinstance(position+1); } @override public int getcount() { // show 3 total pages. return 3; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return farsi.convert(getstring(r.string.label_create)); case 1: return farsi.convert(getstring(r.string.label_rent)); case 2: return farsi.convert(getstring(r.string.label_sell)); } return null; } }
}
parent fragment
import android.app.activity; import android.content.context; import android.content.intent; import android.os.bundle; import android.provider.contactscontract.statusupdates; import android.support.v4.app.fragment; import android.support.v4.app.fragmenttransaction; import android.support.v4.app.fragmentmanager; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.button; import android.widget.linearlayout; import android.widget.radiobutton; import android.widget.textview; import android.widget.toast; public class quickcreatepropertylistingfragment extends fragment implements onclicklistener{ private activity activity; public static quickcreatepropertylistingfragment newinstance(int sectionnumber){ quickcreatepropertylistingfragment fragment = new quickcreatepropertylistingfragment(); bundle args = new bundle(); args.putint("section_number", sectionnumber); fragment.setarguments(args); return fragment; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.quick_create_property_listing, container,false); return rootview; } @override public void onactivitycreated(bundle savedinstancestate) { // todo auto-generated method stub super.onactivitycreated(savedinstancestate); button here = (button) getactivity().findviewbyid(r.id.radio32); here.setonclicklistener(this); } public void onclick1(view arg0) { // todo auto-generated method stub toast.maketext(getactivity().getapplicationcontext(), "this toast message!!! =)" , toast.length_long).show(); textview tv = (textview) getactivity().findviewbyid(r.id.textviewsale); tv.settext("test"); toast.maketext(getactivity().getapplicationcontext(), "this toast message!!! =)" , toast.length_long).show(); } @override public void onclick(view arg0) { // todo auto-generated method stub } } } }
activiti_main.xml
<android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.myapp.myapp.mainactivity"/>
fragment_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.myapp.myapp.mainactivity$placeholderfragment" > <textview android:id="@+id/section_label" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </relativelayout> child fragment.xml <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_margin="5dp" android:text="category" android:textappearance="?android:attr/textappearancemedium"/> <horizontalscrollview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:paddingright="10dp"> <radiogroup android:id="@+id/radiogroupintab3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" > <radiobutton android:id="@+id/radio31" android:layout_width="100dip" android:layout_height="wrap_content" android:background="@drawable/yourbuttonbackground" android:button="@android:color/transparent" android:checked="true" android:gravity="center" android:text="3" /> <radiobutton android:id="@+id/radio32" android:layout_width="100dip" android:layout_height="wrap_content" android:background="@drawable/yourbuttonbackground" android:button="@android:color/transparent" android:checked="false" android:gravity="center" android:text="2" android:onclick="onclick1" /> <radiobutton android:id="@+id/radio33" android:layout_width="100dip" android:layout_height="wrap_content" android:background="@drawable/yourbuttonbackground" android:button="@android:color/transparent" android:checked="false" android:gravity="center" android:text=" 1"/> </radiogroup> </horizontalscrollview> </linearlayout>
i think didn't register fragment view in parent view. please try code. think work!
@override public view oncreateview( layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //view rootview = inflater.inflate(r.layout.quick_create_property_listing, container,false); view rootview = inflater.inflate(activity, r.layout.quick_create_property_listing, this); return rootview; }
Comments
Post a Comment