我试图只在一个片段上显示我的FloatingActionButton并隐藏其余片段。我尝试了多个答案,但没有任何工作。我试过这个解决方案:
但它不起作用。我尝试在两个片段上添加按钮,一个显示,一个隐藏FAB并且可以正常工作,但是一旦我删除按钮,它就不会自动显示。这是我的代码:
hidden_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
shown_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
HiddenFragment.java:
public class HiddenFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.hidden_fragment, container, false);
final FloatingActionButton fab = ((MainActivity) getActivity()).getFloatingActionButton();
if (fab != null) {
((MainActivity) getActivity()).hideFloatingActionButton();
}
return view;
}
}
ShownFragment.java:
public class ShownFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.hidden_fragment, container, false);
final FloatingActionButton fab = ((MainActivity) getActivity()).getFloatingActionButton();
if (fab != null) {
((MainActivity) getActivity()).showFloatingActionButton();
}
return view;
}
}
在我的MainActivity.java中,我有这个:
public FloatingActionButton getFloatingActionButton() {
return fab;
}
public void showFloatingActionButton() {
fab.show();
}
public void hideFloatingActionButton() {
fab.hide();
}
我目前拥有它的方式并不奏效。当我启动应用程序时,启动的第一个片段是HiddenFragment。但是当我去ShownFragment时,FAB并没有出现。我尝试了不同的方法,我为每个片段添加了一个按钮,并将showFloatingActionButton()和hideFloatingActionButton()添加到按钮中,并且可以正常工作。有谁知道我做错了什么?
答案 0 :(得分:7)
要在ViewPager中显示/隐藏带有Fragments的FloatingActionButton,只需使用Select
Travel_no
,Date_time
,Country
,(Select Count(Travel_no) from #temp1 b
where
CAST(b.Date_time AS BIGINT) >= CAST(a.Date_time AS BIGINT) - 10000
and CAST(b.Date_time AS BIGINT) <= CAST(a.Date_time AS BIGINT) + 10000
and a.Country = b.Country
) 'country_within_hr_cnt'
FROM #temp1 a
GROUP BY
Travel_no , Date_time, Country
并显示所需位置的FloatingActionButton,并将其隐藏到您想要的位置。
这将进入活动:
ViewPager.OnPageChangeListener