From 5782c0100d7d127d34ed0eb6808712634409c3cb Mon Sep 17 00:00:00 2001 From: androiddevkkotlin Date: Sun, 6 Dec 2020 13:22:23 +0200 Subject: [PATCH] setShadowOnScrollableView --- .../base/MenuBottomSheetDialogFragment.java | 12 +++++++ .../osmedit/dialogs/BugBottomSheetDialog.java | 34 +++++-------------- .../dialogs/SendGpxBottomSheetFragment.java | 29 +++++----------- .../SendOsmNoteBottomSheetFragment.java | 11 ++++++ .../dialogs/SendPoiBottomSheetFragment.java | 11 ++++++ 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java index 05dbd85340..00cea3e974 100644 --- a/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java @@ -16,6 +16,7 @@ import android.view.ViewTreeObserver.OnScrollChangedListener; import android.view.Window; import android.view.WindowManager; import android.widget.LinearLayout; +import android.widget.ScrollView; import androidx.annotation.ColorInt; import androidx.annotation.ColorRes; @@ -478,4 +479,15 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra } }); } + + protected void setShadowOnScrollableView() { + ScrollView scrollView = getView().findViewById(R.id.scroll_view); + if (scrollView.canScrollVertically(1) || scrollView.canScrollVertically(-1)) { + drawTopShadow(false); + scrollView.getChildAt(0).setPadding(0, 8, 0, 0); + } else { + drawTopShadow(true); + } + } + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/BugBottomSheetDialog.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/BugBottomSheetDialog.java index 0a80b16c95..4ea678880a 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/BugBottomSheetDialog.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/BugBottomSheetDialog.java @@ -1,7 +1,6 @@ package net.osmand.plus.osmedit.dialogs; import android.content.res.ColorStateList; -import android.graphics.Rect; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; @@ -47,10 +46,6 @@ public class BugBottomSheetDialog extends MenuBottomSheetDialogFragment { private OsmNotesPoint point; private HandleOsmNoteAsyncTask.HandleBugListener handleBugListener; private TextInputEditText noteText; - private int contentHeightPrevious = 0; - private int buttonsHeight; - private int shadowHeight; - private ScrollView scrollView; @Override public void createMenuItems(Bundle savedInstanceState) { @@ -63,7 +58,7 @@ public class BugBottomSheetDialog extends MenuBottomSheetDialogFragment { View osmNoteView = View.inflate(UiUtilities.getThemedContext(app, nightMode), R.layout.open_osm_note_text, null); - osmNoteView.getViewTreeObserver().addOnGlobalLayoutListener(getOnGlobalLayoutListener()); + osmNoteView.getViewTreeObserver().addOnScrollChangedListener(getOnGlobalLayoutListener()); TextInputLayout textBox = osmNoteView.findViewById(R.id.name_text_box); textBox.setHint(AndroidUtils.addColon(app, R.string.osn_bug_name)); ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat @@ -80,24 +75,11 @@ public class BugBottomSheetDialog extends MenuBottomSheetDialogFragment { items.add(new DividerSpaceItem(app, app.getResources().getDimensionPixelSize(R.dimen.content_padding_small))); } - private ViewTreeObserver.OnGlobalLayoutListener getOnGlobalLayoutListener() { - return new ViewTreeObserver.OnGlobalLayoutListener() { + private ViewTreeObserver.OnScrollChangedListener getOnGlobalLayoutListener() { + return new ViewTreeObserver.OnScrollChangedListener() { @Override - public void onGlobalLayout() { - Rect visibleDisplayFrame = new Rect(); - buttonsHeight = getResources().getDimensionPixelSize(R.dimen.dialog_button_ex_max_width); - shadowHeight = getResources().getDimensionPixelSize(R.dimen.bottom_sheet_top_shadow_height); - scrollView = getView().findViewById(R.id.scroll_view); - scrollView.getWindowVisibleDisplayFrame(visibleDisplayFrame); - int viewHeight = scrollView.getHeight(); - int contentHeight = visibleDisplayFrame.bottom - visibleDisplayFrame.top - buttonsHeight; - if (contentHeightPrevious != contentHeight) { - boolean showTopShadow; - showTopShadow = viewHeight + shadowHeight < contentHeight; - scrollView.requestLayout(); - contentHeightPrevious = contentHeight; - drawTopShadow(showTopShadow); - } + public void onScrollChanged() { + setShadowOnScrollableView(); } }; } @@ -115,9 +97,9 @@ public class BugBottomSheetDialog extends MenuBottomSheetDialogFragment { } public static void showInstance(@NonNull FragmentManager fm, OsmBugsUtil osmBugsUtil, OsmBugsUtil local, - String text, int titleTextId, int posButtonTextId, final OsmPoint.Action action, - final OsmBugsLayer.OpenStreetNote bug, final OsmNotesPoint point, - HandleOsmNoteAsyncTask.HandleBugListener handleBugListener) { + String text, int titleTextId, int posButtonTextId, final OsmPoint.Action action, + final OsmBugsLayer.OpenStreetNote bug, final OsmNotesPoint point, + HandleOsmNoteAsyncTask.HandleBugListener handleBugListener) { try { if (!fm.isStateSaved()) { BugBottomSheetDialog fragment = new BugBottomSheetDialog(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendGpxBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendGpxBottomSheetFragment.java index 3b02b3c05b..3fdeaaae05 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendGpxBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendGpxBottomSheetFragment.java @@ -1,6 +1,5 @@ package net.osmand.plus.osmedit.dialogs; -import android.graphics.Rect; import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; @@ -54,9 +53,6 @@ public class SendGpxBottomSheetFragment extends MenuBottomSheetDialogFragment { private TextInputEditText tagsField; private TextInputEditText messageField; - private int contentHeightPrevious = 0; - private int buttonsHeight; - private int shadowHeight; private ScrollView scrollView; public void setGpxInfos(GpxInfo[] gpxInfos) { @@ -70,7 +66,7 @@ public class SendGpxBottomSheetFragment extends MenuBottomSheetDialogFragment { LayoutInflater themedInflater = UiUtilities.getInflater(app, nightMode); View sendGpxView = themedInflater.inflate(R.layout.send_gpx_fragment, null); - sendGpxView.getViewTreeObserver().addOnGlobalLayoutListener(getOnGlobalLayoutListener()); + sendGpxView.getViewTreeObserver().addOnScrollChangedListener(getOnGlobalLayoutListener()); tagsField = sendGpxView.findViewById(R.id.tags_field); messageField = sendGpxView.findViewById(R.id.message_field); @@ -132,23 +128,16 @@ public class SendGpxBottomSheetFragment extends MenuBottomSheetDialogFragment { items.add(titleItem); } - private ViewTreeObserver.OnGlobalLayoutListener getOnGlobalLayoutListener() { - return new ViewTreeObserver.OnGlobalLayoutListener() { + private ViewTreeObserver.OnScrollChangedListener getOnGlobalLayoutListener() { + return new ViewTreeObserver.OnScrollChangedListener() { @Override - public void onGlobalLayout() { - Rect visibleDisplayFrame = new Rect(); - buttonsHeight = getResources().getDimensionPixelSize(R.dimen.dialog_button_ex_max_width); - shadowHeight = getResources().getDimensionPixelSize(R.dimen.bottom_sheet_top_shadow_height); + public void onScrollChanged() { scrollView = getView().findViewById(R.id.scroll_view); - scrollView.getWindowVisibleDisplayFrame(visibleDisplayFrame); - int viewHeight = scrollView.getHeight(); - int contentHeight = visibleDisplayFrame.bottom - visibleDisplayFrame.top - buttonsHeight; - if (contentHeightPrevious != contentHeight) { - boolean showTopShadow; - showTopShadow = viewHeight + shadowHeight < contentHeight; - scrollView.requestLayout(); - contentHeightPrevious = contentHeight; - drawTopShadow(showTopShadow); + if (scrollView.canScrollVertically(1) || scrollView.canScrollVertically(-1)) { + drawTopShadow(false); + scrollView.getChildAt(0).setPadding(0, 8, 0, 0); + } else { + drawTopShadow(true); } } }; diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java index 7ab3d9149b..d1178dbca3 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendOsmNoteBottomSheetFragment.java @@ -6,6 +6,7 @@ import android.os.Bundle; import android.view.ContextThemeWrapper; import android.view.View; import android.view.ViewGroup; +import android.view.ViewTreeObserver; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.LinearLayout; @@ -77,6 +78,7 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen final View sendOsmNoteView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.send_osm_note_fragment, null); + sendOsmNoteView.getViewTreeObserver().addOnScrollChangedListener(getOnGlobalLayoutListener()); noteText = sendOsmNoteView.findViewById(R.id.note_text); noteText.setText(((OsmNotesPoint) poi[0]).getText()); @@ -148,6 +150,15 @@ public class SendOsmNoteBottomSheetFragment extends MenuBottomSheetDialogFragmen items.add(bottomSheetItem); } + private ViewTreeObserver.OnScrollChangedListener getOnGlobalLayoutListener() { + return new ViewTreeObserver.OnScrollChangedListener() { + @Override + public void onScrollChanged() { + setShadowOnScrollableView(); + } + }; + } + private void updateAccountName() { String userNameOAuth = settings.USER_DISPLAY_NAME.get(); String userNameOpenID = settings.USER_NAME.get(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java index 35a998e1bb..28134215d7 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendPoiBottomSheetFragment.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.os.Bundle; import android.view.ContextThemeWrapper; import android.view.View; +import android.view.ViewTreeObserver; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.LinearLayout; @@ -60,6 +61,7 @@ public class SendPoiBottomSheetFragment extends MenuBottomSheetDialogFragment { final boolean isNightMode = app.getDaynightHelper().isNightModeForMapControls(); final View sendOsmPoiView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.send_poi_fragment, null); + sendOsmPoiView.getViewTreeObserver().addOnScrollChangedListener(getOnGlobalLayoutListener()); closeChangeSet = sendOsmPoiView.findViewById(R.id.close_change_set_checkbox); messageEditText = sendOsmPoiView.findViewById(R.id.message_field); String defaultChangeSet = createDefaultChangeSet(app); @@ -104,6 +106,15 @@ public class SendPoiBottomSheetFragment extends MenuBottomSheetDialogFragment { items.add(titleItem); } + private ViewTreeObserver.OnScrollChangedListener getOnGlobalLayoutListener() { + return new ViewTreeObserver.OnScrollChangedListener() { + @Override + public void onScrollChanged() { + setShadowOnScrollableView(); + } + }; + } + public static void showInstance(@NonNull FragmentManager fm, @NonNull OsmPoint[] points) { try { if (!fm.isStateSaved()) {