简体   繁体   English

ForYouFragment 片段不更新

[英]ForYouFragment Fragment doesn't update

Hi i have permission check on my forYou fragment when the app starts start and you give the permission and it makes a api call and get the weather for you current location the problem is I must switch the fragment for getting the call and switch again for showing it in the reclyerview the problem is when I give the permission for the location it doesn't do the api after that, I must refresh the fragment嗨,我在应用程序启动时对我的 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
    }

Create one method like this.创建一个这样的方法。

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()
        }
    }
}

then from here然后从这里

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();
          }
      }
}

Also, override the onRequestPermissionsResult in the fragment.此外,覆盖片段中的onRequestPermissionsResult and check if permission granted the call getLocation() .并检查权限是否授予调用getLocation()

enter image description here HERE it says and you write the code without ActivityCompat on the requestpermisson line it doesn't work etiher在此处输入图像描述它说,并且您在 requestpermisson 行上编写没有 ActivityCompat 的代码它不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM