如何在单击浮动操作按钮时关闭片段

时间:2017-02-12 18:31:12

标签: android android-fragments floating-action-button

我正在尝试实现一个fab动作,当它一旦点击就会启动一个片段并再次触摸时会关闭片段。我怎么能这样做。

这是我的代码:

final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();*/

            NewOrdersFragment newOrdersFragment = new NewOrdersFragment();

            if(isOpen){
                fab.startAnimation(FabRanticlockwise);
                FragmentManager orderManager = getSupportFragmentManager();
                orderManager.beginTransaction().remove(newOrdersFragment);
                isOpen = false;
            }

            else{
                fab.startAnimation(FabRClockwise);
                FragmentManager orderManager = getSupportFragmentManager();
                orderManager.beginTransaction().replace(R.id.content_buyer_main_area, newOrdersFragment).commit();
                isOpen = true;
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

list()

beginTransaction().remove(yourFragment)

对你的情况很方便。