我的片段隐藏了导航抽屉? [英] My Fragment hides the Navigation Drawer?

查看:195
本文介绍了我的片段隐藏了导航抽屉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽屉式导航在我的应用程序。如果我选择一个特定的项目和滑动抽屉式导航。该片段重叠导航抽屉。

我尝试添加 popBackStackImmediate(); 但它是没有用的。这是它的样子

活动

 公共类活动来延长MainActivity
{

    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
    }

    @覆盖
    公共无效onNavigationDrawerItemSelected(INT位置)
    {
        // TODO自动生成方法存根
        如果(位置== 1)
        {
            HomeFragment破片=新HomeFragment();
            FragmentTransaction交易= getFragmentManager()的BeginTransaction()。
            transaction.replace(R.id.container,破片);
            器transaction.commit();
        }
        否则,如果(位置== 2)
        {

        }
    }
    公共无效onDrawerOpened(查看drawerview)
    {
        。getFragmentManager()popBackStackImmediate();
    }
 

导航片段

 公共类NavigationDrawerFragment扩展片段实现NavigationDrawerCallbacks
{
    私有静态最后弦乐preF_USER_LEARNED_DRAWER =navigation_drawer_learned;
    私有静态最后弦乐STATE_SELECTED_POSITION =selected_navigation_drawer_position;
    私有静态最后弦乐preFERENCES_FILE =my_app_settings;
    私人NavigationDrawerCallbacks mCallbacks;
    私人RecyclerView mDrawerList;
    私人查看mFragmentContainerView;
    私人DrawerLayout mDrawerLayout;
    私人ActionBarDrawerToggle mActionBarDrawerToggle;
    私人布尔mUserLearnedDrawer;
    私人布尔mFromSavedInstanceState;
    私人诠释mCurrentSelectedPosition;

    @Nullable
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
    {
        查看查看= inflater.inflate(R.layout.fragment_navigation_drawer,集装箱,假);
        mDrawerList =(RecyclerView)view.findViewById(R.id.drawerList);
        LinearLayoutManager的layoutManager =新LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mDrawerList.setLayoutManager(的layoutManager);
        mDrawerList.setHasFixedSize(真正的);

        最后的名单,其中,NavigationItem> navigationItems =使用getMenu();
        NavigationDrawerAdapter适配器=新NavigationDrawerAdapter(navigationItems);
        adapter.setNavigationDrawerCallbacks(本);
        mDrawerList.setAdapter(适配器);
        选择信息(mCurrentSelectedPosition);
        返回查看;
    }

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mUserLearnedDrawer =通过Boolean.valueOf(readSharedSetting(getActivity(),preF_USER_LEARNED_DRAWER,假));
        如果(savedInstanceState!= NULL)
        {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = TRUE;
        }
    }

    @覆盖
    公共无效onAttach(活动活动)
    {
        super.onAttach(活动);
        尝试
        {
            mCallbacks =(NavigationDrawerCallbacks)的活动;
        }
        赶上(ClassCastException异常E)
        {
            抛出新的ClassCastException异常(活动必须实现NavigationDrawerCallbacks。);
        }
    }

    公共ActionBarDrawerToggle getActionBarDrawerToggle()
    {
        返回mActionBarDrawerToggle;
    }

    公共无效setActionBarDrawerToggle(ActionBarDrawerToggle actionBarDrawerToggle)
    {
        mActionBarDrawerToggle = actionBarDrawerToggle;
    }

    公共无效设置(INT fragmentId,DrawerLayout drawerLayout,工具栏工具栏)
    {
        mFragmentContainerView = getActivity()findViewById(fragmentId)。
        mDrawerLayout = drawerLayout;
        mActionBarDrawerToggle =新ActionBarDrawerToggle(getActivity(),mDrawerLayout,工具栏,R.string.drawer_open,R.string.drawer_close)
        {
            @覆盖
            公共无效onDrawerClosed(查看drawerView)
            {
                super.onDrawerClosed(drawerView);
                如果(!isAdded())
                    返回;
                。getActivity()invalidateOptionsMenu();
            }

            @覆盖
            公共无效onDrawerOpened(查看drawerView)
            {
                super.onDrawerOpened(drawerView);
                如果(!isAdded())
                    返回;
                如果(!mUserLearnedDrawer)
                {
                    mUserLearnedDrawer = TRUE;
                    saveSharedSetting(getActivity(),preF_USER_LEARNED_DRAWER,真);
                }
                。getActivity()invalidateOptionsMenu();
            }
        };

        如果(mUserLearnedDrawer&安培;!&安培;!mFromSavedInstanceState)
            mDrawerLayout.openDrawer(mFragmentContainerView);

        mDrawerLayout.post(新的Runnable()
        {
            @覆盖
            公共无效的run()
            {
                mActionBarDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
    }

    公共无效openDrawer()
    {
        mDrawerLayout.openDrawer(mFragmentContainerView);
    }

    公共无效closeDrawer()
    {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }

    @覆盖
    公共无效onDetach()
    {
        super.onDetach();
        mCallbacks = NULL;
    }

    公开名单< NavigationItem>使用getMenu()
    {
        名单< NavigationItem>项目=新的ArrayList< NavigationItem>();
        items.add(新NavigationItem(家,getResources()getDrawable(R.drawable.play)));
        items.add(新NavigationItem(新闻,getResources()getDrawable(R.drawable.play)));
        items.add(新NavigationItem(视频,getResources()getDrawable(R.drawable.play)));
        items.add(新NavigationItem(播放列表,getResources()getDrawable(R.drawable.play)));
        返回的项目;
    }

    无效选择信息(INT位置)
    {
        mCurrentSelectedPosition =位置;
        如果(mDrawerLayout!= NULL)
        {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        如果(mCallbacks!= NULL)
        {
            mCallbacks.onNavigationDrawerItemSelected(位置);
        }
        ((NavigationDrawerAdapter)mDrawerList.getAdapter())selectPosition(位置)。
    }

    公共布尔isDrawerOpen()
    {
        返回mDrawerLayout = NULL和放大器;!&安培; mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }

    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG)
    {
        super.onConfigurationChanged(NEWCONFIG);
        mActionBarDrawerToggle.onConfigurationChanged(NEWCONFIG);
    }

    @覆盖
    公共无效的onSaveInstanceState(包outState)
    {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION,mCurrentSelectedPosition);
    }

    @覆盖
    公共无效onNavigationDrawerItemSelected(INT位置)
    {
        mCallbacks.onNavigationDrawerItemSelected(位置);
        选择信息(位置);

    }

    公共DrawerLayout getDrawerLayout()
    {
        返回mDrawerLayout;
    }

    公共无效setDrawerLayout(DrawerLayout drawerLayout)
    {
        mDrawerLayout = drawerLayout;
    }
}
 

活动布局

 < RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <包括
        机器人:ID =@ + ID / toolbar_actionbar
        布局=@布局/ toolbar_default
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT/>

    < android.support.v4.widget.DrawerLayout
        机器人:ID =@ + ID /抽屉
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_below =@ + ID / toolbar_actionbar>

        <的FrameLayout
            机器人:ID =@ + ID /容器
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent/>

        !<  - 安卓layout_marginTop =机器人:ATTR / actionBarSize - >
        <片段
            机器人:ID =@ + ID / fragment_drawer
            机器人:名称=com.RemoteIt.client.activity.drawer.NavigationDrawerFragment
            机器人:layout_width =@扪/ navigation_drawer_width
            机器人:layout_height =match_parent
            机器人:layout_gravity =开始
            应用程序:布局=@布局/ fragment_navigation_drawer/>
    < /android.support.v4.widget.DrawerLayout>
< / RelativeLayout的>
 

HomeFragment

 公共类HomeFragment扩展片段
{

    公共HomeFragment()
    {}

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
    {视图V = inflater.inflate(R.layout.home,集装箱,假);

        INT redActionButtonSize = getResources()getDimensionPixelSize(R.dimen.red_action_button_size)。
        INT redActionButtonMargin = getResources()getDimensionPixelOffset(R.dimen.action_button_margin)。
        INT redActionButtonContentSize = getResources()getDimensionPixelSize(R.dimen.red_action_button_content_size)。
        INT redActionButtonContentMargin = getResources()getDimensionPixelSize(R.dimen.red_action_button_content_margin)。
        INT redActionMenuRadius = getResources()getDimensionPixelSize(R.dimen.red_action_menu_radius)。
        INT blueSubActionButtonSize = getResources()getDimensionPixelSize(R.dimen.blue_sub_action_button_size)。
        INT blueSubActionButtonContentMargin = getResources()getDimensionPixelSize(R.dimen.blue_sub_action_button_content_margin)。

        ImageView的fabIconStar =新ImageView的(getActivity());
        fabIconStar.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_important));

        FloatingActionButton.LayoutParams starParams =新FloatingActionButton.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
        starParams.setMargins(redActionButtonMargin,redActionButtonMargin,redActionButtonMargin,redActionButtonMargin);
        fabIconStar.setLayoutParams(starParams);

        FloatingActionButton.LayoutParams fabIconStarParams =新FloatingActionButton.LayoutParams(redActionButtonContentSize,redActionButtonContentSize);
        fabIconStarParams.setMargins(redActionButtonContentMargin,redActionButtonContentMargin,redActionButtonContentMargin,redActionButtonContentMargin);

        FloatingActionButton leftCenterButton =新FloatingActionButton.Builder(getActivity())。的setContentView(fabIconStar, fabIconStarParams).setBackgroundDrawable(R.drawable.button_action_red_selector).setPosition(FloatingActionButton.POSITION_TOP_CENTER).setLayoutParams(starParams).build();

        //设置自定义SubActionButtons合适中心菜单
        SubActionButton.Builder lCSubBuilder =新SubActionButton.Builder(getActivity());
        lCSubBuilder.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_action_blue_selector));

        FrameLayout.LayoutParams blueContentParams =新FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
        blueContentParams.setMargins(blueSubActionButtonContentMargin,blueSubActionButtonContentMargin,blueSubActionButtonContentMargin,blueSubActionButtonContentMargin);
        lCSubBuilder.setLayoutParams(blueContentParams);
        //设置自定义布局PARAMS
        FrameLayout.LayoutParams blueParams =新FrameLayout.LayoutParams(blueSubActionButtonSize,blueSubActionButtonSize);
        lCSubBuilder.setLayoutParams(blueParams);

        ImageView的lcIcon1 =新ImageView的(getActivity());
        ImageView的lcIcon2 =新ImageView的(getActivity());
        ImageView的lcIcon3 =新ImageView的(getActivity());
        ImageView的lcIcon4 =新ImageView的(getActivity());
        ImageView的lcIcon5 =新ImageView的(getActivity());
        ImageView的lcIcon6 =新ImageView的(getActivity());
        ImageView的lcIcon7 =新ImageView的(getActivity());
        ImageView的lcIcon8 =新ImageView的(getActivity());
        ImageView的lcIcon9 =新ImageView的(getActivity());

        lcIcon1.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_camera));
        lcIcon2.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_picture));
        lcIcon3.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_video));
        lcIcon4.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_location_found));
        lcIcon5.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_headphones));
        lcIcon6.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_camera));
        lcIcon7.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_picture));
        lcIcon8.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_video));
        lcIcon9.setImageDrawable(getResources()getDrawable(R.drawable.ic_action_location_found));

        //构建自定义选项另一个菜单
        FloatingActionMenu leftCenterMenu =新FloatingActionMenu.Builder(getActivity())。addSubActionView(lCSubBuilder.setContentView(lcIcon1,blueContentParams).build())。addSubActionView(lCSubBuilder.setContentView(lcIcon2,blueContentParams).build())。addSubActionView(lCSubBuilder.setContentView (lcIcon3,blueContentParams).build())。addSubActionView(lCSubBuilder.setContentView(lcIcon4,blueContentParams).build())。addSubActionView(lCSubBuilder.setContentView(lcIcon5,blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon6,blueContentParams).build())。addSubActionView(lCSubBuilder.setContentView(lcIcon7,blueContentParams).build())。addSubActionView(lCSubBuilder.setContentView(lcIcon8,blueContentParams).build())。addSubActionView (lCSubBuilder.setContentView(lcIcon9, blueContentParams).build()).setRadius(redActionMenuRadius).setStartAngle(0).setEndAngle(360).attachTo(leftCenterButton).build();

        返回伏;
    }

}
 

解决方案

以下变化所提出的 FloatingActionButton FloatingActionMenu 类,允许一个特定的ViewGroup被设定为容器两者。测试是用一个类似但更简单的版本的code您发布而成,所以你可能需要作出调整你的设置参数。

该库code假定按钮和菜单都出现在上面的活动内容查看,一切都在其中。添加在 FloatingActionButton 类容器的ViewGroup成员允许我们指定一个孩子的ViewGroup是父母代替。请注意,以下更改只会工作,如果一个 FloatingActionButton 是用来与菜单。

在增加的 FloatingActionButton 类:

 公共类FloatingActionButton扩展的FrameLayout {
    ...
    私人的ViewGroup containerView;
    ...
    公共FloatingActionButton(活动活动,
                                的LayoutParams的LayoutParams,
                                INT主题,
                                可绘制backgroundDrawable,
                                INT位置,
                                查看内容查看,
                                FrameLayout.LayoutParams contentParams
                                //注意增加下列内容
                                //构造函数这里参数
                                ,ViewGroup中containerView){
        ...
        setClickable(真正的);

        //此行是新的。构造函数的其余部分是相同的。
        this.containerView = containerView;

        附加(的LayoutParams);
    }
    ...
    公共查看getActivityContentView(){
        如果(containerView == NULL){
            返程((活动)的getContext())
                。.getWindow()getDecorView()findViewById(android.R.id.content)。
        } 其他 {
            返回containerView;
        }
    }

    公众的ViewGroup getContainerView(){
        返回containerView;
    }

    //下面设定器不是严格必需的,但也可以是使用
    //如果你想切换按钮的和菜单的Z顺序放置
    公共无效setContainerView(ViewGroup中containerView){
        this.containerView = containerView;
    }
    ...
    公共静态类生成器{
        ...
        私人的ViewGroup containerView;
        ...
        公共生成器setContainerView(ViewGroup中containerView){
            this.containerView = containerView;
            回到这一点;
        }

        公共FloatingActionButton版本(){
            返回新FloatingActionButton(活动,
                                            的LayoutParams,
                                            主题,
                                            backgroundDrawable,
                                            位置,
                                            内容查看,
                                            contentParams,
                                            //新说法
                                            containerView);
        }
    }
    ...
}
 

