Android Studio defaults to "Tabbed Activity", how to scroll fragments?

Complete the beginner here.

I used the default Tabbed Activity parameter from the Project Creation Wizard.

I try to make him zip through three different fragments, but I just can't see where to tell the program to do this. I load them as an array, if so, where should I do it and how do I create instances of different fragments?

Any pointers and / or solutions are greatly appreciated.

+8
android android-fragments swipe
source share
3 answers

Here's how you do it: A tutorial with tabs for classes

+4
source share

You can create a pager adapter from which you can call tab-based fragments.

public class TabsPagerAdapter extends FragmentPagerAdapter { public TabsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int index) { switch (index) { case 0: // Top Rated fragment activity return new TopRatedFragment(); case 1: // Games fragment activity return new GamesFragment(); case 2: // Movies fragment activity return new MoviesFragment(); } return null; } @Override public int getCount() { // get item count - equal to number of tabs return 3; } 

}

and initialize the tab values ​​in the onCreate method of the main activity to get the tabs working

 private String[] tabs = { "Top Rated", "Games", "Movies" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initilization viewPager = (ViewPager) findViewById(R.id.pager); actionBar = getActionBar(); mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mAdapter); actionBar.setHomeButtonEnabled(false); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Adding Tabs for (String tab_name : tabs) { actionBar.addTab(actionBar.newTab().setText(tab_name) .setTabListener(this)); } 

}

+2
source share

I know this question is old, but hopefully it can help someone. This was very unpleasant for me, as I try to learn Android, but the wizard seems to have provided an incomplete template.

Anyway,

In fragment_main.xml add text to the TextView, and now the pages will appear where there is content.

 <TextView android:id="@+id/section_label" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Hello World" /> 
+1
source share

All Articles