Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 35 additions & 11 deletions app/src/main/java/com/leanote/android/ui/ObservableWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}

Expand All @@ -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);

}
}
16 changes: 9 additions & 7 deletions app/src/main/java/com/leanote/android/ui/WebViewActivity.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -60,6 +61,7 @@ public void onCreate(Bundle savedInstanceState) {
if (url != null) {
loadUrl(url);
}

}

@Override
Expand Down
160 changes: 137 additions & 23 deletions app/src/main/java/com/leanote/android/ui/post/BlogHomeActivity.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,174 @@
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;
import com.leanote.android.ui.ObservableWebView;
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;
}
}

5 changes: 4 additions & 1 deletion app/src/main/res/layout/title_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
android:text=""
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
android:textStyle="bold"
android:id = "@+id/title_bar"
/>


</RelativeLayout>
29 changes: 15 additions & 14 deletions app/src/main/res/layout/webview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
/>

<include
layout="@layout/toolbar"
android:id="@+id/toolbar" />

<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/progress_bar_height"
android:indeterminate="false"
android:layout_alignTop="@+id/webView"
android:progressDrawable="@drawable/progressbar_horizontal" />
<com.leanote.android.ui.ObservableWebView
android:id="@+id/webView"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/progress_bar_height"
android:indeterminate="false"
android:layout_alignTop="@+id/webView"
android:progressDrawable="@drawable/progressbar_horizontal" />

</FrameLayout>
Loading