和更改到 FloatingActionMenu 类:

 公共类FloatingActionMenu {
    ...
    公共查看getActivityContentView(){
        如果(mainActionView的instanceof FloatingActionButton和放大器;&安培;
            ((FloatingActionButton)mainActionView).getContainerView()!= NULL){
            返回((FloatingActionButton)mainActionView).getContainerView();
        } 其他 {
            返程((活动)mainActionView.getContext())
                。.getWindow()getDecorView()findViewById(android.R.id.content)。
        }
    }
    ...
}
 

然后,在片段的 onCreateView()方法,我们需要添加一个调用新的 setContainerView()的方法 FloatingActionButton.Builder 类。

 公共类HomeFragment扩展片段{
    ...
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState)
    {
        ...
        FloatingActionButton leftCenterButton =新FloatingActionButton.Builder(getActivity())
            .setContentView(fabIconStar,空)
            .setBackgroundDrawable(R.drawable.ic_launcher)
            .setPosition(FloatingActionButton.POSITION_TOP_CENTER)
            .setLayoutParams(starParams)
            //新的方法调用此添加
            .setContainerView(集装箱)
            。建立();
        ...
    }
    ...
}
 

I have a navigation Drawer in my App. If i select a particular item and slide the navigation drawer. The Fragment overlaps the navigation Drawer.

