【发布时间】:2020-10-27 13:20:20
【问题描述】:
我在我的自定义Fragment 中使用MapView,并将所有生命周期方法转发给MapView。我在平板电脑上遇到无限表面更新问题(即使在平板电脑模拟器上:HAXM Nexus 10 API30),但在任何手机上都没有。我删除了所有功能,除了MapView 实现所需的功能,但问题仍然存在。
此无限表面更新导致 RecyclerView 适配器出现问题,因为 onBindViewHolder 被触发多次 (~20) 秒。
我想知道如何阻止MapView 的这种意外行为或至少限制发送到RecyclerView.Adapter 的事件数量。
MapViewFragment:
class MapIssueFragment: Fragment(R.layout.fragment_map), OnMapReadyCallback {
companion object {
private const val TAG = "MapIssueFragment"
fun newInstance(): MapIssueFragment{
return MapIssueFragment()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i(TAG, "onCreate")
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.i(TAG, "onViewCreated")
map.apply {
onCreate(savedInstanceState)
getMapAsync(this@MapIssueFragment)
}
}
override fun onStart() {
super.onStart()
map.onStart()
Log.i(TAG, "onStart")
}
override fun onResume() {
super.onResume()
map.onResume()
Log.i(TAG, "onResume")
}
override fun onPause() {
super.onPause()
map.onPause()
Log.i(TAG, "onPause")
}
override fun onStop() {
super.onStop()
map.onStop()
Log.i(TAG, "onStop")
}
override fun onDestroy() {
super.onDestroy()
map.onDestroy()
Log.i(TAG, "onDestroy")
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
map.onSaveInstanceState(outState)
Log.i(TAG, "onSaveInstanceState")
}
override fun onLowMemory() {
super.onLowMemory()
map.onLowMemory()
Log.i(TAG, "onLowMemory")
}
private lateinit var googleMap: GoogleMap
override fun onMapReady(googleMap: GoogleMap) {
this.googleMap = googleMap
}
}
fragment_map:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/requirements_container"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/requirements_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.50"
android:visibility="visible"
>
<TextView
android:id="@+id/requirements_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/no_requirements"
android:gravity="center"
android:visibility="gone"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/requirements_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_requirement_detail"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
即使没有 RecyclerView 适配器,我也可以在 Logcat 中看到错误,您可以在其中看到刷新率:
2020-10-27 13:47:50.908 E/RecyclerView: No adapter attached; skipping layout
2020-10-27 13:47:50.914 E/RecyclerView: No adapter attached; skipping layout
2020-10-27 13:47:50.963 E/RecyclerView: No adapter attached; skipping layout
2020-10-27 13:47:50.972 E/RecyclerView: No adapter attached; skipping layout
2020-10-27 13:47:51.025 E/RecyclerView: No adapter attached; skipping layout
2020-10-27 13:47:51.029 E/RecyclerView: No adapter attached; skipping layout
2020-10-27 13:47:51.075 E/RecyclerView: No adapter attached; skipping layout
2020-10-27 13:47:51.092 E/RecyclerView: No adapter attached; skipping layout
【问题讨论】:
-
无限表面更新是什么意思?与
RecyclerView有什么关系? -
我不确定它的相关性,但看起来像表面更新触发适配器更新 - 正如您从 logcat 中看到的那样。表面更新频率由
developer option - show surface updates确定,在分析器中,当显示MapIssueFragment时(在其他屏幕上为 0-3%),我可以看到空闲时的 CPU 使用率约为 50%。 -
所有这些只是平板电脑的问题。手机使用此实现没有任何问题 - 表面更新是正常的(如果没有任何变化,则不进行表面更新),空闲时 CPU 使用率为 0-3%。
标签: android kotlin android-fragments android-recyclerview android-mapview