From 4c5911804a14c3d3f8f657946082fbc11b364527 Mon Sep 17 00:00:00 2001 From: Gregor Santner Date: Sun, 5 Jun 2016 01:42:10 +0200 Subject: [PATCH] Collapsing top menu --- .../activity/MainActivity.java | 23 +-- .../ui/ContextMenuWebView.java | 2 +- .../diaspora_android/ui/NestedWebView.java | 142 ++++++++++++++++++ app/src/main/res/layout/main__app_bar.xml | 27 +++- app/src/main/res/layout/main__content.xml | 2 + 5 files changed, 170 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/com/github/dfa/diaspora_android/ui/NestedWebView.java diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index b7f9884b..079fd63c 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -37,6 +37,7 @@ import android.os.Environment; import android.os.Handler; import android.provider.MediaStore; import android.support.annotation.NonNull; +import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; @@ -143,6 +144,9 @@ public class MainActivity extends AppCompatActivity @BindView(R.id.main__layout) DrawerLayout navDrawer; + @BindView(R.id.collapsing_toolbar) + CollapsingToolbarLayout toolbarTopCollapsing; + // NavHeader cannot be bound by Butterknife private TextView navheaderTitle; @@ -412,7 +416,6 @@ public class MainActivity extends AppCompatActivity if (webView.canGoBack()) { webView.goBack(); - setTitle(R.string.app_name); } else { Snackbar snackbar = Snackbar .make(swipeRefreshLayout, R.string.confirm_exit, Snackbar.LENGTH_LONG) @@ -431,7 +434,6 @@ public class MainActivity extends AppCompatActivity @Override public void onReceive(Context context, Intent intent) { String url = intent.getStringExtra("url"); - setTitle(R.string.app_name); webView.loadUrl(url); } }; @@ -475,7 +477,6 @@ public class MainActivity extends AppCompatActivity case R.id.action_notifications: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/notifications"); - setTitle(R.string.jb_notifications); return true; } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); @@ -486,7 +487,6 @@ public class MainActivity extends AppCompatActivity case R.id.action_conversations: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/conversations"); - setTitle(R.string.jb_conversations); return true; } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); @@ -502,7 +502,6 @@ public class MainActivity extends AppCompatActivity case R.id.action_compose: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/status_messages/new"); - setTitle(R.string.compose); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -560,10 +559,8 @@ public class MainActivity extends AppCompatActivity } else { // User have added a search tag if (wasClickedOnSearchForPeople) { webView.loadUrl("https://" + podDomain + "/people.mobile?q=" + cleanTag); - setTitle(R.string.search_by_person); } else { webView.loadUrl("https://" + podDomain + "/tags/" + cleanTag); - setTitle(R.string.search_by_tag); } } @@ -773,7 +770,6 @@ public class MainActivity extends AppCompatActivity public void onClick(View view) { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/notifications"); - setTitle(R.string.jb_notifications); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -819,7 +815,6 @@ public class MainActivity extends AppCompatActivity public void onClick(View view) { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/conversations"); - setTitle(R.string.jb_notifications); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -845,7 +840,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_stream: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/stream"); - setTitle(R.string.jb_stream); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -855,7 +849,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_profile: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/people/" + appSettings.getProfileId()); - setTitle(R.string.jb_profile); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -866,7 +859,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_followed_tags: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/followed_tags"); - setTitle(R.string.jb_followed_tags); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -876,7 +868,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_aspects: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/aspects"); - setTitle(R.string.jb_aspects); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -886,7 +877,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_activities: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/activity"); - setTitle(R.string.jb_activities); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -896,7 +886,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_liked: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/liked"); - setTitle(R.string.jb_liked); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -906,7 +895,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_commented: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/commented"); - setTitle(R.string.jb_commented); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -916,7 +904,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_mentions: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/mentions"); - setTitle(R.string.jb_mentions); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -926,7 +913,6 @@ public class MainActivity extends AppCompatActivity case R.id.nav_public: { if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/public"); - setTitle(R.string.jb_public); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } @@ -1058,5 +1044,4 @@ public class MainActivity extends AppCompatActivity grantResults); } } - } \ No newline at end of file diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java index 14431523..6b42b313 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java @@ -27,7 +27,7 @@ import java.io.File; * Subclass of WebView which adds a context menu for long clicks on images or links to share, save * or open with another browser */ -public class ContextMenuWebView extends WebView { +public class ContextMenuWebView extends NestedWebView { public static final int ID_SAVE_IMAGE = 10; public static final int ID_EXTERNAL_BROWSER = 11; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/NestedWebView.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/NestedWebView.java new file mode 100644 index 00000000..4f59a339 --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/NestedWebView.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2015 takahirom + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.dfa.diaspora_android.ui; + +import android.content.Context; +import android.support.v4.view.MotionEventCompat; +import android.support.v4.view.NestedScrollingChild; +import android.support.v4.view.NestedScrollingChildHelper; +import android.support.v4.view.ViewCompat; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.webkit.WebView; + +public class NestedWebView extends WebView implements NestedScrollingChild { + private int mLastY; + private final int[] mScrollOffset = new int[2]; + private final int[] mScrollConsumed = new int[2]; + private int mNestedOffsetY; + private NestedScrollingChildHelper mChildHelper; + + public NestedWebView(Context context) { + this(context, null); + } + + public NestedWebView(Context context, AttributeSet attrs) { + this(context, attrs, android.R.attr.webViewStyle); + } + + public NestedWebView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + mChildHelper = new NestedScrollingChildHelper(this); + setNestedScrollingEnabled(true); + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + boolean returnValue = false; + + MotionEvent event = MotionEvent.obtain(ev); + final int action = MotionEventCompat.getActionMasked(event); + if (action == MotionEvent.ACTION_DOWN) { + mNestedOffsetY = 0; + } + int eventY = (int) event.getY(); + event.offsetLocation(0, mNestedOffsetY); + switch (action) { + case MotionEvent.ACTION_MOVE: + int deltaY = mLastY - eventY; + // NestedPreScroll + if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) { + deltaY -= mScrollConsumed[1]; + mLastY = eventY - mScrollOffset[1]; + event.offsetLocation(0, -mScrollOffset[1]); + mNestedOffsetY += mScrollOffset[1]; + } + returnValue = super.onTouchEvent(event); + + // NestedScroll + if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) { + event.offsetLocation(0, mScrollOffset[1]); + mNestedOffsetY += mScrollOffset[1]; + mLastY -= mScrollOffset[1]; + } + break; + case MotionEvent.ACTION_DOWN: + returnValue = super.onTouchEvent(event); + mLastY = eventY; + // start NestedScroll + startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL); + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + returnValue = super.onTouchEvent(event); + // end NestedScroll + stopNestedScroll(); + break; + } + return returnValue; + } + + // Nested Scroll implements + @Override + public void setNestedScrollingEnabled(boolean enabled) { + mChildHelper.setNestedScrollingEnabled(enabled); + } + + @Override + public boolean isNestedScrollingEnabled() { + return mChildHelper.isNestedScrollingEnabled(); + } + + @Override + public boolean startNestedScroll(int axes) { + return mChildHelper.startNestedScroll(axes); + } + + @Override + public void stopNestedScroll() { + mChildHelper.stopNestedScroll(); + } + + @Override + public boolean hasNestedScrollingParent() { + return mChildHelper.hasNestedScrollingParent(); + } + + @Override + public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, + int[] offsetInWindow) { + return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow); + } + + @Override + public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) { + return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow); + } + + @Override + public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) { + return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed); + } + + @Override + public boolean dispatchNestedPreFling(float velocityX, float velocityY) { + return mChildHelper.dispatchNestedPreFling(velocityX, velocityY); + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/main__app_bar.xml b/app/src/main/res/layout/main__app_bar.xml index f754c0ff..44cab75b 100644 --- a/app/src/main/res/layout/main__app_bar.xml +++ b/app/src/main/res/layout/main__app_bar.xml @@ -10,14 +10,29 @@ - + android:layout_height="81dp" + android:fitsSystemWindows="true" + app:contentScrim="?attr/colorPrimary" + app:expandedTitleMarginEnd="64dp" + app:expandedTitleMarginStart="48dp" + app:layout_scrollFlags="scroll|enterAlways|snap" + app:titleEnabled="true"> + + + + @@ -25,8 +40,8 @@