1

我在片段內顯示地圖在視圖頁面中,具有細節片段,對於地圖我已動態創建片段。我正在使用單個活動,因此每個屏幕都由一個片段表示。 NewsDetailsFragment包含一個ViewPager(地圖)。ViewPager並非在超過1片段中顯示地圖v2

當我最初導航到NewsDetailsFragment時,地圖加載,現在如果我加載NewsDetailsFragment的新實例就像在滾動頁面上一樣,我正在顯示下一條記錄。第二個NewsDetailsFragment中的ViewPager不會顯示地圖。在LogCat中,日誌顯示一切正常,地圖顯示並設置標記。但在地圖上顯示黑屏。

如果有1個加載的地圖ViewPager,更多的NewsDetailsFragment不會顯示他們的地圖,只有當我按回來,直到我沒有NewsDetailsFragment(因此沒有ViewPagers運行),只有如果我打開一個NewsDetailsFragment的新實例,它會加載地圖。

請解決我的問題。

+0

這是一個已知的問題,借這個鏈接看看。 http://stackoverflow.com/questions/14419521/moving-mapfragment-surfaceview-causes-black-background-flickering – osayilgan

+0

我有同樣的問題,看看這個鏈接。這解決了這個問題。 https://github.com/jfeinstein10/SlidingMenu/issues/168#issuecomment-11531681 – osayilgan

+0

假設,我有4個片段。在第二和第三個片段中,我顯示地圖,剩下的兩個片斷中沒有顯示地圖.Senerio是:當我從第一個片段滾動到第二個時,此時在地圖上顯示黑屏。然後再次從第2個到第1個和第1個到第2個滾動,顯示地圖。 –

回答

0
import com.google.android.gms.maps.SupportMapFragment; 

SupportMapFragment mMapFragment = SupportMapFragment.newInstance();  
FragmentManager fm = fragment.getChildFragmentManager(); 
fm.beginTransaction().add(R.id.map, mMapFragment).commit(); 
mMapFragment.getMapAsync(this); 

@Override 
public void onMapReady(GoogleMap googleMap) { 
mGoogleMap = googleMap; 

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
    // TODO: Consider calling 
    // ActivityCompat#requestPermissions 
    // here to request the missing permissions, and then overriding 
    // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
    //           int[] grantResults) 
    // to handle the case where the user grants the permission. See the documentation 
    // for ActivityCompat#requestPermissions for more details. 
    return; 
} 
mGoogleMap.setMyLocationEnabled(true); 

buildGoogleApiClient(); 

mGoogleApiClient.connect(); 
} 

protected synchronized void buildGoogleApiClient() { 

mGoogleApiClient = new GoogleApiClient.Builder(getContext()) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 
} 

@Override 
public void onConnected(Bundle bundle) { 

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
    // TODO: Consider calling 
    // ActivityCompat#requestPermissions 
    // here to request the missing permissions, and then overriding 
    // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
    //           int[] grantResults) 
    // to handle the case where the user grants the permission. See the documentation 
    // for ActivityCompat#requestPermissions for more details. 
    return; 
} 
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
     mGoogleApiClient); 
if (mLastLocation != null) { 
    //place marker at current position 
    mGoogleMap.clear(); 
    //setLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()); 
} 


mLocationRequest = new LocationRequest(); 
mLocationRequest.setInterval(50000); //50 seconds 
mLocationRequest.setFastestInterval(30000); //30 seconds 
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
//mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter 

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 
Toast.makeText(customAdapter.getContext(), "onConnectionSuspended", Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
Toast.makeText(customAdapter.getContext(), "onConnectionFailed", Toast.LENGTH_SHORT).show(); 

}

@Override 
public void onLocationChanged(Location location) { 
sourceLat = location.getLatitude(); 
sourceLng = location.getLongitude(); 
getRestaurantDetails(); 
latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
//latLng = new LatLng(lat, lng); 
//Log.wtf("Lat lng", lat + " " + lng); 
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18)); 
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
} 

其中片段是你viewPagerFragment

的情況下在XML

<LinearLayout 
android:id="@+id/map" 
android:layout_width="match_parent" 
android:layout_height="250dp" />