I developed an android applicaion with acion bar that has 3 tabs with 3 difrrent fragments and I tried to pass a parameter from parent activity to fragments, I used the solution given in THIS qusetion and every thing is right, the only problem is that, now the activity sends this parameter to fragments just once at the start f applicaion, I need to get this parameter each time the user select the tabs, I think I should do this opration on onTabSelected method but have no idea how to do that, any suggestion?
Try this !!
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
Log.d("tab selected",""+tab.getPosition());
if(tab.getPosition==0)
{
Fragment fragA=new FrgmentA();
fragA.getData("data");
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("tab Unselected",""+tab.getPosition());
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("tab Reselected",""+tab.getPosition());
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Profile").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Picture").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Help").setTabListener(tabListener);
mActionBar.addTab(tab);
FragmentA extends Fragment{
public FragmentA(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.fragment_image, container, false);
return view;
}
public void getData(String s)
{ // your implementation
}
}
hey please pass the value when the fragment instance is created ,please take a look on below codes
I just passwed an list once the fragment is loaded via viewpager
private void setupViewPager(ViewPager viewPager, List<AccountDetail> listAc) {
Homepage.ViewPagerAdapter adapter = new Homepage.ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new AllFragment("", listAc), "All");
adapter.addFrag(new AllFragment("1", listAc), "Credit");
adapter.addFrag(new AllFragment("2", listAc), "Debit");
viewPager.setAdapter(adapter);
}
And then write a constructor in your fragment be like this
public AFragment(String text, List<AccountDetail> manufactist) {
mText = text;
manufactureListAcoonut = manufactist;
}
Related
i try to use ActionBarTabs like Twitter's swipeable tabs in android. I have three tabs and three asynctasks every tab has gridview in each other. But my view use tabs randomly. For example when page open FragmentOne will be loaded in second view. When i go secondview FragmentTwo loads third view. When i go third one i see second one. When i back second one i see first views content. Where is my mistake do you think?
pageType = getIntent().getIntExtra("type", 0);
mViewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
bar.setSelectedNavigationItem(position);
}
});
// Adding Tabs
if (pageType != 3) {
for (String tab_name : tabs) {
bar.addTab(bar.newTab().setText(tab_name).setTabListener(this));
}
} else {
for (String tab_name : tabs_multi) {
bar.addTab(bar.newTab().setText(tab_name).setTabListener(this));
}
}
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
String text = getActionBar().getSelectedTab().getText().toString();
if (text.equals("Futbol")) {
return new FragmentOne(pageType);
} else if (text.equals("Basketbol")) {
return new FragmentTwo(pageType);
} else if (text.equals("Voleybol")) {
return new FragmentThree(pageType);
} else if (text.equals("Video")) {
return new FragmentOne(pageType);
} else if (text.equals("Albüm")) {
return new FragmentTwo(pageType);
} else if (text.equals("Sesler")) {
return new FragmentThree(pageType);
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
That's not how you are supposed to implement a PagerAdapter.getItem function. Android will call this function for each possible index to get the fragment that should be shown on that tab. Since your getCount returns 3, your getItem function should have a switch statement with case statements for 0, 1, and 2, and you should construct one of your 3 fragments in each case statement. The PagerAdapter will then keep track of those fragment instances and return them.
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
}
return null;
}
I'm a bit confused about how the selected tab and the text are working, but you really do need to have just three fragments, with one on each tab.
I am trying to bind the ActionBar Tabs to a ViewPager. At the very beginning I created two separate projects for ActionBar Tabs and ViewPager, and they were working fine. When trying to bind the to each other, according to the code below, the ViewPager comply to the TabListener, in other sense, when I touch an ActionBar Tab the ViewPager change accordingly and display the respective View.For an example, I have three Tabs, when touching the Tab number two, the ViewPager displays the respective page number two. and so on.
But the ActionBar Tabs do not obey the ViewPager, in other sense, when swiping the screen to move to the next page of the ViewPager, the ViewPager shows the respective View BUT the ActionBar Tab does not change its current selected state according to the current ViewPager's selected View. For an example, when swiping to to the ViewPage number three, the ViewPager displays its respected View which is number three, BUT, the current selected ActionBar Tab does not change to be number three. I could be accessing ViewPage three while the highlighted Tab is number one.
I hope I explained the problem clearly.
MainActivity
private ViewPager mViewPager;
private MyTabsPagerAdapter mPagerAdapter;
private ActionBar mActionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> mFragList = new ArrayList<Fragment>();
mFragList.add(new Fragment01());
mFragList.add(new Fragment02());
mFragList.add(new Fragment03());
mViewPager = (ViewPager) findViewById(R.id.pager);
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPagerAdapter = new MyTabsPagerAdapter(getSupportFragmentManager(), mFragList);
mViewPager.setAdapter(mPagerAdapter);
for(int i=0; i<mFragList.size(); i++) {
mActionBar.addTab(mActionBar.newTab().setText("Fragment0"+(i+1)).setTabListener(this));
}
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(arg0.getPosition());
//mActionBar.setSelectedNavigationItem(arg0.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
MyTabsPagerAdapter
public class MyTabsPagerAdapter extends FragmentPagerAdapter {
List<Fragment> mFragList;
public MyTabsPagerAdapter(FragmentManager fm, List<Fragment> mFragList) {
super(fm);
// TODO Auto-generated constructor stub
this.mFragList = mFragList;
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return this.mFragList.get(arg0);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return this.mFragList.size();
}
}
You need to set a listener for when the viewpager changes for example,
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
This waits for the viewpager to change and when it does, it sets the actionbar position equal to the position you moved the viewpager to.
In my application, I'm showing 4 tabs using ViewPager.
tab1 | tab2 | tab3 | tab4
public class Sports_Swipe extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private Sports_SwipeAdapter mAdapter;
private ActionBarSherlock mSherlock;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3", "Tab4", "Tab5"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sports_swipe);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new Sports_SwipeAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#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
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
///////////////////////////////////////////////////
public class Sports_SwipeAdapter extends FragmentPagerAdapter {
public Sports_SwipeAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new Tab1();
case 1:
// Games fragment activity
return new Tab2();
case 2:
// Movies fragment activity
return new Tab3();
case 3:
// Games fragment activity
return new Tab4();
case 4:
// Movies fragment activity
return new Tab5();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 5;
}
}
/////////////////////////////////////
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_tab1, container, false);
//Here I get all the fields of Tab1
}
//I did this for remaining tabs also
My activity is extends FragmentActivity. Whenever the page loads I'm showing the data of tab1. Now if user clicks on tab2 I'm getting the data and showing. Now user clicks on tab3 then i'm showing the data of tab3. Up to this it is fine. Now when user goes back to the tab1 then the data is not available on the tab1. It is showing empty page.
How should I save the tab data or tab state.
Thanks
you just need to set offscreen page limit by using
setOffscreenPageLimit(int limit)
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
so use
ViewPager.setOffscreenPageLimit(3);
If data is loaded in fragment at run time (from server) then use sqlite to save data. The idea is first you download and save data in database and when fragment change or tab change populate data from database. If you want temporary storage then use load your data in your MainActivity and show data to respective fragment.
Dears,
I searched for this issue for more than a day but with no luck.
I implement exactly the code posted here:
Adding Navigation Tabs
My code for onTabSelected look like:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.alert_fragment_container, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
// prepare adapter for ExpandableListView
Log.i("After Adapter Created", "Passed");
final ExpandableListAdapter expListAdapter = new AlertsAdapter(
mActivity, myAlerts, violations);
Log.i("After Adapter Initialized", "Passed");
((MyCustomFragment)mFragment).violations.setAdapter(expListAdapter);
}
The code is working fine till last line, where I need to set the adapter for public static list initialized in MyCustomFragment in onCreateView, here my code for fragment:
public class MyCustomFragment extends Fragment {
public MyCustomFragment() {
}
public static ExpandableListView violations;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_alerts_poi, container, false);
violations = (ExpandableListView) rootView.findViewById(R.id.POIAlertList);
Log.i("onCreateView POI", "Called");
return rootView;
}
}
It give Null pointer error. With my debugging logs, I notice that this log Log.i("onCreateView POI", "Called"); appears after this Log.i("After Adapter Initialized", "Passed");. This means that I'm trying to set the adapter for a fragment isn't initialized yet.
This is the exact problem I'm face, I need to fed the ExpandableListView with data based on Tab selection in onTabSelected.
What I'm doing wrong? What is the best solution?
Regards,
It seems that you need a ViewPager, I just implemented a navigation tabs few days ago, here is my code, it navigates between 4 fragments:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
private ActionBar actionBar;
private ViewPager mViewPager;
private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
actionBar=getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon1).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon2).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon3).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon4).setTabListener(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
And here is the adapter:
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
Have a look # this Tablayout.onTabselected for latest API updates. ActionBar.TabListener is a old implementation.
i have an action bar tab, it has two tabs, the first tabs has a fragment with a layout containing a button, wat i want to do is is when i click the button it will kinda switch the layout of the current fragment where that button belongs.. and is there any way to this stuff?.. i have an idea but it produces an nullexception on the other tab, anyway lets just concentrate on the 1st tab wherein i want to switch views when a button is clicked, here is my sample code:
MainActivity:
ActionBar acBar = getActionBar();
acBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = acBar.newTab().setText("Patient");
ActionBar.Tab tab2 = acBar.newTab().setText("Contact");
Fragment frag_a = new PatientFragment();
Fragment frag_b = new ContactFragment();
tab1.setTabListener(new MyTabListener(this, frag_a));
tab2.setTabListener(new MyTabListener(this, frag_b));
acBar.addTab(tab1);
acBar.addTab(tab2);
TabListener:
class MyTabListener implements ActionBar.TabListener{
private Fragment gFrag;
private Activity gAct;
public MyTabListener(Activity act, Fragment frag){
this.gFrag = frag;
this.gAct = act;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(gAct, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, gFrag);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(gFrag);
}
}
Fragment A:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_a, container, false);
;
Button next = (Button) view.findViewById(R.id.prompt);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getActivity(), "Next!",Toast.LENGTH_LONG).show();
}
});
return view;
}
Mainly i just want to switch views in the fragment, and if there is any other way that u guys know please do share tnx!
I suggest you to switch the whole Fragment. This API was designed for such purpoises.