I Tried to add popBackStackImmediate(); But it is of no use. This is how it looks like

Activity

public class Activity extends MainActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onNavigationDrawerItemSelected(int position)
    {
        // TODO Auto-generated method stub
        if (position == 1)
        {
            HomeFragment Frag = new HomeFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.container, Frag);
            transaction.commit();
        }
        else if (position == 2)
        {

        }
    }
    public void onDrawerOpened(View drawerview)
    {
        getFragmentManager().popBackStackImmediate();
    }

Navigation Fragment

public class NavigationDrawerFragment extends Fragment implements NavigationDrawerCallbacks
{
    private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
    private static final String PREFERENCES_FILE = "my_app_settings"; 
    private NavigationDrawerCallbacks mCallbacks;
    private RecyclerView mDrawerList;
    private View mFragmentContainerView;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mActionBarDrawerToggle;
    private boolean mUserLearnedDrawer;
    private boolean mFromSavedInstanceState;
    private int mCurrentSelectedPosition;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
        mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mDrawerList.setLayoutManager(layoutManager);
        mDrawerList.setHasFixedSize(true);

        final List<NavigationItem> navigationItems = getMenu();
        NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(navigationItems);
        adapter.setNavigationDrawerCallbacks(this);
        mDrawerList.setAdapter(adapter);
        selectItem(mCurrentSelectedPosition);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mUserLearnedDrawer = Boolean.valueOf(readSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "false"));
        if (savedInstanceState != null)
        {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            mCallbacks = (NavigationDrawerCallbacks) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
        }
    }

    public ActionBarDrawerToggle getActionBarDrawerToggle()
    {
        return mActionBarDrawerToggle;
    }

    public void setActionBarDrawerToggle(ActionBarDrawerToggle actionBarDrawerToggle)
    {
        mActionBarDrawerToggle = actionBarDrawerToggle;
    }

    public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar)
    {
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
        mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close)
        {
            @Override
            public void onDrawerClosed(View drawerView)
            {
                super.onDrawerClosed(drawerView);               
                if (!isAdded())
                    return;
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerOpened(View drawerView)
            {
                super.onDrawerOpened(drawerView);
                if (!isAdded())
                    return;
                if (!mUserLearnedDrawer)
                {
                    mUserLearnedDrawer = true;
                    saveSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "true");
                }               
                getActivity().invalidateOptionsMenu();
            }
        };

        if (!mUserLearnedDrawer && !mFromSavedInstanceState)
            mDrawerLayout.openDrawer(mFragmentContainerView);

        mDrawerLayout.post(new Runnable()
        {
            @Override
            public void run()
            {
                mActionBarDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
    }

    public void openDrawer()
    {
        mDrawerLayout.openDrawer(mFragmentContainerView);
    }

    public void closeDrawer()
    {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        mCallbacks = null;
    }

    public List<NavigationItem> getMenu()
    {
        List<NavigationItem> items = new ArrayList<NavigationItem>();
        items.add(new NavigationItem("Home", getResources().getDrawable(R.drawable.play)));
        items.add(new NavigationItem("News", getResources().getDrawable(R.drawable.play)));
        items.add(new NavigationItem("Videos", getResources().getDrawable(R.drawable.play)));
        items.add(new NavigationItem("Playlist", getResources().getDrawable(R.drawable.play)));
        return items;
    }

    void selectItem(int position)
    {
        mCurrentSelectedPosition = position;
        if (mDrawerLayout != null)
        {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null)
        {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
        ((NavigationDrawerAdapter) mDrawerList.getAdapter()).selectPosition(position);
    }

    public boolean isDrawerOpen()
    {
        return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        mActionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
    }

    @Override
    public void onNavigationDrawerItemSelected(int position)
    {
        mCallbacks.onNavigationDrawerItemSelected(position);
        selectItem(position);

    }

    public DrawerLayout getDrawerLayout()
    {
        return mDrawerLayout;
    }

    public void setDrawerLayout(DrawerLayout drawerLayout)
    {
        mDrawerLayout = drawerLayout;
    }
}

Activity Layout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">

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

        <!-- android:layout_marginTop="?android:attr/actionBarSize"-->
        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.RemoteIt.client.activity.drawer.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"/>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

HomeFragment

public class HomeFragment extends Fragment
{

    public HomeFragment()
    {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {       View v = inflater.inflate(R.layout.home, container, false);

        int redActionButtonSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_size);
        int redActionButtonMargin = getResources().getDimensionPixelOffset(R.dimen.action_button_margin);
        int redActionButtonContentSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_content_size);
        int redActionButtonContentMargin = getResources().getDimensionPixelSize(R.dimen.red_action_button_content_margin);
        int redActionMenuRadius = getResources().getDimensionPixelSize(R.dimen.red_action_menu_radius);
        int blueSubActionButtonSize = getResources().getDimensionPixelSize(R.dimen.blue_sub_action_button_size);
        int blueSubActionButtonContentMargin = getResources().getDimensionPixelSize(R.dimen.blue_sub_action_button_content_margin);

        ImageView fabIconStar = new ImageView(getActivity());
        fabIconStar.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_important));

        FloatingActionButton.LayoutParams starParams = new FloatingActionButton.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        starParams.setMargins(redActionButtonMargin, redActionButtonMargin, redActionButtonMargin, redActionButtonMargin);
        fabIconStar.setLayoutParams(starParams);

        FloatingActionButton.LayoutParams fabIconStarParams = new FloatingActionButton.LayoutParams(redActionButtonContentSize, redActionButtonContentSize);
        fabIconStarParams.setMargins(redActionButtonContentMargin, redActionButtonContentMargin, redActionButtonContentMargin, redActionButtonContentMargin);

        FloatingActionButton leftCenterButton = new FloatingActionButton.Builder(getActivity()).setContentView(fabIconStar, fabIconStarParams).setBackgroundDrawable(R.drawable.button_action_red_selector).setPosition(FloatingActionButton.POSITION_TOP_CENTER).setLayoutParams(starParams).build();

        // Set up customized SubActionButtons for the right center menu
        SubActionButton.Builder lCSubBuilder = new SubActionButton.Builder(getActivity());
        lCSubBuilder.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_action_blue_selector));

        FrameLayout.LayoutParams blueContentParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        blueContentParams.setMargins(blueSubActionButtonContentMargin, blueSubActionButtonContentMargin, blueSubActionButtonContentMargin, blueSubActionButtonContentMargin);
        lCSubBuilder.setLayoutParams(blueContentParams);
        // Set custom layout params
        FrameLayout.LayoutParams blueParams = new FrameLayout.LayoutParams(blueSubActionButtonSize, blueSubActionButtonSize);
        lCSubBuilder.setLayoutParams(blueParams);

        ImageView lcIcon1 = new ImageView(getActivity());
        ImageView lcIcon2 = new ImageView(getActivity());
        ImageView lcIcon3 = new ImageView(getActivity());
        ImageView lcIcon4 = new ImageView(getActivity());
        ImageView lcIcon5 = new ImageView(getActivity());
        ImageView lcIcon6 = new ImageView(getActivity());
        ImageView lcIcon7 = new ImageView(getActivity());
        ImageView lcIcon8 = new ImageView(getActivity());
        ImageView lcIcon9 = new ImageView(getActivity());

        lcIcon1.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_camera));
        lcIcon2.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_picture));
        lcIcon3.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_video));
        lcIcon4.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_location_found));
        lcIcon5.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_headphones));
        lcIcon6.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_camera));
        lcIcon7.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_picture));
        lcIcon8.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_video));
        lcIcon9.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_location_found));

        // Build another menu with custom options
        FloatingActionMenu leftCenterMenu = new FloatingActionMenu.Builder(getActivity()).addSubActionView(lCSubBuilder.setContentView(lcIcon1, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon2, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon3, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon4, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon5, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon6, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon7, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon8, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon9, blueContentParams).build()).setRadius(redActionMenuRadius).setStartAngle(0).setEndAngle(360).attachTo(leftCenterButton).build();

        return v;
    }

}

解决方案

The following changes were made to the FloatingActionButton and FloatingActionMenu classes to allow a specific ViewGroup to be set as the container for both. Tests were made with a similar but simpler version of the code you've posted, so you may need to make adjustments to your setup parameters.

The library code assumes that the Button and Menu are to appear on top of an Activity's content View, and everything therein. The addition of a container ViewGroup member in the FloatingActionButton class allows us to specify a child ViewGroup to be the parent instead. Please note that the following changes will only work if a FloatingActionButton is used with the Menu.

The additions to the FloatingActionButton class:

public class FloatingActionButton extends FrameLayout {
    ...
    private ViewGroup containerView;
    ...
    public FloatingActionButton(Activity activity, 
                                LayoutParams layoutParams, 
                                int theme,
                                Drawable backgroundDrawable,
                                int position,
                                View contentView,
                                FrameLayout.LayoutParams contentParams      
                                // Note the addition of the following
                                // constructor parameter here
                                , ViewGroup containerView) {
        ...
        setClickable(true);

        // This line is new. The rest of the constructor is the same.
        this.containerView = containerView;     

        attach(layoutParams);
    }
    ...
    public View getActivityContentView() {
        if(containerView == null) {
            return ((Activity)getContext())
                .getWindow().getDecorView().findViewById(android.R.id.content);
        } else {
            return containerView;
        }
    }

