Android MapView导致曲面更新

Android MapView导致曲面更新,android,kotlin,android-fragments,android-recyclerview,android-mapview,Android,Kotlin,Android Fragments,Android Recyclerview,Android Mapview,我在自定义的片段中使用了,并将所有生命周期方法转发到MapView。我对平板电脑上的无限表面更新(甚至在平板电脑模拟器上:haxmnexus10api30)有一个问题,但在任何手机上都没有。我删除了所有函数,除了MapView实现所需的函数,但问题仍然存在 此无限曲面更新导致RecyclerView适配器出现问题,因为onBindViewHolder由秒触发多次(~20) 我想知道如何停止MapView的这种意外行为,或者至少限制发送到RecyclerView.Adapter的事件数 MapVi

我在自定义的
片段中使用了
,并将所有生命周期方法转发到
MapView
。我对平板电脑上的无限表面更新(甚至在平板电脑模拟器上:haxmnexus10api30)有一个问题,但在任何手机上都没有。我删除了所有函数,除了
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>

多亏了你,我找到了解决办法。将
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"/>


无限曲面更新是什么意思?如何与回收视图相关?我不确定它是如何相关的,但看起来像表面更新触发适配器更新-正如您从logcat中看到的。表面更新的频率由
developer选项-显示表面更新
确定,在profiler中,当显示
MapisRefragment
时,我可以看到空闲状态下约50%的cpu使用率(其他屏幕上为0-3%)。所有这一切仅是平板电脑问题。手机使用此实现时不会出现任何问题-表面更新是正常的(如果没有任何更改,则不会进行表面更新),空闲时CPU使用率为0-3%。
<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"/>