Add Support fragment inside a fragment and proves that layered fragmentw with map works

This commit is contained in:
neslihanturan 2019-08-26 16:04:57 +03:00
parent 67af6d8ae9
commit ed796b4044
4 changed files with 115 additions and 0 deletions

View file

@ -1,6 +1,14 @@
package fr.free.nrw.commons.nearby; package fr.free.nrw.commons.nearby;
import android.os.Bundle;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.theme.NavigationBaseActivity; import fr.free.nrw.commons.theme.NavigationBaseActivity;
public class NearbyTestFragmentLayersActivity extends NavigationBaseActivity { public class NearbyTestFragmentLayersActivity extends NavigationBaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_test_fragment_layers);
}
} }

View file

@ -0,0 +1,84 @@
package fr.free.nrw.commons.nearby;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
import fr.free.nrw.commons.R;
import static fr.free.nrw.commons.contributions.ContributionsFragment.CONTRIBUTION_LIST_FRAGMENT_TAG;
public class NearbyTestLayersFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_simple, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(getActivity(), getString(R.string.mapbox_commons_app_token));
// Create supportMapFragment
SupportMapFragment mapFragment;
if (savedInstanceState == null) {
// Create fragment
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
// Build mapboxMap
MapboxMapOptions options = MapboxMapOptions.createFromAttributes(getActivity(), null);
options.camera(new CameraPosition.Builder()
.target(new LatLng(-52.6885, -70.1395))
.zoom(9)
.build());
// Create map fragment
mapFragment = SupportMapFragment.newInstance(options);
// Add map fragment to parent container
getChildFragmentManager().executePendingTransactions();
transaction.add(R.id.container, mapFragment, "com.mapbox.map");
transaction.commit();
} else {
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag("com.mapbox.map");
}
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.SATELLITE, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
Log.d("NearbyTests","Fragment inside fragment with map works");
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
});
}
});
}
}

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="fr.free.nrw.commons.nearby.NearbyTestLayersFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>