在FragmentTransection期间隐藏/显示浮动操作按钮

时间:2016-10-21 10:34:50

标签: android floating-action-button fragmenttransaction

我有这个代码用于打开一些Message Fragment。

private void openMessage(int position) {
    MessageRecord item = data.get(position);
    item.setIsRead(true);
    item.save();
    List<MessageRecord> tmp = new ArrayList<>(data);
    updateScreen(tmp);

    MessagesActivity2 parentActivity = ((MessagesActivity2) context);
    parentActivity.fab.hide();
    FragmentTransaction ft = parentActivity.getSupportFragmentManager().beginTransaction();

    ft.addToBackStack(null);
    MessageFragment fragment1 = MessageFragment.newInstance(item);
    ft.add(R.id.frame, fragment1);

    ft.commit();

}

这条线隐藏了我的工厂。     parentActivity.fab.hide();

但是按下“返回”按钮并返回到活动视图后,我无法弄清楚如何重新打开我的FAB。

2 个答案:

答案 0 :(得分:1)

在活动中覆盖<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.highcharts.com/highcharts.js"></script> <div class="clearfix"> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#chart1" aria-controls="chart1" role="tab" data-toggle="tab">Chart 1</a></li> <li role="presentation"><a href="#chart2" aria-controls="chart2" role="tab" data-toggle="tab">Chart 2</a></li> </ul> <!-- Tab panes --> <div class="tab-content"> <div role="tabpanel" class="tab-pane active" id="chart1"> <div class="areaChartTwoWay newChart" style="height: 250px; margin: 0 auto"></div> </div> <div role="tabpanel" class="tab-pane" id="chart2"> <div class="areaChartOneWay newChart" style="height: 250px; margin: 0 auto"></div> </div> </div> </div>并根据您的要求检查片段是否为空,然后隐藏您的onBackPressed()

答案 1 :(得分:0)

不要直接在Activity上添加FAB,而是在片段(FABFragment)上添加FAB,并为您的消息添加另一个片段(MessageFragment)。

因此,每当您想要显示MessageFragment时,请使用以下代码替换FABFragment:

MessageFragment fragment1 = MessageFragment.newInstance(item); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.addToBackStack(null); ft.replace(R.id.frame, fragment1);

要在您的片段之间进行通信,请使用接口(https://developer.android.com/training/basics/fragments/communicating.html