Android MapView导致曲面更新

问题描述

我在自定义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
    }
}

片段地图

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

解决方法

由于this question,我找到了解决方法。将MapView.layerType设置为hardware降低刷新率。 这只是解决方法,而不是正确的解决方案!

<com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layerType="hardware"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/map_change_size_button"/>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...