diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 0833b17..bd4202c 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -6,7 +6,7 @@ - + diff --git a/app/src/main/java/com/leanote/android/ui/ObservableWebView.java b/app/src/main/java/com/leanote/android/ui/ObservableWebView.java index 708a688..0de1837 100644 --- a/app/src/main/java/com/leanote/android/ui/ObservableWebView.java +++ b/app/src/main/java/com/leanote/android/ui/ObservableWebView.java @@ -2,35 +2,56 @@ import android.content.Context; import android.util.AttributeSet; + +import android.util.Log; import android.webkit.WebView; /** - * Basic activity for displaying a WebView. + * Created by mashenjun on 3-12-15. */ - public class ObservableWebView extends WebView { + private OnScrollChangedCallback mOnScrollChangedCallback; public ObservableWebView(final Context context) { super(context); } -// public ObservableWebView(final Context context, final AttributeSet attrs) { -// super(context, attrs); -// } -// -// public ObservableWebView(final Context context, final AttributeSet attrs, -// final int defStyle) { -// super(context, attrs, defStyle); -// } + + public ObservableWebView(final Context context, final AttributeSet attrs) { + super(context, attrs); + } + + public ObservableWebView(final Context context, final AttributeSet attrs, + final int defStyle) { + super(context, attrs, defStyle); + } + @Override protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) { super.onScrollChanged(l, t, oldl, oldt); + + float webcontent = getContentHeight() * (getResources().getDisplayMetrics().density);// webview的高度 + float webnow = getHeight() + getScrollY();// 当前webview的高度 + Log.i(this.getClass().getSimpleName(),"para "+webcontent+" "+webnow); + if (mOnScrollChangedCallback != null) { - mOnScrollChangedCallback.onScroll(l - oldl, t - oldt); + + if (Math.abs(webcontent - webnow) < 1) { + // 已经处于底端 + // Log.i("TAG1", "已经处于底端"); + mOnScrollChangedCallback.onPageEnd(l, t, oldl, oldt); + } else if (getScrollY() == 0) { + // Log.i("TAG1", "已经处于顶端"); + mOnScrollChangedCallback.onPageTop(l, t, oldl, oldt); + } + else { + mOnScrollChangedCallback.onScroll(l - oldl, t - oldt); + } + } } @@ -48,5 +69,8 @@ public void setOnScrollChangedCallback( */ public static interface OnScrollChangedCallback { public void onScroll(int dx, int dy); + public void onPageEnd(int l, int t, int oldl, int oldt); + public void onPageTop(int l, int t, int oldl, int oldt); + } } diff --git a/app/src/main/java/com/leanote/android/ui/WebViewActivity.java b/app/src/main/java/com/leanote/android/ui/WebViewActivity.java index 1c82145..e81b639 100644 --- a/app/src/main/java/com/leanote/android/ui/WebViewActivity.java +++ b/app/src/main/java/com/leanote/android/ui/WebViewActivity.java @@ -1,32 +1,33 @@ package com.leanote.android.ui; -import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.util.AttributeSet; -import android.util.Log; import android.view.MenuItem; import android.view.View; -import android.view.Window; import android.webkit.WebView; import com.leanote.android.R; +/** + * Basic activity for displaying a WebView. + */ + public abstract class WebViewActivity extends AppCompatActivity { /** Primary webview used to display content. */ - private static final String URL = "url"; + protected static String URL = ""; protected WebView mWebView; //protected ObservableWebView mWebView; @Override public void onCreate(Bundle savedInstanceState) { - supportRequestWindowFeature(Window.FEATURE_PROGRESS); - +// supportRequestWindowFeature(Window.FEATURE_PROGRESS); +// super.onCreate(savedInstanceState); + // clear title text so there's no title until actual web page title can be shown // this is done here rather than in the manifest to automatically handle descendants // such as AuthenticatedWebViewActivity @@ -60,6 +61,7 @@ public void onCreate(Bundle savedInstanceState) { if (url != null) { loadUrl(url); } + } @Override diff --git a/app/src/main/java/com/leanote/android/ui/post/BlogHomeActivity.java b/app/src/main/java/com/leanote/android/ui/post/BlogHomeActivity.java index 5539e96..ddb485c 100644 --- a/app/src/main/java/com/leanote/android/ui/post/BlogHomeActivity.java +++ b/app/src/main/java/com/leanote/android/ui/post/BlogHomeActivity.java @@ -1,16 +1,14 @@ package com.leanote.android.ui.post; -import android.app.ActionBar; -import android.content.Context; +import android.animation.ObjectAnimator; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.View; -import android.view.Window; -import android.view.WindowManager; +import android.view.ViewTreeObserver; +import android.view.animation.Animation; +import android.view.animation.Transformation; import android.webkit.WebView; -import android.webkit.WebViewClient; +import android.widget.FrameLayout; import com.leanote.android.R; import com.leanote.android.model.AccountHelper; @@ -18,43 +16,159 @@ import com.leanote.android.ui.WebViewActivity; + public class BlogHomeActivity extends WebViewActivity { - String userid; + private String username; + private FrameLayout webview_wrapper ; + private Toolbar toolbar ; + private Boolean hasMeasured = false; + private Boolean isTopHide = false; + private Boolean isTop = true; + static long TIME_ANIMATION =600; + static String Url ; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setTitle(""); - setContentView(R.layout.webview); + setTitle("Blog"); + webview_wrapper = (FrameLayout) findViewById(R.id.webview_wrapper); + + + toolbar = (Toolbar) findViewById(R.id.toolbar); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowTitleEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // note: do NOT call mWebView.getSettings().setUserAgentString(WordPress.getUserAgent()) // here since it causes problems with the browser-sniffing that some sites rely on to // format the page for mobile display - mWebView = (WebView) findViewById(R.id.webView); + + mWebView = (WebView) this.findViewById(R.id.webView); mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); -// mWebView.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() { -// public void onScroll(int dx, int dy) { -// //这里我们根据dx和dy参数做自己想做的事情 -// Log.i("dxdy", "dx: " + dx + "dy: " + dy); -// } -// }); + ViewTreeObserver viewTreeObserver = webview_wrapper.getViewTreeObserver(); + viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { + @Override + public boolean onPreDraw() { + + if (!hasMeasured) { + FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout + .LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); + layoutParams.setMargins(0,toolbar.getHeight(), 0, 0); + mWebView.setLayoutParams(layoutParams); + + hasMeasured = true; + } + return true; + } + }); + + + + ((ObservableWebView)mWebView).setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() { + public void onScroll(int dx, int dy) { + + //这里我们根据dx和dy参数做自己想做的事情 + if (dy > 100 && isTopHide == false) { + + hideTop(); + + } + } + + @Override + public void onPageEnd(int l, int t, int oldl, int oldt) { + + } + + @Override + public void onPageTop(int l, int t, int oldl, int oldt) { + if (isTopHide == true) { + + showTop(); + + } + } + }); // load URL if one was provided in the intent - userid=AccountHelper.getDefaultAccount().getmUserName(); - String url = "http://blog.leanote.com/" + userid; - if (url != null) { - loadUrl(url); + username=AccountHelper.getDefaultAccount().getmUserName(); + Url = "http://blog.leanote.com/" + username; + if (Url != null) { + mWebView.loadUrl(Url); } } -} + private void showTop() { + //toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)); + + ObjectAnimator anim1 = ObjectAnimator.ofFloat(toolbar, "y", toolbar.getY(), + 0); + anim1.setDuration(TIME_ANIMATION); + anim1.start(); +// +// +// ObjectAnimator anim4 = ObjectAnimator.ofFloat(mWebView, "y", mWebView.getY(), +// 0); +// anim4.setDuration(TIME_ANIMATION); +// anim4.start(); + + + final int topmatgin = toolbar.getHeight(); + Animation a = new Animation() { + + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)mWebView.getLayoutParams(); + params.topMargin = (int)(topmatgin * interpolatedTime); + mWebView.setLayoutParams(params); + } + }; + a.setDuration(TIME_ANIMATION); // in ms + mWebView.startAnimation(a); + + + isTopHide = false; + } + + + /** + * 隐藏上部的布局 + */ + private void hideTop() { + + //toolbar.animate().translationY(-toolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start(); + + ObjectAnimator anim1 = ObjectAnimator.ofFloat(toolbar, "y", 0, + -toolbar.getHeight()); + anim1.setDuration(TIME_ANIMATION); + anim1.start(); +// +// Log.i(this.getClass().getSimpleName(), "para " + mWebView.getY() + " " + webview_wrapper.getY()); +// ObjectAnimator anim4 = ObjectAnimator.ofFloat(mWebView, "y", mWebView.getY(), +// 0); +// anim4.setDuration(TIME_ANIMATION); +// anim4.start(); + + final int topmatgin = 0; + Animation a = new Animation() { + + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)mWebView.getLayoutParams(); + params.topMargin = (int)(topmatgin * interpolatedTime); + mWebView.setLayoutParams(params); + } + }; + a.setDuration(TIME_ANIMATION); // in ms + mWebView.startAnimation(a); + + isTopHide = true; + } +} diff --git a/app/src/main/res/layout/title_bar.xml b/app/src/main/res/layout/title_bar.xml index 88db546..c60ce40 100644 --- a/app/src/main/res/layout/title_bar.xml +++ b/app/src/main/res/layout/title_bar.xml @@ -11,6 +11,9 @@ android:text="" android:textColor="#fff" android:textSize="20sp" - android:textStyle="bold" /> + android:textStyle="bold" + android:id = "@+id/title_bar" + /> + \ No newline at end of file diff --git a/app/src/main/res/layout/webview.xml b/app/src/main/res/layout/webview.xml index b1de311..e17aa1e 100644 --- a/app/src/main/res/layout/webview.xml +++ b/app/src/main/res/layout/webview.xml @@ -4,24 +4,25 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 8b27094..57b4edf 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -341,13 +341,13 @@ @color/white +