Use BottomSheetDialogFragment from material design

This commit is contained in:
Alexander Sytnyk 2017-08-17 18:56:18 +03:00
parent e2100f4b04
commit 353980552d
2 changed files with 69 additions and 83 deletions

View file

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/bg_color"
android:orientation="vertical">
android:background="@drawable/bg_bottom_menu_dark">
<ScrollView
android:id="@+id/navigation_types_scroll_view"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:background="?attr/bg_color"
android:orientation="vertical">
<LinearLayout
android:id="@+id/navigation_types_container"
@ -38,28 +38,28 @@
android:textSize="@dimen/default_desc_text_size"/>
</LinearLayout>
</ScrollView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/dashboard_divider"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/dashboard_divider"/>
<FrameLayout
android:id="@+id/cancel_row"
android:layout_width="match_parent"
android:layout_height="@dimen/measure_distance_bottom_sheet_cancel_button_height"
android:background="?attr/selectableItemBackground">
<FrameLayout
android:id="@+id/cancel_row"
android:layout_width="match_parent"
android:layout_height="@dimen/measure_distance_bottom_sheet_cancel_button_height"
android:background="?attr/selectableItemBackground">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/shared_string_cancel"
android:textAllCaps="true"
android:textColor="?attr/color_dialog_buttons"
android:textSize="14sp"
android:textStyle="bold"/>
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/shared_string_cancel"
android:textAllCaps="true"
android:textColor="?attr/color_dialog_buttons"
android:textSize="14sp"
android:textStyle="bold"/>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

View file

@ -1,32 +1,33 @@
package net.osmand.plus.measurementtool;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.AndroidUiHelper;
import java.util.ArrayList;
import java.util.List;
public class SnapToRoadBottomSheetDialogFragment extends BottomSheetDialogFragment {
public class SnapToRoadBottomSheetDialogFragment extends android.support.design.widget.BottomSheetDialogFragment {
public static final String TAG = "SnapToRoadBottomSheetDialogFragment";
@ -39,18 +40,20 @@ public class SnapToRoadBottomSheetDialogFragment extends BottomSheetDialogFragme
this.listener = listener;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
nightMode = getMyApplication().getDaynightHelper().isNightModeForMapControls();
portrait = AndroidUiHelper.isOrientationPortrait(getActivity());
final OsmandSettings settings = getMyApplication().getSettings();
final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
final View mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_snap_to_road_bottom_sheet_dialog, container);
if (portrait) {
AndroidUtils.setBackground(getActivity(), mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark);
}
final View mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_snap_to_road_bottom_sheet_dialog, null);
AndroidUtils.setBackground(getActivity(), mainView, nightMode,
portrait ? R.drawable.bg_bottom_menu_light : R.drawable.bg_bottom_sheet_topsides_landscape_light,
portrait ? R.drawable.bg_bottom_menu_dark : R.drawable.bg_bottom_sheet_topsides_landscape_dark);
mainView.findViewById(R.id.cancel_row).setOnClickListener(new View.OnClickListener() {
@Override
@ -58,13 +61,16 @@ public class SnapToRoadBottomSheetDialogFragment extends BottomSheetDialogFragme
dismiss();
}
});
if (nightMode) {
((TextView) mainView.findViewById(R.id.choose_navigation_title)).setTextColor(getResources().getColor(R.color.ctx_menu_info_text_dark));
((TextView) mainView.findViewById(R.id.choose_navigation_title))
.setTextColor(ContextCompat.getColor(getActivity(), R.color.ctx_menu_info_text_dark));
}
LinearLayout navContainer = (LinearLayout) mainView.findViewById(R.id.navigation_types_container);
LinearLayout container = (LinearLayout) mainView.findViewById(R.id.navigation_types_container);
final List<ApplicationMode> modes = new ArrayList<>(ApplicationMode.values(settings));
modes.remove(ApplicationMode.DEFAULT);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -75,6 +81,7 @@ public class SnapToRoadBottomSheetDialogFragment extends BottomSheetDialogFragme
dismiss();
}
};
for (int i = 0; i < modes.size(); i++) {
ApplicationMode mode = modes.get(i);
View row = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.list_item_icon_and_title, null);
@ -82,47 +89,22 @@ public class SnapToRoadBottomSheetDialogFragment extends BottomSheetDialogFragme
((TextView) row.findViewById(R.id.title)).setText(mode.toHumanString(getContext()));
row.setOnClickListener(onClickListener);
row.setTag(i);
navContainer.addView(row);
container.addView(row);
}
final int screenHeight = AndroidUtils.getScreenHeight(getActivity());
final int statusBarHeight = AndroidUtils.getStatusBarHeight(getActivity());
final int navBarHeight = AndroidUtils.getNavBarHeight(getActivity());
mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final View scrollView = mainView.findViewById(R.id.navigation_types_scroll_view);
int scrollViewHeight = scrollView.getHeight();
int dividerHeight = AndroidUtils.dpToPx(getContext(), 1);
int cancelButtonHeight = getContext().getResources().getDimensionPixelSize(R.dimen.measure_distance_bottom_sheet_cancel_button_height);
int spaceForScrollView = screenHeight - statusBarHeight - navBarHeight - dividerHeight - cancelButtonHeight;
if (scrollViewHeight > spaceForScrollView) {
scrollView.getLayoutParams().height = spaceForScrollView;
scrollView.requestLayout();
if (!portrait) {
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
BottomSheetDialog dialog = (BottomSheetDialog) dialogInterface;
FrameLayout bottomSheet = (FrameLayout) dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
}
if (!portrait) {
if (screenHeight - statusBarHeight - mainView.getHeight()
>= AndroidUtils.dpToPx(getActivity(), 8)) {
AndroidUtils.setBackground(getActivity(), mainView, nightMode,
R.drawable.bg_bottom_sheet_topsides_landscape_light, R.drawable.bg_bottom_sheet_topsides_landscape_dark);
} else {
AndroidUtils.setBackground(getActivity(), mainView, nightMode,
R.drawable.bg_bottom_sheet_sides_landscape_light, R.drawable.bg_bottom_sheet_sides_landscape_dark);
}
}
ViewTreeObserver obs = mainView.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
return mainView;
dialog.setContentView(mainView);
((View) mainView.getParent()).setBackgroundResource(0);
}
@Override
@ -144,9 +126,13 @@ public class SnapToRoadBottomSheetDialogFragment extends BottomSheetDialogFragme
super.onDestroyView();
}
@Override
protected Drawable getContentIcon(@DrawableRes int id) {
return getIcon(id, nightMode ? R.color.ctx_menu_info_text_dark : R.color.on_map_icon_color);
private OsmandApplication getMyApplication() {
return ((MapActivity) getActivity()).getMyApplication();
}
private Drawable getContentIcon(@DrawableRes int id) {
return getMyApplication().getIconsCache()
.getIcon(id, nightMode ? R.color.ctx_menu_info_text_dark : R.color.on_map_icon_color);
}
interface SnapToRoadFragmentListener {