android - 无法在抽屉导航 Android 的 fragment 内添加选项卡

标签 android android-fragments android-tabs navigation-drawer

1) 我在这里遵循了 Android 开发者文档中的抽屉导航示例 developer.android.com/training/implementing-navigation/nav-drawer.html
并创建了我的整个应用程序。在给定的示例中,他们为抽屉中选择的每个项目使用了 fragment ,称为 fragment ,代码如下

Bundle args = new Bundle();
args.putInt("Title_Number", position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

2) 现在我想要一个 Fragment 中的 Tab 行为 即,当我在 Nav-Drawer 中选择一个特定项目时,加载的 Fragment 应该显示一个 Tab 栏在上面是这样的。 http://flic.kr/p/hn4G3i

3) 我已按照此处给出的教程和示例进行操作
developer.android.com/training/implementing-navigation/lateral.html ,
但此处给出的示例使用的是与 Fragment 不兼容的 FragmentActivity(据我所知)。

有人可以帮助我在我的应用中实现这种行为吗?提前致谢。

最佳答案

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cgg.gov.in.apps.eoffice.source.R;

public class TestTabsinsideFragment extends Fragment
{
    View rootView;

public TestTabsinsideFragment () 
{
    // Empty constructor required for fragment subclasses
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState)
{   

getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Apply the layout for the fragment
rootView = inflater.inflate(R.layout.approve_leaves, container, false);


getActivity().setTitle("New tabbed layout inside Fragment :-) ");


ActionBar.TabListener tabListener = new ActionBar.TabListener() {
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        // show the given tab
    }

    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        // hide the given tab
    }

    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        // probably ignore this event
    }
};

// Add 3 tabs, specifying the tab's text and TabListener
for (int i1 = 0; i1 < 3; i1++) {
    getActivity().getActionBar().addTab(
            getActivity().getActionBar().newTab()
            .setText("Tab " + (i1 + 1))
            .setTabListener(tabListener));
}


return rootView;
}

我自己解决了这个问题。 :D

关于android - 无法在抽屉导航 Android 的 fragment 内添加选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19856941/

相关文章:

android - React Native - Android TV - DPad 功能

android - 动态创建的 <li> 在 html5 中水平滚动

android - 如何选择抽屉导航中的第一项并在应用程序启动时打开 fragment

android - 标签 View 可以在 fragment 中使用吗

android - 在上下文操作栏中,如何通过使选项卡不可点击/滑动来禁用滑动选项卡?

android - 如何生成用于模拟的 android.jar?

android - 在 Android Phonegap App 中填写时防止滚动到输入

android - 这是从 fragment 访问数组列表的正确方法吗?

android - Android如何在ViewPager中处理 fragment 生命周期?

android - 如何为android设置actionbar标签的高度