Android Tab Layout inside Fragment

狂风中的少年 提交于 2020-01-13 14:59:34

问题


I have a main Activity with a navigation drawer and I'm trying to realize a layout like Play Music App with tabs.

The MainActivity has a FrameLayout like a container:

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

and I want to put inside the @+id/container a Fragment with Tabs.

How's the best way to do this?

I thought I can create a MyFragment which extends Fragment and implements TabListener. The associated layout could be 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 then how can I proceed?


回答1:


Try following the following steps:

  • First, create your activity and extend FragmentActivity and implement TabListener
  • Secondly, create your TabsPagerAdapter
  • Thirdly, create your fragment (or fragments if more than one is needed).
  • You can then set the adapter in your activity after requesting navigation mode to tabs.

Code Sample

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) {

   }
}


来源:https://stackoverflow.com/questions/25476369/android-tab-layout-inside-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!