android - Change multiple fragments from ONE ActionTab -
i have tried multiple times add two fragments 1 action tab.when press action tab "search" want load 1 fragment when swipe go 2nd fragment under action tab.please find below code used.
mainactivity.java
package com.example.swipetabs; import com.tabs.*; import android.os.bundle; import android.app.actionbar; import android.app.actionbar.tab; import android.app.actionbar.tablistener; import android.app.activity; import android.app.fragmenttransaction; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.view.menu; import android.widget.imageview; import com.comparenow.*; public class mainactivity extends fragmentactivity implements tablistener { actionbar action_bar; viewpager viewpager; imageview creditcards; fragment fragement; fragment fragement1; fragment fragement2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewpager=(viewpager)findviewbyid(r.id.pager); viewpager.setadapter(new myadoptor(getsupportfragmentmanager())); viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpageselected(int arg0) { // todo auto-generated method stub action_bar.setselectednavigationitem(arg0); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { // todo auto-generated method stub } @override public void onpagescrollstatechanged(int arg0) { // todo auto-generated method stub } }); action_bar=getactionbar(); //action_bar.setbackgrounddrawable(d) action_bar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.tab tab1=action_bar.newtab(); tab1.settext("login"); tab1.settablistener(this); actionbar.tab tab2=action_bar.newtab(); tab2.settext("compare now"); tab2.settablistener(this); actionbar.tab tab3=action_bar.newtab(); tab3.settext("search"); tab3.settablistener(this); action_bar.addtab(tab1); action_bar.addtab(tab2); action_bar.addtab(tab3); } public void switchtofragmentb(){ viewpager.setcurrentitem(4); } @override public void ontabreselected(tab arg0, fragmenttransaction arg1) { // todo auto-generated method stub } @override public void ontabselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub //this code viewpager.setcurrentitem(tab.getposition()); string name=(string)tab.gettext().tostring(); // compare tab if (name == "search") { //load 1st fragment fragement1= new creditcards(); getfragmentmanager().begintransaction() .setcustomanimations(android.r.animator.fade_in, android.r.animator.fade_out) .replace(r.id.store_fragment_container, fragment).commit(); //load 2nd fragment fragement2 = new loginfrag(); getfragmentmanager().begintransaction() .setcustomanimations(android.r.animator.fade_in, android.r.animator.fade_out) .replace(r.id.imageview1,fragement2).commit(); } @override public void ontabunselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } } class myadoptor extends fragmentpageradapter { public myadoptor(fragmentmanager fm) { super(fm); // todo auto-generated constructor stub } @override public fragment getitem(int arg0) { // todo auto-generated method stub fragment fragement=null; if(arg0==0) { fragement=new loginfrag(); } if(arg0==1) { fragement=new comparefrag(); } if(arg0==2) { fragement=new searchfrag(); } system.out.print("<><><><>"); return fragement; } @override public int getcount() { // todo auto-generated method stub return 3; } }
creditcards.java (fragment) /loginfrag .java (fragement ) both same its's got corresponsing xml files well.
package com.comparenow; import com.example.swipetabs.r; import com.example.swipetabs.r.layout; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class creditcards extends fragment { public creditcards() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.creditcards_c, container, false); }
i have problem below code segment,im not sure parameters.
fragement1= new creditcards(); getfragmentmanager().begintransaction() .setcustomanimations(android.r.animator.fade_in, android.r.animator.fade_out) .replace(r.id.store_fragment_container, fragment).commit(); //load 2nd fragment fragement2 = new loginfrag(); getfragmentmanager().begintransaction() .setcustomanimations(android.r.animator.fade_in, android.r.animator.fade_out) .replace(r.id.imageview1,fragement2).commit(); }
in general, believe you're looking create viewpager
within fragment
within viewpager
.
your mainactivity
more or less right, should forget below viewpager.setcurrentitem(tab.getposition())
in ontabselected()
. instead of using searchfrag
in pager adapter's getitem()
method, you're going need create , use parent fragment
both searchfrag
, whatever other fragment
want scroll in search tab. fragment
can simple , contain viewpager
, pager adapter. in parent fragment's
pager adapter, you'll write getitem()
method return searchfrag
item 0 , whatever other fragment want scroll item 1. both of these fragment's
under search tab.
for example, fragmentpageradapter
hold searchfrag
, other fragment
should have getitem()
method following:
public static class samplefragmentadapter extends fragmentpageradapter { public samplefragmentadapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int arg0) { if(arg0 == 0){ return new searchfrag(); } else { return new otherfragmentundersearchtab(); } } @override public int getcount() { return 2; } }
this parent fragment
(let's call parentfragment
) should replace searchfrag
in getitem()
method of myadoptor
so:
@override public fragment getitem(int arg0) { // todo auto-generated method stub fragment fragement=null; if(arg0==0) { fragement=new loginfrag(); } if(arg0==1) { fragement=new comparefrag(); } if(arg0==2) { fragement=new parentfragment(); } system.out.print("<><><><>"); return fragement; }
Comments
Post a Comment