ListView与CoordinatorLayout中的SearchView重叠 [英] A ListView overlaps a SearchView in a CoordinatorLayout
问题描述
我在AndroidX迁移后正在修复一个应用程序,但我遇到一个非常奇怪的错误,即我的 ListView
没有加载到 onCreate
/ onStart上
/ onResume
事件:
这是我的代码:
other_ruins.xml
<?xml version ="1.0"encoding ="utf-8"?< LinearLayoutxmlns:android ="http://schemas.android.com/apk/res/android"xmlns:app =" http://schemas.android.com/apk/res-autoandroid:layout_width =" match_parent"android:layout_height =" match_parent"android:orientation =" vertical">< androidx.coordinatorlayout.widget.CoordinatorLayoutandroid:layout_weight =" 1"android:layout_width =" match_parent"android:layout_height =" match_parent"><include layout="@layout/search_container";/><列表视图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.AdViewandroid: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"?>< FrameLayoutxmlns:app =" http://schemas.android.com/apk/res-autoxmlns: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.Toolbarandroid:id ="@@ id/toolbar"app:theme =" @ style/ToolBarStyle"android:layout_width =" match_parent"android:layout_height ="?attr/actionBarSize"android:background =" @ color/colorPrimary"/>< com.miguelcatalan.materialsearchview.MaterialSearchViewandroid:id ="@@ id/search_view"android:layout_width =" match_parent"android:layout_height =" wrap_content"/></FrameLayout>
关于,我也使用 AppBarLayout
测试了 search_container ,并使该应用崩溃了;因此,这不是解决方案.
任何想法我该如何解决?为什么会这样呢?如果您知道如何解决上一个问题,并以某种方式在 onCreate
/ onStart
/ onResume
上调用 getView
事件事件是另一种解决方案.
在我的情况下,我需要将 FrameLayout
包裹在 AppBarLayout
上,以使其正常工作.我没有做,它复制了工具栏,创建了一个奇怪的阴影.这是最终的更改:
<?xml version ="1.0"encoding ="UTF-8"?>< com.google.android.material.appbar.AppBarLayoutxmlns:app =" http://schemas.android.com/apk/res-autoxmlns:android ="http://schemas.android.com/apk/res/android"android:id =" @ + id/toolbar_container"android:layout_width =" match_parent"android:layout_height =" wrap_content">< FrameLayoutandroid:layout_width =" match_parent"android:layout_height =" wrap_content">< androidx.appcompat.widget.Toolbarandroid:id ="@@ id/toolbar"app:theme =" @ style/ToolBarStyle"android:layout_width =" match_parent"android:layout_height ="?attr/actionBarSize"android:background =" @ color/colorPrimary"/>< com.miguelcatalan.materialsearchview.MaterialSearchViewandroid:id ="@@ id/search_view"android:layout_width =" match_parent"android:layout_height =" wrap_content"/></FrameLayout></com.google.android.material.appbar.AppBarLayout>
其中只有一个会破坏应用程序.
I'm fixing an app after the AndroidX migration, and I had a very odd bug that my ListView
was not being loaded on the onCreate
/onStart
/onResume
events:
A ListView is not displaying any data on OnCreate/OnStart after AndroidX migration
By matter of chance, I decided to "simplify" my layout and removed a LinearLayout
that contained the ListView
. After this change, the data was loaded properly because it seems LinearLayout
was somehow blocking the getView
event. However, after this change, the SearchView
is being overlapped by the ListView
, and I don't know how to put them in the right order (first the SearchView
followed by the ListView
).
This is how it looks now:
This is my code:
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>
Regarding, the search_container I tested also using the AppBarLayout
and crashed the app; therefore, it's not a solution.
Any idea how can I fix it? And why is this happening? If you know how to fix the previous issue and somehow call the getView
event on the onCreate
/onStart
/onResume
events is another solution.
In my case, I needed to wrap my FrameLayout
with a AppBarLayout
make it work properly, when I didn´t do it, it duplicates the toolbar creating an strange shadow. This is the final change:
<?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:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
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>
</com.google.android.material.appbar.AppBarLayout>
Only one of them breaks the app.
这篇关于ListView与CoordinatorLayout中的SearchView重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!