它是如何工作的?我有如下布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/search_form_fragment"
android:name="FragmentClass"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/result_list_fragment"
android:name="FragmentClass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</LinearLayout>
请注意第二个 fragment 有 android:visibility="gone"
并且实际上它在屏幕上不可见。但是这段代码:
boolean bothVisible = firstFrag.isVisible() && secondFrag.isVisible();
返回true
,这出乎我的意料。我想知道使用 android:visibility
是否正确,因为我在文档中找不到任何关于它的信息。
最佳答案
根据 Fragment source , isVisible
定义为:
final public boolean isVisible() {
return isAdded() && !isHidden() && mView != null
&& mView.getWindowToken() != null &&
mView.getVisibility() == View.VISIBLE;
}
即,它附加到 Activity,未隐藏(通过 FragmentTransaction.hide ), View 被放大, View 附加到窗口, fragment 的内部 View 是 View.VISIBLE
。
我认为问题是为了膨胀您的 fragment ,系统创建了一个布局来保存 fragment 的 View 。这是您设置为 View.GONE
的 View ,而不是 Fragment 创建的内部 View 。
我可能会建议将您的条件更改为:
findViewById(R.id.result_list_fragment).getVisibility() == View.VISIBLE
关于android - fragment 机器人 :visibility in xml layout definition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466868/