高德地圖黑屏問題


高德地圖在Fragment中出現黑屏問題解決方案:

1、Fragment切換過程中不使用replace方法,使用show、hide方法即可解決每次切換地圖后黑屏問題。但是首次切換至地圖時依然會閃一下,請參照步驟2。

2、地圖Fragment首次切換時出現閃屏,在Fragment所在Activity中添加getWindow().setFormat(PixelFormat.TRANSLUCENT)解決。

具體參照以下代碼:

import android.content.Intent
import android.graphics.Color
import android.graphics.PixelFormat
import android.os.Bundle
import android.support.v4.app.FragmentTransaction
import android.view.View
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.android.app.BaseActivity
import com.android.ui.fragment.BoomFragment
import com.android.ui.fragment.HomeFragment
import com.android.ui.fragment.MapsFragment
import com.jaeger.library.StatusBarUtil
import com.sd2w.beilin.app.R
import kotlinx.android.synthetic.main.activity_main.*

/**
 * 應用啟動引導頁面
 *
 * @author 祁連山
 * @date 2017-09-18
 * @version 1.0
 */

class MainActivity : BaseActivity() {

    lateinit var locationClient: AMapLocationClient
    lateinit var clientOptions: AMapLocationClientOption

    var homeFragment = HomeFragment()
    var boomFragment = BoomFragment()
    var mapsFragment = MapsFragment()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setFormat(PixelFormat.TRANSLUCENT)

        setContentView(R.layout.activity_main)

        StatusBarUtil.setColor(this, Color.parseColor("#318df0"))
        // 默認添加首頁
        getTransaction().add(R.id.id_main_holder, homeFragment).commit()
        id_main_group.check(R.id.id_radio_home)
        id_main_group.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.id_radio_home -> {
                    id_left_back.visibility = View.VISIBLE
                    id_foo_text.text = "測試標題一"
                    getTransaction().show(homeFragment).hide(boomFragment).hide(mapsFragment).commit()
                }
                R.id.id_radio_boom -> {
                    id_left_back.visibility = View.INVISIBLE
                    id_foo_text.text = "測試標題二"
                    if (!boomFragment.isAdded) {
                        getTransaction().add(R.id.id_main_holder, boomFragment).show(boomFragment).hide(homeFragment).hide(mapsFragment).commit()
                        return@setOnCheckedChangeListener
                    }
                    getTransaction().hide(homeFragment).show(boomFragment).hide(mapsFragment).commit()
                }
                R.id.id_radio_map -> {
                    id_left_back.visibility = View.VISIBLE
                    id_foo_text.text = "測試標題三"
                    if (!mapsFragment.isAdded) {
                        getTransaction().add(R.id.id_main_holder, mapsFragment).show(mapsFragment).hide(homeFragment).hide(boomFragment).commit()
                        return@setOnCheckedChangeListener
                    }
                    getTransaction().hide(homeFragment).hide(boomFragment).show(mapsFragment).commit()
                }
            }
        }
        id_right_img.setOnClickListener {
            var it = Intent(this, ProfileActivity::class.java)
            startActivity(it)
        }
    }

    fun getTransaction(): FragmentTransaction {
        return supportFragmentManager.beginTransaction()
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM