使用ListView锚定FloatingActionButton

时间:2016-01-11 17:12:37

标签: android listview floating-action-button

我正在尝试使用FAB创建一个列表,以便在滚动列表时获得FAB效果(显示/隐藏)。

问题是我的ListView和FAB位于不同的文件中。

使用FAB按钮布局:          

 Private Sub DownloadExcel()

    Response.Clear()

    'Dim dt As DataTable = TryCast(ViewState("GridData"), DataTable)

    Grid_Bad_Meters.AllowPaging = False
    Grid_Bad_Meters.AllowSorting = False

    'Grid_Bad_Meters.DataSource = dt
    'Grid_Bad_Meters.DataBind()

    Dim sfile As String = "Communication_Failures" & Now.Ticks

    Response.AddHeader("content-disposition", "attachment;filename=" & sfile & ".xls")
    Response.Charset = ""

    ' If you want the option to open the Excel file without saving then 
    ' comment out the line below 
    ' Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = "application/vnd.ms-excel"
    Dim stringWrite As New System.IO.StringWriter()
    Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
    Grid_Bad_Meters.RenderControl(htmlWrite)
    Response.Write(stringWrite.ToString())
    Response.End()

    'Grid_Bad_Meters.AllowPaging = True
    'Grid_Bad_Meters.AllowSorting = True
    'GridView1.DataSource = dt

    'GridView1.DataBind()


End Sub

布局item_list.xml

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/item_list" />
    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/addForwardFilter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:layout_anchor="@id/custom_list"
        app:layout_anchorGravity="bottom|right|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add" />

</android.support.design.widget.CoordinatorLayout>

布局custom_list_content.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="mypackage.test.fragment.FItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ItemListActivity"
    tools:layout="@layout/custom_list_content" />

当我尝试锚定时,我得到以下内容:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/custom_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />

</FrameLayout>

此示例有效:

java.lang.IllegalStateException: Could not find CoordinatorLayout descendant view with id mypackage.test:id/custom_list to anchor view android.support.design.widget.FloatingActionButton

任何提示?

2 个答案:

答案 0 :(得分:2)

错误非常明确:FloatingActionButton无法找到它应该锚定的id。这是因为id位于 custom_list_content.xml 中,仅在 tools:layout 属性中引用。 tools 命名空间仅用于设计时间(IDE可以在我们的示例中使用片段中的正确内容进行预览),并且不用于运行时。基本上,在运行时,布局最终会像这样:

<FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/item_list"
            android:name="mypackage.test.fragment.FItemListFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/addForwardFilter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:layout_anchor="@id/custom_list"
        app:layout_anchorGravity="bottom|right|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add" />

</android.support.design.widget.CoordinatorLayout>

因此,FloatingActionButton无法找到 custom_list 。在固定示例中,所有内容都放在同一个文件中,包括锚ID,因此没有问题。

答案 1 :(得分:0)

我认为CoordinatorLayout不适用于ListViews。您必须改为使用RecyclerView