[android] #5738 - default navigation drawer example

[android] #5738 - migrated to MapFragment approach & AppCompat compatible

[android] #5378 - make drawer work with inner class fragments

[android] #5738 - navigation drawer proposition

[android] #5738 - cleanup
This commit is contained in:
Tobrun
2016-07-21 08:23:51 +02:00
parent 494e6849c2
commit ea2ee649fa
17 changed files with 395 additions and 22 deletions

View File

@@ -1504,12 +1504,12 @@ public class MapView extends FrameLayout {
LatLng tapLatLng = fromScreenLocation(new PointF(xCoordinate, yCoordinate));
// NaN and Infinite checks to prevent JSON errors at send to server time
if (Double.isNaN(tapLatLng.getLatitude()) || Double.isNaN(tapLatLng.getLongitude())) {
if (Double.isNaN(tapLatLng.getLatitude()) || Double.isNaN(tapLatLng.getLongitude())) {
Log.d(MapView.class.getSimpleName(), "trackGestureEvent() has a NaN lat or lon. Returning.");
return;
}
if (Double.isInfinite(tapLatLng.getLatitude()) || Double.isInfinite(tapLatLng.getLongitude())) {
if (Double.isInfinite(tapLatLng.getLatitude()) || Double.isInfinite(tapLatLng.getLongitude())) {
Log.d(MapView.class.getSimpleName(), "trackGestureEvent() has an Infinite lat or lon. Returning.");
return;
}
@@ -1536,12 +1536,12 @@ public class MapView extends FrameLayout {
LatLng tapLatLng = fromScreenLocation(new PointF(xCoordinate, yCoordinate));
// NaN and Infinite checks to prevent JSON errors at send to server time
if (Double.isNaN(tapLatLng.getLatitude()) || Double.isNaN(tapLatLng.getLongitude())) {
if (Double.isNaN(tapLatLng.getLatitude()) || Double.isNaN(tapLatLng.getLongitude())) {
Log.d(MapView.class.getSimpleName(), "trackGestureDragEndEvent() has a NaN lat or lon. Returning.");
return;
}
if (Double.isInfinite(tapLatLng.getLatitude()) || Double.isInfinite(tapLatLng.getLongitude())) {
if (Double.isInfinite(tapLatLng.getLatitude()) || Double.isInfinite(tapLatLng.getLongitude())) {
Log.d(MapView.class.getSimpleName(), "trackGestureDragEndEvent() has an Infinite lat or lon. Returning.");
return;
}
@@ -1822,7 +1822,7 @@ public class MapView extends FrameLayout {
return false;
}
requestDisallowInterceptTouchEvent(true);
// requestDisallowInterceptTouchEvent(true);
// reset tracking modes if gesture occurs
resetTrackingModesIfRequired();
@@ -2638,11 +2638,11 @@ public class MapView extends FrameLayout {
* @param callback The callback object that will be triggered when the map is ready to be used.
*/
@UiThread
public void getMapAsync(@NonNull final OnMapReadyCallback callback) {
if (!mInitialLoad) {
public void getMapAsync(final OnMapReadyCallback callback) {
if (!mInitialLoad && callback != null) {
callback.onMapReady(mMapboxMap);
} else {
if(callback!=null) {
if (callback != null) {
mOnMapReadyCallbackList.add(callback);
}
}

View File

@@ -9,7 +9,7 @@
<FrameLayout
android:id="@+id/markerViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent" />
<com.mapbox.mapboxsdk.maps.widgets.CompassView
android:id="@+id/compassView"
@@ -28,11 +28,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/bg_default_selector"
android:clickable="true"
android:contentDescription="@string/attributionsIconContentDescription"
android:padding="7dp"
android:src="@drawable/ic_info_outline_24dp_selector"
android:background="@drawable/bg_default_selector"/>
android:src="@drawable/ic_info_outline_24dp_selector" />
<com.mapbox.mapboxsdk.maps.widgets.MyLocationView
android:id="@+id/userLocationView"

View File

@@ -24,7 +24,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.infowindow.InfoWindowActivity"
android:description="@string/description_info_window"
@@ -187,7 +186,6 @@
android:name="@string/category"
android:value="@string/category_annotation" />
</activity>
<activity
android:name=".activity.annotation.PolylineActivity"
android:description="@string/description_polyline"
@@ -261,7 +259,6 @@
android:name="@string/category"
android:value="@string/category_maplayout" />
</activity>
<activity
android:name=".activity.annotation.MarkerViewActivity"
android:description="@string/description_view_marker"
@@ -278,7 +275,6 @@
android:name="@string/category"
android:value="@string/category_annotation" />
</activity>
<activity
android:name=".activity.navigation.LocationPickerActivity"
android:description="@string/description_location_picker"
@@ -287,7 +283,6 @@
android:name="@string/category"
android:value="@string/category_navigation" />
</activity>
<activity
android:name=".activity.fragment.ViewPagerActivity"
android:description="@string/description_viewpager"
@@ -296,6 +291,15 @@
android:name="@string/category"
android:value="@string/category_fragment" />
</activity>
<activity
android:name=".activity.maplayout.NavigationDrawerActivity"
android:description="@string/description_navigation_drawer"
android:label="@string/title_activity_navigation_drawer"
android:theme="@style/AppTheme.ActionBar.Transparent">
<meta-data
android:name="@string/category"
android:value="@string/category_fragment" />
</activity>
<!-- Configuration Settings -->
<meta-data
@@ -306,12 +310,12 @@
android:value="sk.eyJ1IjoiYmxlZWdlIiwiYSI6InNpcml1c2x5In0.KyT-boMyC_xZYTYojTc8zg" />
<!-- Comment out this setting to switch to external storage (and disable internal) in your app -->
<!--<meta-data-->
<!--android:name="com.mapbox.SetStorageExternal"-->
<!--android:value="true" />-->
<!-- <meta-data -->
<!-- android:name="com.mapbox.SetStorageExternal" -->
<!-- android:value="true" /> -->
<service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService" />
</application>
</manifest>
</manifest>

View File

@@ -0,0 +1,266 @@
package com.mapbox.mapboxsdk.testapp.activity.maplayout;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapFragment;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.testapp.R;
public class NavigationDrawerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationDrawerFragment navigationDrawerFragment;
getFragmentManager()
.beginTransaction()
.add(R.id.navigation_drawer, navigationDrawerFragment = new NavigationDrawerFragment())
.commit();
navigationDrawerFragment.setUp(this,
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout),
getSupportActionBar());
}
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
MapboxMapOptions options = new MapboxMapOptions();
switch (position) {
case 0:
options.styleUrl(Style.DARK);
options.camera(new CameraPosition.Builder()
.target(new LatLng(39.913271, 116.413891))
.zoom(12)
.build());
break;
case 1:
options.styleUrl(Style.LIGHT);
options.camera(new CameraPosition.Builder()
.target(new LatLng(31.227831, 121.449076))
.zoom(6)
.build());
break;
case 2:
options.styleUrl(Style.SATELLITE);
options.camera(new CameraPosition.Builder()
.target(new LatLng(22.294297, 114.177891))
.zoom(8)
.build());
break;
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, MapFragment.newInstance(options))
.commit();
}
public static class NavigationDrawerFragment extends Fragment {
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;
private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;
public NavigationDrawerFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
mFromSavedInstanceState = true;
}
mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
mDrawerLayout.bringChildToFront(drawerView);
mDrawerLayout.requestLayout();
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
selectItem(mCurrentSelectedPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.drawer_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(new ArrayAdapter<>(
inflater.getContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
/**
* Users of this fragment must call this method to set up the navigation drawer interactions.
*
* @param fragmentId The android:id of this fragment in its activity's layout.
* @param drawerLayout The DrawerLayout containing this fragment's UI.
*/
public void setUp(Activity activity, int fragmentId, DrawerLayout drawerLayout, ActionBar actionBar) {
mFragmentContainerView = activity.findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerLayout.setScrimColor(Color.TRANSPARENT);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
activity,
mDrawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}
getActivity().invalidateOptionsMenu();
}
};
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
Activity activity = getActivity();
if (activity != null && activity instanceof NavigationDrawerActivity) {
NavigationDrawerActivity navActivity = (NavigationDrawerActivity) activity;
navActivity.onNavigationDrawerItemSelected(position);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 B

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mapbox.mapboxsdk.testapp.activity.maplayout.NavigationDrawerActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp" />
</FrameLayout>
<FrameLayout
android:id="@+id/navigation_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</android.support.v4.widget.DrawerLayout>

View File

@@ -0,0 +1,7 @@
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:background="#cccc"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ScrimInsetsView">
<attr name="appInsetForeground" format="reference|color" />
</declare-styleable>
</resources>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="toolbar_padding_top">25dp</dimen>
</resources>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppBaseTheme">
</style>r
<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
<item name="colorPrimary">@android:color/transparent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
<style name="AppTheme.ActionBar" parent="AppTheme">
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorPrimary">@color/primary</item>
</style>
</resources>

View File

@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View File

@@ -11,4 +11,9 @@
<dimen name="toolbar_shadow">4dp</dimen>
<dimen name="locationview_background_drawable_padding">2dp</dimen>
<dimen name="locationview_padding_top">350dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="navigation_drawer_width">240dp</dimen>
<dimen name="toolbar_padding_top">0dp</dimen>
</resources>

View File

@@ -53,6 +53,7 @@
<string name="activity_offline">Offline Map</string>
<string name="activity_minmax_zoom">Min/Max Zoom</string>
<string name="activity_viewpager">ViewPager</string>
<string name="title_activity_navigation_drawer">Navigation Drawer</string>
<!-- Description -->
<string name="description_user_location_tracking">Tracks the location of the user</string>
@@ -90,6 +91,7 @@
<string name="description_dynamic_info_window_adapter">Learn how to create a dynamic custom InfoWindow</string>
<string name="description_location_picker">Use a fixed Marker to select your location</string>
<string name="description_viewpager">Use SupportMapFragments in a ViewPager</string>
<string name="description_navigation_drawer">Test for Navigation Drawer support</string>
<string name="menuitem_title_concurrent_infowindow">Concurrent Open InfoWindows</string>
<string name="menuitem_title_deselect_markers_on_tap">Deselect Markers On Tap</string>
@@ -173,4 +175,15 @@
<string name="navigation_select_location_button_text">Select Location!</string>
<string name="title_section1">Section 1</string>
<string name="title_section2">Section 2</string>
<string name="title_section3">Section 3</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_example">Example action</string>
<string name="action_settings">Settings</string>
</resources>

View File

@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppBaseTheme" />
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
@@ -10,4 +8,16 @@
<item name="android:windowBackground">@color/white</item>
</style>
<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
<item name="android:windowContentOverlay">@null</item>
<item name="windowActionBarOverlay">true</item>
<item name="colorPrimary">@android:color/transparent</item>
</style>
<style name="AppTheme.ActionBar" parent="AppTheme">
<item name="windowActionBarOverlay">false</item>
</style>
<style name="AppTheme" parent="AppBaseTheme" />
</resources>