无法里面的导航抽屉的Andr​​oid片段添加标签 [英] Unable to add Tabs inside Fragment of Navigation Drawer Android

查看:108
本文介绍了无法里面的导航抽屉的Andr​​oid片段添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我已经遵循了Android开发者文档中的导航抽屉例如的这里 <一href="http://developer.android.com/training/implementing-navigation/nav-drawer.html">developer.android.com/training/implementing-navigation/nav-drawer.html
并创建了我的整个应用程序。在给定的例子,他们使用的片段在抽屉称为片段选择具有以下code各自项目

 捆绑的args =新包();
args.putInt(的title_number,位置);
fragment.setArguments(参数);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()代替(R.id.content_frame,片段).commit()。
 

2)现在,我希望有一个片段也就是,当我选择在导航抽屉一个特定的项目,分片装应显示标签栏在标签的行为在上面这句话。 http://flic.kr/p/hn4G3i

3)我按照教程和例子在这里
给出 <一href="http://developer.android.com/training/implementing-navigation/lateral.html">developer.android.com/training/implementing-navigation/lateral.html,
但这里给出的例子是使用 FragmentActivity 这是不符合片段兼容(按我的知识)。

有人能帮助我实现我的应用程序这一行为。提前致谢。

解决方案

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

公共类TestTabsinsideFragment扩展片段
{
    查看rootView;

公共TestTabsinsideFragment()
{
    需要片段子类//空构造
}

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{

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

//应用布局片段
rootView = inflater.inflate(R.layout.approve_leaves,集装箱,假);


。getActivity()的setTitle(新标签布局中片段:-));


ActionBar.TabListener tabListener =新ActionBar.TabListener(){
    公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction英尺){
        //显示给定的标签
    }

    公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction英尺){
        //隐藏指定的标签
    }

    公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction英尺){
        //可能忽略此事件
    }
};

//添加3个标签,指定标签的文本和TabListener
为(中间体的i1 = 0; i1的3;;的i1 ++){
    getActivity()。getActionBar()。addTab(
            getActivity()。getActionBar()。newTab()
            .setText(标签+(I1 + 1))
            .setTabListener(tabListener));
}


返回rootView;
}
 

我有固定自己的问题。 :D

1) I have followed the Navigation Drawer example in Android Developer Docs here developer.android.com/training/implementing-navigation/nav-drawer.html
and created my whole application. In the given example, they have used Fragments for each item selected in the Drawer called fragments with following code

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) Now I want a Tab behavior inside a Fragment i.e., when I select a particular item in the Nav-Drawer, the Fragment loaded should display a Tab bar on the top something like this. http://flic.kr/p/hn4G3i

3) I have followed the tutorial and example given here
developer.android.com/training/implementing-navigation/lateral.html,
but the example given here is using FragmentActivity which is not compatible with Fragments (as per my knowledge).

Could someone help me achieving this behaviour in my app. Thanks in advance.

解决方案

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;
}

I have fixed the problem myself. :D

这篇关于无法里面的导航抽屉的Andr​​oid片段添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