Android ViewPager在多个片段中不显示地图v2
我在fragment inside view pager中显示地图,带有细节片段,用于动态创建片段的地图。我使用单个活动,因此每个屏幕都由一个片段表示。NewsDetails片段包括一个ViewPager(地图) 当我最初导航到NewsDetailsFragment时,地图被加载,现在如果我加载NewsDetailsFragment的一个新实例,就像在滚动页面上一样,我将显示我的下一条记录。第二个NewsDetails片段中的ViewPager不会显示地图。在LogCat中,日志显示一切正常,地图正在显示,标记已设置。但在地图上显示黑屏的地方 如果有1个已加载的地图ViewPager,则任何更多的NewsDetailsFragment都不会显示其地图,只有当我按Back直到没有NewsDetailsFragment(因此没有正在运行的ViewPager)时,只有当我打开NewsDetailsFragment的新实例时,它才会加载地图 请解决我的问题Android ViewPager在多个片段中不显示地图v2,android,android-fragments,android-viewpager,google-maps-android-api-2,Android,Android Fragments,Android Viewpager,Google Maps Android Api 2,我在fragment inside view pager中显示地图,带有细节片段,用于动态创建片段的地图。我使用单个活动,因此每个屏幕都由一个片段表示。NewsDetails片段包括一个ViewPager(地图) 当我最初导航到NewsDetailsFragment时,地图被加载,现在如果我加载NewsDetailsFragment的一个新实例,就像在滚动页面上一样,我将显示我的下一条记录。第二个NewsDetails片段中的ViewPager不会显示地图。在LogCat中,日志显示一切正常,地
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();
}
其中fragment是viewPagerFragment的实例
在xml中
<LinearLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="250dp" />
这是一个已知问题,请查看此链接。我也有同样的问题,看看这个链接。这就解决了问题。假设,我有4个片段。在第2和第3个片段上,我显示地图,在其余两个片段上,我不显示地图。Senerio是:当我将从第1个片段滚动到第2个片段时,在地图的位置上,显示黑屏。然后,我再次从第2个滚动到第1个,从第1个滚动到第2个,地图显示出来。嗨,奥萨伊尔根,我在你提到的链接中看到了解决方案。但对我来说没有用,显示出同样的问题:(FragmentTransaction或view pager中是否有任何设置?
<LinearLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="250dp" />