Android tab layout inside snippet

I have a basic action with a navigation box, and I'm trying to implement a tabbed play app like Play Music App.

MainActivity has FrameLayout as a container:

<FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> 

and I want to place a Tab Fragment inside @ + id / container.

What is the best way to do this?

I thought I could create a MyFragment that extends the fragment and implements the TabListener. The corresponding layout may look like

 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> 

But how can I continue?

+3
source share
1 answer

Try the following steps:

  • First create your activity and extend FragmentActivity and implement TabListener
  • Second, create your TabsPagerAdapter
  • Third, create a fragment (or fragments if more than one is required).
  • Then you can configure the adapter in its activity after requesting the navigation mode on the tabs.

Code example

 public class SampleActivity extends FragmentActivity implements ActionBar.TabListener { private ViewPager viewPager; private ActionBar actionBar; private TabsPagerAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_ACTION_BAR); setContentView(R.layout.gd_pager); getActionBar().setDisplayHomeAsUpEnabled(true); viewPager = (ViewPager) findViewById(R.id.pager); actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); String [] tabs = new String[]{"Tab1", "Tab2", "Tab3", "Tab3"}; for(String tab : tabs) { actionBar.addTab(actionBar.newTab() .setText(tab) .setTabListener(this)); } mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mAdapter); @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { viewPager.setCurrentItem(tab.getPosition()); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { } } 
0
source

Source: https://habr.com/ru/post/1013676/


All Articles