    public ViewGroup getContainerView() {
        return containerView;
    }

    // The following setter is not strictly necessary, but may be of use
    // if you want to toggle the Button's and Menu's z-order placement
    public void setContainerView(ViewGroup containerView) {
        this.containerView = containerView;
    }
    ...
    public static class Builder {
        ...
        private ViewGroup containerView;
        ...
        public Builder setContainerView(ViewGroup containerView) {
            this.containerView = containerView;
            return this;
        }

        public FloatingActionButton build() {
            return new FloatingActionButton(activity,
                                            layoutParams,
                                            theme,
                                            backgroundDrawable,
                                            position,
                                            contentView,
                                            contentParams,
                                            // New argument
                                            containerView);
        }
    }
    ...
}

And the changes to the FloatingActionMenu class:

public class FloatingActionMenu {
    ...
    public View getActivityContentView() {
        if(mainActionView instanceof FloatingActionButton && 
            ((FloatingActionButton) mainActionView).getContainerView() != null) {
            return ((FloatingActionButton) mainActionView).getContainerView();
        } else {
            return ((Activity)mainActionView.getContext())
                .getWindow().getDecorView().findViewById(android.R.id.content);
        }
    }
    ...
}

Then, in the Fragment's onCreateView() method, we need to add a call the new setContainerView() method of the FloatingActionButton.Builder class.

public class HomeFragment extends Fragment {
    ...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        ...
        FloatingActionButton leftCenterButton = new FloatingActionButton.Builder(getActivity())
            .setContentView(fabIconStar, null)
            .setBackgroundDrawable(R.drawable.ic_launcher)
            .setPosition(FloatingActionButton.POSITION_TOP_CENTER)
            .setLayoutParams(starParams)
            // The new method call is added here
            .setContainerView(container)
            .build();
        ...
    }
    ...
}

这篇关于我的片段隐藏了导航抽屉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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