【问题标题】:Using Tabs and Fragments to display my Layouts使用选项卡和片段显示我的布局
【发布时间】:2015-04-21 20:43:15
【问题描述】:

我有一个现有的应用程序,我想添加选项卡以在布局之间导航 我确实找到了几个关于同一主题的教程和主题,但我是 Android 的初学者,我发现这样做有一些困难,所以我希望有人可以帮助我做到这一点 顺便说一句,我确实在 Android Studio 中使用了 TabbedActivity,它确实添加了选项卡,但我不知道如何为这些选项卡实现布局并且仍然有选项卡栏,因为我这样做了

if(tab.getPosition()==0){
         Intent intent = new Intent("com.blooddonor.blooddonor.profil");
         startActivity(intent);

      }

而且我不会认为它会打开另一个未显示在具有标签的主要布局上的标签

【问题讨论】:

    标签: android android-layout android-fragments android-tabs android-pageradapter


    【解决方案1】:

    您想使用片段而不是活动来使用标签导航。您正在做的是启动一个不会启用选项卡导航的新活动。我建议创建一个选项卡式活动并查看它是如何构建的。这是来自股票样本的一个非常基本的选项卡式导航。

    public class TabbedActivity extends Activity implements ActionBar.TabListener {
    
    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v13.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;
    
    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabbed);
    
        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
    
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    
        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
    
    
    // set up your tabs here instead using this for loop
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }
    
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }
    
    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
    
    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
    
    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
    // Here is where you would implement which fragment to set
            return PlaceholderFragment.newInstance(position + 1);
        }
    
        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                default:
                    return null;
            }
        }
    }
    
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";
    
        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }
    
        public PlaceholderFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_placeholder, container, false);
            return rootView;
        }
    }
    }
    

    【讨论】:

    • 谢谢!但是我如何创建片段以及如何将它们用于此选项卡式活动
    • 您可以像创建活动一样创建片段。我假设您使用的是 android studio,它只是 File > New > Fragment,然后选择您想要的片段类型。要将其添加到页面中,您需要使用我上面提到的 ViewPager,或者提供这样的片段容器:
    • 然后你需要使用片段管理器: FragmentManager fragmentManager = getFragmentManager();然后像这样更改片段:fragmentManager.beginTransaction() .replace(R.id.container, PlaceHolderFragment.newInstance(), "fragment tag") .commit();
    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多