class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(DashboardFragmentActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
Is it possible instead to change Fragment to FragmentActivity? How would you implement this, I'm confuse about those two.
Fragments are always used in FragmentActivities, so you cannot replace a Fragment with a FragmentActivity, because nested activities are deprecated.
And you always have an option to use getActivity(); in the Fragments to get the parent Activity, so for whatever reason you wanted to replace Fragment with FragmentActivity it can be achieved with just Fragment.
Related
i have the following activity which produced 3 tabs, and put fragment in each one of them.
public class ClientActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabA = bar.newTab().setCustomView(R.layout.tab_a_layout);
ActionBar.Tab tabB = bar.newTab().setCustomView(R.layout.tab_b_layout);
ActionBar.Tab tabC = bar.newTab().setCustomView(R.layout.tab_c_layout);
Fragment fragmentA = new firstTab();
Fragment fragmentB = new secondTab();
Fragment fragmentC = new thirdTab();
tabA.setTabListener(new MyTabsListener(fragmentA));
tabB.setTabListener(new MyTabsListener(fragmentB));
tabC.setTabListener(new MyTabsListener(fragmentC));
bar.addTab(tabA);
bar.addTab(tabB);
bar.addTab(tabC);
}
protected class MyTabsListener implements ActionBar.TabListener {
private Fragment fragment;
public MyTabsListener(Fragment fragment)
{
this.fragment = fragment;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
ft.add(R.id.fragment_place, fragment, null);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
}
in the first fragment (tabA) i have listview and listener which replacing the fragment on click.
when i am clicking on the second tab (after selecting line on the listview on tabA), the listener adding the tab instead of replacing it.
it happens because the TabUnsellected is removing the wrong fragment (it was first_tab, but replaced to test_tab on the listview selecting row).
how can i use the remove option on the ft on TabUnselected to remove the CURRENT fragment on the tab, assuming i have always 1 fragment on each tab?
Thanks
Fixed it by changing from ft.add to ft.replace on the TabSelected:
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_place, fragment);
ft.commit();
i have 3 actionbar tabs home:video:purchase by default home will be selected.I can switch between different tabs where i'm able to replace the fragments.I have a gridview in home fragment on click of any grid cell i'm using the below piece of code to navigate to purchase tab,i'm able to replace the home fragment with purchase fragment but how can change the actionbar tab from home tab to purchase tab?????
FragmentTransaction ft = parentActivity
.getFragmentManager()
.beginTransaction();
ft.replace(R.id.mainLayout,new purchase());
ft.commit();
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
ft.setCustomAnimations(R.animator.fragmentanimatorleft,
R.animator.fragmentanimatorright);
ft.replace(R.id.mainLayout, fragment);
// isSettingClicked = false;
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
try {
ft.remove(fragment);
} catch (Exception e) {
// TODO: handle exception
}
}
}
I am trying to add fragments to the backstack but it is not working, I keep getting a: java.lang.IllegalStateException: This FragmentTransaction is not allowed to be added to the back stack.
I am using actionbar sherlock and in my main activity, i listen for the tabs
private class MyTabListener implements ActionBar.TabListener{
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft){
//to hide keyboard
final InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
if(tab.equals(tabData))
{
ft.setCustomAnimations(R.anim.animation_fragment_out, R.anim.animation_fragment_switch);
ft.replace(android.R.id.content, dataFrag);
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);
//ft.setTransition(R.anim.animation_fragment_switch);
curTab = tabData;
ft.addToBackStack(null);
//ft.commit();
//ft.commit();
}
else if(tab.equals(tabComp))//new competition
{
ft.setCustomAnimations(R.anim.animation_fragment_out, R.anim.animation_fragment_switch);
ft.replace(android.R.id.content, teamsFrag);
//ft.setTransition(R.anim.animation_fragment_switch);
//ft.commit();
actionBar.removeTab(tabData);
actionBar.removeTab(tabComp);
actionBar.addTab(tabMatches);
actionBar.addTab(tabRed);
actionBar.addTab(tabBlue);
curTab = tabComp;
ft.addToBackStack(null);
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft){
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft){
}
}
Is possible to use TabWidget without TabHost? I wanna something like Tabs navigation for ActionBar failback for older phones.
So I only want to show user tabs and listen on click actions, where I get active tab ID. Nothing more.
I know in common situations TabsNavigatin for actionBar is just for navigate through Fragments. But I easily avoid Fragments. :
class mTabListener implements ActionBar.TabListener {
private Screen screen;
public mTabListener(Screen screen) {
this.screen = screen;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
screen.onTabReselected(tab, ft);
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
screen.onTabSelected(tab, ft);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
screen.onTabUnselected(tab, ft);
}
}
public abstract class Screen extends Activity {
protected void addTab(String title, int what, boolean selected) {
if (Global.API < 11)
return;
ActionBar bar = getActionBar();
Tab tab = bar.newTab()
.setTag(new Integer(what))
.setTabListener(new mTabListener(this))
.setText(title);
bar.addTab(tab, selected);
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Integer what = (Integer)tab.getTag();
tabSelected(what);
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void tabSelected(int what) {
}
}
But I can't find how to add tab buttons to TabWidget;
No. Tab widget without tab host is like using pushbutton for the tabs. You will have do the manipulation of tab navigation.
Here is the solution, TabWidget without tabHost
https://github.com/muratonnet/android-SingleTabWidget
I am wondering if this can be done. Basically I have a layout with 2 fragments in it that I use for most of my tabs but on 2 of the tabs I want to add a couple more fragments to display more things. Is it possible to change the content view to a different layout when changing tabs?
If that cant be done I thought about creating a layout with all the fragment parts that I would need and just changing the layouts of the fragments so the ones I dont use wont "show". I dont mean using FragmentTransaction.hide() because I want the fragments to fill the screen when others are not used. Would that be a bad idea or is there an easier way to do what I want?
here is my code for the activity and tabs
public class Tabs extends Activity{
long deleteID;
#Override
public void onCreate(Bundle create){
super.onCreate(create);
setContentView(R.layout.main_layout);
createTabs();
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayHomeAsUpEnabled(true);
if(create != null){
bar.setSelectedNavigationItem(create.getInt("Home",0));
}
}
I also add tabs a whatnot but thats not important
this is the actionbar subclass
private class TabListener implements ActionBar.TabListener{
TabContent mFragment;
public TabListener(TabContent fragment) {
mFragment = fragment;
}
#Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
ListFragment newListFragment = new BowlersListFragment();
Fragment newFragment = new BowlerEntryFrag();
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frameOne, newListFragment);
ft.replace(R.id.frameTwo, newFragment);
ft.addToBackStack(null);
ft.commit();
}
#Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
if(ft != null){
ft.remove(mFragment);
}
}
}
}
I tried doing Activity.setContentView(r.layout.newView) but that won't work
I had similar problem, so implemented it like this. I have a mainActivity that holds and creates the tabs. That activity uses a layout with one fragment container, main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
In my activity I have the following code for creating the tabs and fragments:
historyTab = actionbar.newTab().setText("History");
historyTab.setTabListener(new MyTabsListener(new HistoryFragment()));
actionbar.addTab(historyTab);
My tabListener looks something like this:
private class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
private boolean isFragmentAdded = false;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xaction = fragMgr.beginTransaction();
if (fragment instanceof HistoryFragment) {
fragment = new HistoryFragment();
xaction.add(R.id.fragment_container, fragment);
} else if (fragment instanceof .. some other fragment..) {
fragment = new .. some other fragment();
xaction.add(R.id.fragment_container, fragment);
}
}
xaction.commit();
}
#Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xaction;
xaction = fragMgr.beginTransaction();
Fragment fragment = fragMgr.findFragmentById(R.id.fragment_container);
xaction.remove(fragment);
xaction.commit();
}
}
The layout for the History fragment in my example, can be anything, even a layout that contains two fragments, or as much you need. Just be careful, and in onCreateview method in the fragment, use this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
if (view == null) { // this line is very important!
view = inflater.inflate(R.layout.history_fragment_layout, container, false);
listView = (ListView) view.findViewById(R.id.historyList);
}
return view;
}
Hope that helps!:)