android - fragment 机器人 :visibility in xml layout definition

标签 android android-layout android-fragments

它是如何工作的?我有如下布局:

<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/

相关文章:

android - Delphi 获取android combobox 选中的item 文本

java - Jar 库与 Android 库。有什么区别?

android - LinearLayout * 元素大小

android - 不同大小的图像不适合 viewpager

android - 自定义TabLayout中选定的选项卡

android - 当我点击 Activity 上的按钮时,如何更改当前 fragment 中按钮的文本?

java - 为什么我无法从 Fragment 转换为 MyOwnFragment?

android - 如何根据我在 map 上的折线的位置获取插槽

java - Apache http客户端

android - URL 提取完成时禁用进度条 : A standard scenario