0

I would like to make it possible to swipe between the tabs in my app. I have already found plenty tutorials to do this, but I can't get it to work with my code, because I use different code. MyActivity.class:

public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

final ActionBar actionBar = getActionBar();
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

             ActionBar.Tab tabA = actionBar.newTab();
                tabA.setIcon(R.drawable.icon_settings);
              actionBar.setDisplayShowHomeEnabled(false);
             actionBar.setDisplayShowTitleEnabled(false);

            tabA.setText("");
         tabA.setTabListener(new com.example.MainActivity.TabListener<Tab1>(this, "Tab1", Tab1.class));
         actionBar.addTab(tabA);





          Tab tabB = actionBar.newTab();
        tabB.setText("Tab2");
         tabB.setTabListener(new com.example.MainActivity.TabListener<Tab2>(this, "Tab2", Tab2.class));
          actionBar.addTab(tabB);

            Tab tabC = actionBar.newTab();           
            tabC.setText("Tab3");

         tabC.setTabListener(new com.example.MainActivity.TabListener<Tab3>(this, "Tab3", Tab3.class));
         actionBar.addTab(tabC);

        if (savedInstanceState != null) {
              int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
            getActionBar().setSelectedNavigationItem(savedIndex);
       }
}

Main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
               >
    <TabHost
            android:id="@+id/tabHost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:paddingTop="5dip">
            <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
            </TabWidget>
            <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
<LinearLayout
                    android:id="@+id/einstellungen"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" android:orientation="vertical" android:background="#ffffff">
</LinearLayout>
            <LinearLayout
                android:id="@+id/tab1"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
           </LinearLayout>

            <LinearLayout
                    android:id="@+id/tab2"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
            </LinearLayout>
            <LinearLayout
                    android:id="@+id/tab3"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

Tab1.class

public class Tab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
    return myFragmentView;
}

}

TabListener.class

public class TabListener <T extends Fragment> implements ActionBar.TabListener{

    private final Activity myActivity;
    private final String myTag;
    private final Class myClass;

    public TabListener(Activity activity, String tag, Class<T> cls) {
        myActivity = activity;
        myTag = tag;
        myClass = cls;
    }
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        if (myFragment == null) {
            myFragment = Fragment.instantiate(myActivity, myClass.getName());
            ft.add(android.R.id.content, myFragment, myTag);
        } else {
            ft.attach(myFragment);
        }

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        if (myFragment != null) {
            ft.detach(myFragment);
        }

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}
mmBs
  • 8,421
  • 6
  • 38
  • 46
Fynn
  • 210
  • 1
  • 6
  • 28

1 Answers1

0

You can do it easier using the default ActionBar Tabs + Swipe Views of your Eclipse, Android Sudio or IntelliJ IDEA. Anyway you can take a look at my answer here Android ActionBar Tabs - Swipe or here Creating Swipe Views with Tabs you can find a very nice tutorial by Android Developers.

Hope I helped you

Community
  • 1
  • 1
Rick
  • 3,943
  • 6
  • 33
  • 45
  • I already looked at your comment and tried to implement it, but I don't get it to work. Could you tell me where exactly in my code I have to copy which part of your code? And I think my code needs "extends Activity" to work and I don't know how I can use Activity and FragmentActivity – Fynn May 16 '14 at 13:28
  • Couldn't you open a new project and follow exactly the tutorial? It's simpler – Rick May 16 '14 at 14:25
  • I already have a lot of code in my project, I think it would be more work to try to migrate the code – Fynn May 16 '14 at 14:29
  • 1
    But much harder for someone to debug your code than you running through a clean tutorial, and learning how it works (+1 for using tutorials ;-) ). – Booger May 17 '14 at 13:27