【问题标题】:ForYouFragment Fragment doesn't updateForYouFragment 片段不更新
【发布时间】:2021-04-20 21:17:48
【问题描述】:

您好,当应用程序启动时,我对我的 forYou 片段进行了权限检查,并且您授予权限,它会进行 api 调用并获取您当前位置的天气问题是我必须切换片段以获取呼叫并切换再次在reclyerview中显示它,问题是当我授予它不执行api的位置的权限时,我必须刷新片段

    fusedLocationClient.lastLocation.addOnSuccessListener(requireActivity()) { location ->
        if (location != null) {
            currentLocation = location
            val currentLatLng = LatLng(location.latitude, location.longitude)
            val currentAddress = getAddress(location.latitude, location.longitude)
            Log.d("TAG", "klagenfurt : ${currentAddress}")
            retrofitOneCallResponse(location.latitude, location.longitude, currentAddress)
            dataService.getFavorites(this)

        }


    }
    if (ActivityCompat.checkSelfPermission(
            requireContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(
            requireActivity(),
            arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
            MapViewFragment.LOCATION_PERMISSION_REQUEST_CODE
        )

        return
    }

【问题讨论】:

    标签: android google-maps kotlin android-fragments


    【解决方案1】:

    enter image description here 这里它说,你在 requestpermisson 行上编写没有 ActivityCompat 的代码,它也不起作用

    【讨论】:

      【解决方案2】:

      创建一个这样的方法。

      private fun getLocation(){
      fusedLocationClient.lastLocation.addOnSuccessListener(requireActivity()) { location ->
              if (location != null) {
                  currentLocation = location
                  val currentLatLng = LatLng(location.latitude, location.longitude)
                  val currentAddress = getAddress(location.latitude, location.longitude)
                  Log.d("TAG", "klagenfurt : ${currentAddress}")
                  retrofitOneCallResponse(location.latitude, location.longitude, currentAddress)
                  dataService.getFavorites(this)
              }else{
               getLocation()
              }
          }
      }
      

      然后从这里

      if (ActivityCompat.checkSelfPermission(
                  requireContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
          {
              requestPermissions(
                  arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
                  MapViewFragment.LOCATION_PERMISSION_REQUEST_CODE
              )
          }else{
            getLocation()
      }
      
      
      override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray): Unit {
            if(requestCode==MapViewFragment.LOCATION_PERMISSION_REQUEST_CODE){
                if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    getLocation();
                }
            }
      }
      

      另外,覆盖片段中的onRequestPermissionsResult。并检查权限是否授予调用getLocation()

      【讨论】:

      • 我无法编写getLocation方法,因为fusedLocation需要权限检查
      • 试试这个代码模式,你会发现编译时不会出错,因为在检查条件后调用了 getLocation。 Studio 将分析流程
      • 我不明白 onRequestPermissonResult?
      • 我发了一个截图,请看这个,我试了 3 天,现在没有任何效果
      • 这是一个我已经修复的错误,请立即检查,在片段中直接调用requestPermissions,以便将结果提供给frament,否则它会进入活动状态。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多