Add codes from mapbox demo repository to test support fragment, map works in a single layer fragment

This commit is contained in:
neslihanturan 2019-08-24 11:14:39 +03:00
parent dbd1ad2ff1
commit 5575d42df2
7 changed files with 114 additions and 14 deletions

View file

@ -143,7 +143,7 @@
android:label="@string/title_activity_nearby_test_activity" /> android:label="@string/title_activity_nearby_test_activity" />
<activity <activity
android:name=".nearby.NearbyTestFragment" android:name=".nearby.NearbyTestFragmentActivity"
android:label="@string/title_activity_nearby_test_fragment" /> android:label="@string/title_activity_nearby_test_fragment" />

View file

@ -15,7 +15,7 @@ import fr.free.nrw.commons.explore.SearchActivity;
import fr.free.nrw.commons.explore.categories.ExploreActivity; import fr.free.nrw.commons.explore.categories.ExploreActivity;
import fr.free.nrw.commons.nearby.NearbyTestActivity; import fr.free.nrw.commons.nearby.NearbyTestActivity;
import fr.free.nrw.commons.nearby.NearbyTestFragment; import fr.free.nrw.commons.nearby.NearbyTestFragmentActivity;
import fr.free.nrw.commons.notification.NotificationActivity; import fr.free.nrw.commons.notification.NotificationActivity;
import fr.free.nrw.commons.review.ReviewActivity; import fr.free.nrw.commons.review.ReviewActivity;
import fr.free.nrw.commons.settings.SettingsActivity; import fr.free.nrw.commons.settings.SettingsActivity;
@ -74,5 +74,5 @@ public abstract class ActivityBuilderModule {
abstract NearbyTestActivity bindNearbyTestActivity(); abstract NearbyTestActivity bindNearbyTestActivity();
@ContributesAndroidInjector @ContributesAndroidInjector
abstract NearbyTestFragment bindNearbyTestFragment(); abstract NearbyTestFragmentActivity bindNearbyTestFragment();
} }

View file

@ -42,7 +42,7 @@ public class NearbyTestActivity extends AppCompatActivity {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() { mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override @Override
public void onStyleLoaded(@NonNull Style style) { public void onStyleLoaded(@NonNull Style style) {
Log.d("NearbyTestActivity","onStyleLoaded"); Log.d("NearbyTests","Map inside activity is ready, works");
// Map is set up and the style has loaded. Now you can add data or make other map adjustments. // Map is set up and the style has loaded. Now you can add data or make other map adjustments.
} }
}); });

View file

@ -1,6 +0,0 @@
package fr.free.nrw.commons.nearby;
import fr.free.nrw.commons.theme.NavigationBaseActivity;
public class NearbyTestFragment extends NavigationBaseActivity {
}

View file

@ -0,0 +1,71 @@
package fr.free.nrw.commons.nearby;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
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 fr.free.nrw.commons.theme.NavigationBaseActivity;
public class NearbyTestFragmentActivity extends NavigationBaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_test_fragment);
// 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(this, getString(R.string.mapbox_commons_app_token));
// Create supportMapFragment
SupportMapFragment mapFragment;
if (savedInstanceState == null) {
// Create fragment
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Build mapboxMap
MapboxMapOptions options = MapboxMapOptions.createFromAttributes(this, 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
transaction.add(R.id.container, mapFragment, "com.mapbox.map");
transaction.commit();
} else {
mapFragment = (SupportMapFragment) getSupportFragmentManager().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","Map inside support fragment is ready, works");
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
});
}
});
}
}

View file

@ -18,7 +18,6 @@ import androidx.appcompat.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -40,8 +39,7 @@ import fr.free.nrw.commons.explore.categories.ExploreActivity;
import fr.free.nrw.commons.kvstore.JsonKvStore; import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.logging.CommonsLogSender; import fr.free.nrw.commons.logging.CommonsLogSender;
import fr.free.nrw.commons.nearby.NearbyTestActivity; import fr.free.nrw.commons.nearby.NearbyTestActivity;
import fr.free.nrw.commons.nearby.NearbyTestFragment; import fr.free.nrw.commons.nearby.NearbyTestFragmentActivity;
import fr.free.nrw.commons.notification.NotificationActivity;
import fr.free.nrw.commons.review.ReviewActivity; import fr.free.nrw.commons.review.ReviewActivity;
import fr.free.nrw.commons.settings.SettingsActivity; import fr.free.nrw.commons.settings.SettingsActivity;
import timber.log.Timber; import timber.log.Timber;
@ -187,7 +185,7 @@ public abstract class NavigationBaseActivity extends BaseActivity
return true; return true;
case R.id.action_nearby_test_fragment: case R.id.action_nearby_test_fragment:
drawerLayout.closeDrawer(navigationView); drawerLayout.closeDrawer(navigationView);
startActivityWithFlags(this, NearbyTestFragment.class, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT, startActivityWithFlags(this, NearbyTestFragmentActivity.class, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT,
Intent.FLAG_ACTIVITY_SINGLE_TOP); Intent.FLAG_ACTIVITY_SINGLE_TOP);
return true; return true;
case R.id.action_feedback: case R.id.action_feedback:

View file

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fragment_below_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center_horizontal" />
<androidx.cardview.widget.CardView
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/fragment_below_textview"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="2dp"
app:cardElevation="@dimen/cardview_default_elevation">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.cardview.widget.CardView>
</RelativeLayout>