diff options
author | Andrew Dolgov <[email protected]> | 2014-10-16 13:53:44 +0400 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2014-10-16 13:53:44 +0400 |
commit | 811bc035c0935d321b2cad085853580e44888f62 (patch) | |
tree | 0ba39359f2cf17e5577bed2ace480f776277b8ea /src/org | |
parent | 636ddc362369c7658ea40bb3f9e488db902f6408 (diff) |
use viewholder for headlines view
automatically enlarge flavor image
Diffstat (limited to 'src/org')
-rw-r--r-- | src/org/fox/ttrss/HeadlinesFragment.java | 161 | ||||
-rw-r--r-- | src/org/fox/ttrss/util/EnlargingImageView.java | 252 |
2 files changed, 340 insertions, 73 deletions
diff --git a/src/org/fox/ttrss/HeadlinesFragment.java b/src/org/fox/ttrss/HeadlinesFragment.java index f58e0a0b..155fcfd0 100644 --- a/src/org/fox/ttrss/HeadlinesFragment.java +++ b/src/org/fox/ttrss/HeadlinesFragment.java @@ -648,6 +648,20 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, } } */ + static class HeadlineViewHolder { + public TextView titleView; + public TextView feedTitleView; + public ImageView markedView; + public ImageView publishedView; + public TextView excerptView; + public ImageView flavorImageView; + public TextView authorView; + public TextView dateView; + public CheckBox selectionBoxView; + public ImageView menuButtonView; + + } + private class ArticleListAdapter extends ArrayAdapter<Article> { private ArrayList<Article> items; @@ -697,8 +711,9 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; - + final Article article = items.get(position); + HeadlineViewHolder holder; int headlineFontSize = Integer.parseInt(m_prefs.getString("headlines_font_size_sp", "13")); int headlineSmallFontSize = Math.max(10, Math.min(18, headlineFontSize - 2)); @@ -723,55 +738,71 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(layoutId, null); + + holder = new HeadlineViewHolder(); + holder.titleView = (TextView)v.findViewById(R.id.title); + + holder.feedTitleView = (TextView)v.findViewById(R.id.feed_title); + holder.markedView = (ImageView)v.findViewById(R.id.marked); + holder.publishedView = (ImageView)v.findViewById(R.id.published); + holder.excerptView = (TextView)v.findViewById(R.id.excerpt); + holder.flavorImageView = (ImageView) v.findViewById(R.id.flavor_image); + holder.authorView = (TextView)v.findViewById(R.id.author); + holder.dateView = (TextView) v.findViewById(R.id.date); + holder.selectionBoxView = (CheckBox) v.findViewById(R.id.selected); + holder.menuButtonView = (ImageView) v.findViewById(R.id.article_menu_button); + + + v.setTag(holder); // http://code.google.com/p/android/issues/detail?id=3414 ((ViewGroup)v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); + } else { + holder = (HeadlineViewHolder) v.getTag(); } - - TextView tt = (TextView)v.findViewById(R.id.title); - - if (tt != null) { - tt.setText(Html.fromHtml(article.title)); + + if (holder.titleView != null) { + holder.titleView.setText(Html.fromHtml(article.title)); if (m_prefs.getBoolean("enable_condensed_fonts", false)) { Typeface tf = TypefaceCache.get(m_activity, "sans-serif-condensed", article.unread ? Typeface.BOLD : Typeface.NORMAL); - if (tf != null && !tf.equals(tt.getTypeface())) { - tt.setTypeface(tf); + if (tf != null && !tf.equals(holder.titleView.getTypeface())) { + holder.titleView.setTypeface(tf); } - tt.setTextSize(TypedValue.COMPLEX_UNIT_SP, Math.min(21, headlineFontSize + 5)); + holder.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, Math.min(21, headlineFontSize + 5)); } else { - tt.setTextSize(TypedValue.COMPLEX_UNIT_SP, Math.min(21, headlineFontSize + 3)); + holder.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, Math.min(21, headlineFontSize + 3)); } - adjustTitleTextView(article.score, tt, position); + adjustTitleTextView(article.score, holder.titleView, position); } - TextView ft = (TextView)v.findViewById(R.id.feed_title); - if (ft != null) { + + if (holder.feedTitleView != null) { if (article.feed_title != null && (m_feed.is_cat || m_feed.id < 0)) { /* if (article.feed_title.length() > 20) ft.setText(article.feed_title.substring(0, 20) + "..."); else */ - ft.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize); - ft.setText(article.feed_title); + holder.feedTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize); + holder.feedTitleView.setText(article.feed_title); } else { - ft.setVisibility(View.GONE); + holder.feedTitleView.setVisibility(View.GONE); } } - ImageView marked = (ImageView)v.findViewById(R.id.marked); - if (marked != null) { - marked.setImageResource(article.marked ? R.drawable.ic_star_full : R.drawable.ic_star_empty); + + if (holder.markedView != null) { + holder.markedView.setImageResource(article.marked ? R.drawable.ic_star_full : R.drawable.ic_star_empty); - marked.setOnClickListener(new OnClickListener() { + holder.markedView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { @@ -783,12 +814,12 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, }); } - ImageView published = (ImageView)v.findViewById(R.id.published); - if (published != null) { - published.setImageResource(article.published ? R.drawable.ic_published : R.drawable.ic_unpublished); + + if (holder.publishedView != null) { + holder.publishedView.setImageResource(article.published ? R.drawable.ic_published : R.drawable.ic_unpublished); - published.setOnClickListener(new OnClickListener() { + holder.publishedView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { @@ -802,31 +833,29 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, String articleContent = article.content != null ? article.content : ""; - TextView te = (TextView)v.findViewById(R.id.excerpt); - - if (te != null) { + if (holder.excerptView != null) { if (!m_prefs.getBoolean("headlines_show_content", true)) { - te.setVisibility(View.GONE); + holder.excerptView.setVisibility(View.GONE); } else { String excerpt = Jsoup.parse(articleContent).text(); if (excerpt.length() > CommonActivity.EXCERPT_MAX_SIZE) excerpt = excerpt.substring(0, CommonActivity.EXCERPT_MAX_SIZE) + "..."; - te.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineFontSize); - te.setText(excerpt); + holder.excerptView.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineFontSize); + holder.excerptView.setText(excerpt); } } - final ImageView flavorImage = (ImageView) v.findViewById(R.id.flavor_image); - if (flavorImage != null && m_prefs.getBoolean("headlines_show_flavor_image", true)) { - flavorImage.setVisibility(View.GONE); + + if (holder.flavorImageView != null && m_prefs.getBoolean("headlines_show_flavor_image", true)) { + holder.flavorImageView.setVisibility(View.GONE); Document doc = Jsoup.parse(articleContent); - if (doc != null) { Element img = doc.select("img").first(); + if (doc != null) { if (img != null) { String imgSrc = img.attr("src"); @@ -835,14 +864,14 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, if (imgSrc.indexOf("//") == 0) imgSrc = "http:" + imgSrc; - flavorImage.setTag(imgSrc); - DisplayImageOptions options = new DisplayImageOptions.Builder(). cacheInMemory(true). cacheOnDisk(true). build(); - ImageLoader.getInstance().displayImage(imgSrc, flavorImage, options, new ImageLoadingListener() { + final ImageView flavorImageView = holder.flavorImageView; + + ImageLoader.getInstance().displayImage(imgSrc, holder.flavorImageView, options, new ImageLoadingListener() { @Override public void onLoadingCancelled(String arg0, @@ -859,14 +888,16 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, if (!isAdded()) return; if (arg2.getWidth() > 128 && arg2.getHeight() > 128) { - - if (!m_activity.isCompatMode()) { - ObjectAnimator anim = ObjectAnimator.ofFloat(flavorImage, "alpha", 0f, 1f); - anim.setDuration(500); - anim.start(); + if (arg0 != null && !arg0.equals(arg1.getTag())) { + if (!m_activity.isCompatMode() && flavorImageView.getVisibility() != View.VISIBLE) { + ObjectAnimator anim = ObjectAnimator.ofFloat(flavorImageView, "alpha", 0f, 1f); + anim.setDuration(500); + anim.start(); + } } - flavorImage.setVisibility(View.VISIBLE); + flavorImageView.setTag(arg0); + flavorImageView.setVisibility(View.VISIBLE); } } @@ -874,7 +905,6 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, public void onLoadingFailed(String arg0, View arg1, FailReason arg2) { // TODO Auto-generated method stub - } @Override @@ -885,9 +915,6 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, } }); - - //new DownloadFlavorImagesTask().execute(flavorImage); - } } @@ -896,40 +923,30 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, String articleAuthor = article.author != null ? article.author : ""; - TextView author = (TextView)v.findViewById(R.id.author); - if (author != null) { - author.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize); + if (holder.authorView != null) { + holder.authorView.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize); if (articleAuthor.length() > 0) { - author.setText(getString(R.string.author_formatted, articleAuthor)); + holder.authorView.setText(getString(R.string.author_formatted, articleAuthor)); } else { - author.setText(""); + holder.authorView.setText(""); } } - - /* ImageView separator = (ImageView)v.findViewById(R.id.headlines_separator); - - if (separator != null && m_onlineServices.isSmallScreen()) { - separator.setVisibility(View.GONE); - } */ - - TextView dv = (TextView) v.findViewById(R.id.date); - - if (dv != null) { - dv.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize); + + if (holder.dateView != null) { + holder.dateView.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize); Date d = new Date((long)article.updated * 1000); DateFormat df = new SimpleDateFormat("MMM dd, HH:mm"); df.setTimeZone(TimeZone.getDefault()); - dv.setText(df.format(d)); + holder.dateView.setText(df.format(d)); } - CheckBox cb = (CheckBox) v.findViewById(R.id.selected); - if (cb != null) { - cb.setChecked(m_selectedArticles.contains(article)); - cb.setOnClickListener(new OnClickListener() { + if (holder.selectionBoxView != null) { + holder.selectionBoxView.setChecked(m_selectedArticles.contains(article)); + holder.selectionBoxView.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { @@ -948,14 +965,12 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, } }); } - - ImageView iv = (ImageView) v.findViewById(R.id.article_menu_button); - - if (iv != null) { + + if (holder.menuButtonView != null) { //if (m_activity.isDarkTheme()) // ib.setImageResource(R.drawable.ic_mailbox_collapsed_holo_dark); - iv.setOnClickListener(new OnClickListener() { + holder.menuButtonView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { getActivity().openContextMenu(v); diff --git a/src/org/fox/ttrss/util/EnlargingImageView.java b/src/org/fox/ttrss/util/EnlargingImageView.java new file mode 100644 index 00000000..b6f9bce9 --- /dev/null +++ b/src/org/fox/ttrss/util/EnlargingImageView.java @@ -0,0 +1,252 @@ +package org.fox.ttrss.util;
+
+/*
+ * Copyright (C) 2013 Tomáš Procházka
+ *
+ * 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.
+ */
+
+import java.lang.reflect.Field;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.View;
+
+/**
+ * Special version of ImageView which allow enlarge width of image if android:adjustViewBounds is true.
+ *
+ * <p>It simulate HTML behaviour <img src="" widh="100" /></p>
+ * <p><a href="http://stackoverflow.com/questions/6202000/imageview-one-dimension-to-fit-free-space-and-second-evaluate-to-keep-aspect-rati">Stackoverflow question link</a></p>
+ *
+ * <p>It also allow set related view which will be used as reference for size measure.</p>
+ *
+ * @author Tomáš Procházka <<a href="mailto:[email protected]">[email protected]</a>>
+ * @version $Revision: 0$ ($Date: 6.6.2011 18:16:52$)
+ */
+public class EnlargingImageView extends android.widget.ImageView {
+
+ private int mDrawableWidth;
+ private int mDrawableHeight;
+ private boolean mAdjustViewBoundsL;
+ private int mMaxWidthL = Integer.MAX_VALUE;
+ private int mMaxHeightL = Integer.MAX_VALUE;
+ private View relatedView;
+
+ public EnlargingImageView(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+
+ // hack for acces some private field of parent :-(
+ Field f;
+ try {
+ f = android.widget.ImageView.class.getDeclaredField("mAdjustViewBounds");
+ f.setAccessible(true);
+ setAdjustViewBounds((Boolean) f.get(this));
+
+ f = android.widget.ImageView.class.getDeclaredField("mMaxWidth");
+ f.setAccessible(true);
+ setMaxWidth((Integer) f.get(this));
+
+ f = android.widget.ImageView.class.getDeclaredField("mMaxHeight");
+ f.setAccessible(true);
+ setMaxHeight((Integer) f.get(this));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public EnlargingImageView(Context context, AttributeSet attrs) {
+ this(context, attrs, 0);
+ }
+
+ public EnlargingImageView(Context context) {
+ super(context);
+ }
+
+ public void setAdjustViewBounds(boolean adjustViewBounds) {
+ super.setAdjustViewBounds(adjustViewBounds);
+ mAdjustViewBoundsL = adjustViewBounds;
+ }
+
+ public void setMaxWidth(int maxWidth) {
+ super.setMaxWidth(maxWidth);
+ mMaxWidthL = maxWidth;
+ }
+
+ public void setMaxHeight(int maxHeight) {
+ super.setMaxHeight(maxHeight);
+ mMaxHeightL = maxHeight;
+ }
+
+ @Override
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+
+ if (getDrawable() == null) {
+ setMeasuredDimension(0, 0);
+ return;
+ }
+
+ mDrawableWidth = getDrawable().getIntrinsicWidth();
+ mDrawableHeight = getDrawable().getIntrinsicHeight();
+
+ int w = 0;
+ int h = 0;
+
+ // Desired aspect ratio of the view's contents (not including padding)
+ float desiredAspect = 0.0f;
+
+ // We are allowed to change the view's width
+ boolean resizeWidth = false;
+
+ // We are allowed to change the view's height
+ boolean resizeHeight = false;
+
+ if (mDrawableWidth > 0) {
+ w = mDrawableWidth;
+ h = mDrawableHeight;
+ if (w <= 0) w = 1;
+ if (h <= 0) h = 1;
+
+ // We are supposed to adjust view bounds to match the aspect
+ // ratio of our drawable. See if that is possible.
+ if (mAdjustViewBoundsL) {
+
+ int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
+ int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
+
+ resizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
+ resizeHeight = heightSpecMode != MeasureSpec.EXACTLY;
+
+ desiredAspect = (float) w / (float) h;
+ }
+ }
+
+ int pleft = getPaddingLeft();
+ int pright = getPaddingRight();
+ int ptop = getPaddingTop();
+ int pbottom = getPaddingBottom();
+
+ int widthSize;
+ int heightSize;
+
+ if (resizeWidth || resizeHeight) {
+ /* If we get here, it means we want to resize to match the
+ drawables aspect ratio, and we have the freedom to change at
+ least one dimension.
+ */
+
+ // Get the max possible width given our constraints
+ widthSize = resolveAdjustedSize(w + pleft + pright,
+ mMaxWidthL, widthMeasureSpec);
+
+ // Get the max possible height given our constraints
+ heightSize = resolveAdjustedSize(h + ptop + pbottom,
+ mMaxHeightL, heightMeasureSpec);
+
+ if (desiredAspect != 0.0f) {
+ // See what our actual aspect ratio is
+ float actualAspect = (float) (widthSize - pleft - pright) /
+ (heightSize - ptop - pbottom);
+
+ if (Math.abs(actualAspect - desiredAspect) > 0.0000001) {
+
+ // Try adjusting width to be proportional to height
+ if (resizeWidth) {
+ int newWidth = (int) (desiredAspect * (heightSize - ptop - pbottom)) + pleft + pright;
+ if (/*newWidth <= widthSize &&*/newWidth > 0) {
+ widthSize = Math.min(newWidth, mMaxWidthL);
+ heightSize = (int) ((widthSize - pleft - pright) / desiredAspect) + ptop + pbottom;
+ }
+ }
+
+ // Try adjusting height to be proportional to width
+ if (resizeHeight) {
+ int newHeight = (int) ((widthSize - pleft - pright) / desiredAspect) + ptop + pbottom;
+ if (/*newHeight <= heightSize && */newHeight > 0) {
+ heightSize = Math.min(newHeight, mMaxHeightL);
+ widthSize = (int) (desiredAspect * (heightSize - ptop - pbottom)) + pleft + pright;
+ }
+ }
+ }
+ }
+ } else {
+ /* We are either don't want to preserve the drawables aspect ratio,
+ or we are not allowed to change view dimensions. Just measure in
+ the normal way.
+ */
+ w += pleft + pright;
+ h += ptop + pbottom;
+
+ w = Math.max(w, getSuggestedMinimumWidth());
+ h = Math.max(h, getSuggestedMinimumHeight());
+
+ widthSize = resolveSize(w, widthMeasureSpec);
+ heightSize = resolveSize(h, heightMeasureSpec);
+ }
+
+ //Log.d(Constants.LOGTAG, mDrawableWidth + ":" + mDrawableHeight + " to " + widthSize + ":" + heightSize);
+
+ setMeasuredDimension(widthSize, heightSize);
+
+ if (relatedView != null) {
+ //Log.i(Constants.LOGTAG, getTag() + " onMeasure:" + widthSize + ", " + heightSize + " update size of related view!");
+ relatedView.getLayoutParams().width = widthSize;
+ relatedView.getLayoutParams().height = heightSize;
+ }
+
+ }
+
+ @Override
+ protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+ super.onLayout(changed, left, top, right, bottom);
+ //Log.d(Constants.LOGTAG, getTag() + " onLayout:" + left + ", " + top + ", " + right + ", " + bottom);
+ }
+
+ /**
+ * Experimental. This view will be set to the same size as this image.
+ */
+ public void setRelatedView(View view) {
+ relatedView = view;
+ }
+
+ @Override
+ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+ super.onSizeChanged(w, h, oldw, oldh);
+ //Log.d(Constants.LOGTAG, getTag() + " onSizeChanged:" + w + ", " + h + ", " + oldw + ", " + oldh);
+ }
+
+ private int resolveAdjustedSize(int desiredSize, int maxSize, int measureSpec) {
+ int result = desiredSize;
+ int specMode = MeasureSpec.getMode(measureSpec);
+ int specSize = MeasureSpec.getSize(measureSpec);
+ switch (specMode) {
+ case MeasureSpec.UNSPECIFIED:
+ /* Parent says we can be as big as we want. Just don't be larger
+ than max size imposed on ourselves.
+ */
+ result = Math.min(desiredSize, maxSize);
+ break;
+ case MeasureSpec.AT_MOST:
+ // Parent says we can be as big as we want, up to specSize.
+ // Don't be larger than specSize, and don't be larger than
+ // the max size imposed on ourselves.
+ result = Math.min(Math.min(desiredSize, specSize), maxSize);
+ break;
+ case MeasureSpec.EXACTLY:
+ // No choice. Do what we are told.
+ result = specSize;
+ break;
+ }
+ return result;
+ }
+}
\ No newline at end of file |