首页 > 解决方案 > ListView 与 CoordinatorLayout 中的 SearchView 重叠

问题描述

我在 AndroidX 迁移后修复了一个应用程序,我遇到了一个非常奇怪的错误,即我的/ /事件ListView没有被加载:onCreateonStartonResume

AndroidX 迁移后,ListView 未在 OnCreate/OnStart 上显示任何数据

偶然的机会,我决定“简化”我的布局并删除一个LinearLayout包含ListView. 在此更改之后,数据已正确加载,因为它似乎LinearLayout以某种方式阻止了getView事件。但是,在此更改之后,SearchView被 重叠ListView,我不知道如何将它们按正确的顺序排列(首先是 ,SearchView然后是ListView)。

这是它现在的样子:

例子

这是我的代码:

other_ruins.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include layout="@layout/search_container" />
        <ListView
            android:id="@+id/lstOtherRuins"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:nestedScrollingEnabled="true"
            android:layout_margin="8dp"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:choiceMode="singleChoice"
            android:layout_below="@id/toolbar_container"
            android:layout_gravity="left|start" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:adSize="SMART_BANNER"
        app:adUnitId="@string/banner_ad_unit_id" />
</LinearLayout>

search_container.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<FrameLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_container"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        app:theme="@style/ToolBarStyle"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary" />

    <com.miguelcatalan.materialsearchview.MaterialSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</FrameLayout>

关于,我测试的search_containerAppBarLayout也使用了应用程序并崩溃了;因此,这不是一个解决方案。

知道如何解决吗?为什么会这样?如果您知道如何解决上一个问题并以某种方式调用// eventsgetView上的事件是另一种解决方案。onCreateonStartonResume

标签: androidandroid-layoutlistviewandroidxandroid-coordinatorlayout

解决方案


当您使用inside时,您需要将FrameLayoutinsearch_container.xml替换AppBarLayout为theAppBarLayoutToolbarToolbarCoordinatorLayout

<?xml version="1.0" encoding="UTF-8" ?>
<com.google.android.material.appbar.AppBarLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_container"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        app:theme="@style/ToolBarStyle"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary" />

    <com.miguelcatalan.materialsearchview.MaterialSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</com.google.android.material.appbar.AppBarLayout>

推荐阅读