From 723fff03ecb828eef700a0be2048349796123e27 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 2 Jan 2013 18:38:03 +0400 Subject: cleaner titlewebview implementation --- src/org/fox/ttrss/ArticleFragment.java | 16 +++--- src/org/fox/ttrss/TitleWebView.java | 91 ++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 src/org/fox/ttrss/TitleWebView.java (limited to 'src/org/fox/ttrss') diff --git a/src/org/fox/ttrss/ArticleFragment.java b/src/org/fox/ttrss/ArticleFragment.java index 38b66290..7372487d 100644 --- a/src/org/fox/ttrss/ArticleFragment.java +++ b/src/org/fox/ttrss/ArticleFragment.java @@ -13,23 +13,19 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import com.nobu_games.android.view.web.TitleBarWebView; - import android.annotation.SuppressLint; -import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.Paint; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.util.Log; import android.util.TypedValue; -import android.view.GestureDetector; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; +import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.MotionEvent; @@ -42,7 +38,6 @@ import android.webkit.WebSettings.LayoutAlgorithm; import android.webkit.WebView; import android.widget.ArrayAdapter; import android.widget.Button; -import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; @@ -174,16 +169,17 @@ public class ArticleFragment extends Fragment implements GestureDetector.OnDoubl } } - TitleBarWebView web = (TitleBarWebView)view.findViewById(R.id.content); + TitleWebView web = (TitleWebView)view.findViewById(R.id.content); if (web != null) { - if (!m_activity.isPortrait() && m_activity.isSmallScreen()) { + /* if (!m_activity.isPortrait() && m_activity.isSmallScreen()) { + View header = view.findViewById(R.id.article_header); LinearLayout article = (LinearLayout)view.findViewById(R.id.article_fragment); article.removeView(header); - + web.setEmbeddedTitleBarCompat(header); - } + } */ web.setWebChromeClient(new WebChromeClient() { @Override diff --git a/src/org/fox/ttrss/TitleWebView.java b/src/org/fox/ttrss/TitleWebView.java new file mode 100644 index 00000000..e39fc268 --- /dev/null +++ b/src/org/fox/ttrss/TitleWebView.java @@ -0,0 +1,91 @@ +package org.fox.ttrss; + +// http://www.techques.com/question/1-9718245/Webview-in-Scrollview + +import android.content.Context; +import android.graphics.Canvas; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.webkit.WebView; + +public class TitleWebView extends WebView{ + + public TitleWebView(Context context, AttributeSet attrs){ + super(context, attrs); + } + + private int titleHeight; + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + // determine height of title bar + View title = getChildAt(0); + titleHeight = title==null ? 0 : title.getMeasuredHeight(); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev){ + return true; // don't pass our touch events to children (title bar), we send these in dispatchTouchEvent + } + + private boolean touchInTitleBar; + @Override + public boolean dispatchTouchEvent(MotionEvent me){ + + boolean wasInTitle = false; + switch(me.getActionMasked()){ + case MotionEvent.ACTION_DOWN: + touchInTitleBar = (me.getY() <= visibleTitleHeight()); + break; + + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + wasInTitle = touchInTitleBar; + touchInTitleBar = false; + break; + } + if(touchInTitleBar || wasInTitle) { + View title = getChildAt(0); + if(title!=null) { + // this touch belongs to title bar, dispatch it here + me.offsetLocation(0, getScrollY()); + return title.dispatchTouchEvent(me); + } + } + // this is our touch, offset and process + me.offsetLocation(0, -titleHeight); + return super.dispatchTouchEvent(me); + } + + /** + * @return visible height of title (may return negative values) + */ + private int visibleTitleHeight(){ + return titleHeight-getScrollY(); + } + + @Override + protected void onScrollChanged(int l, int t, int oldl, int oldt){ + super.onScrollChanged(l, t, oldl, oldt); + View title = getChildAt(0); + if(title!=null) // undo horizontal scroll, so that title scrolls only vertically + title.offsetLeftAndRight(l - title.getLeft()); + } + + @Override + protected void onDraw(Canvas c){ + + c.save(); + int tH = visibleTitleHeight(); + if(tH>0) { + // clip so that it doesn't clear background under title bar + int sx = getScrollX(), sy = getScrollY(); + c.clipRect(sx, sy+tH, sx+getWidth(), sy+getHeight()); + } + c.translate(0, titleHeight); + super.onDraw(c); + c.restore(); + } + } \ No newline at end of file -- cgit v1.2.3-54-g00ecf