From a4f958017134e2a590da62cb11b6f2d9e8dbae12 Mon Sep 17 00:00:00 2001 From: seven332 Date: Sun, 20 Mar 2016 02:03:36 +0800 Subject: [PATCH] Set span size 2 for wide thumb --- .../ehviewer/client/data/GalleryInfo.java | 3 + .../client/parser/GalleryListParser.java | 4 +- .../ehviewer/ui/scene/GalleryAdapter.java | 24 +++++++- .../hippo/ehviewer/ui/scene/HistoryScene.java | 1 + .../com/hippo/ehviewer/widget/TileThumb.java | 57 +++++++++++++++++++ .../java/com/hippo/widget/ContentLayout.java | 8 ++- app/src/main/res/layout/item_gallery_grid.xml | 3 +- app/src/main/res/values/dimens.xml | 2 + 8 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/hippo/ehviewer/widget/TileThumb.java diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java index 74755508c..5853b46cc 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java @@ -90,6 +90,9 @@ public class GalleryInfo implements Parcelable { @Nullable public String[] simpleTags; + public int thumbWidth; + public int thumbHeight; + /** * language from title */ diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java index 9a321ea06..918db5e02 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java @@ -79,8 +79,8 @@ public class GalleryListParser { gi.category = EhUtils.getCategory(ParserUtils.trim(m.group(1))); gi.posted = ParserUtils.trim(m.group(2)); - //gi.thumbHeight = ParserUtils.parseInt(m.group(3)); - //gi.thumbWidth = ParserUtils.parseInt(m.group(4)); + gi.thumbHeight = ParserUtils.parseInt(m.group(3)); + gi.thumbWidth = ParserUtils.parseInt(m.group(4)); if (m.group(5) == null) { gi.thumb = ParserUtils.trim("http://" + m.group(7).replace('~', '/')); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java index 2fca5d772..698690874 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java @@ -23,6 +23,7 @@ import android.os.Build; import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -59,6 +60,8 @@ abstract class GalleryAdapter extends RecyclerView.Adapter implem private final RecyclerView mRecyclerView; private final GridAutoSpanLayoutManager mLayoutManager; private RecyclerView.ItemDecoration mGirdDecoration; + private final GridLayoutManager.SpanSizeLookup mDefaultSpanSizeLookup; + private GridLayoutManager.SpanSizeLookup mThumbSpanSizeLookup; private int mType = TYPE_INVALID; public GalleryAdapter(@NonNull LayoutInflater inflater, @NonNull Resources resources, @@ -67,6 +70,7 @@ abstract class GalleryAdapter extends RecyclerView.Adapter implem mResources = resources; mRecyclerView = recyclerView; mLayoutManager = layoutManager; + mDefaultSpanSizeLookup = mLayoutManager.getSpanSizeLookup(); int paddingH = mResources.getDimensionPixelOffset(R.dimen.list_content_margin_h); int paddingV = mResources.getDimensionPixelOffset(R.dimen.list_content_margin_v); @@ -90,6 +94,7 @@ abstract class GalleryAdapter extends RecyclerView.Adapter implem int columnWidth = mResources.getDimensionPixelOffset(R.dimen.gallery_list_column_width); mLayoutManager.setColumnSize(columnWidth); mLayoutManager.setStrategy(GridAutoSpanLayoutManager.STRATEGY_MIN_SIZE); + mLayoutManager.setSpanSizeLookup(mDefaultSpanSizeLookup); if (null != mGirdDecoration) { mRecyclerView.removeItemDecoration(mGirdDecoration); } @@ -100,9 +105,13 @@ abstract class GalleryAdapter extends RecyclerView.Adapter implem int columnWidth = mResources.getDimensionPixelOffset(R.dimen.gallery_grid_column_width); mLayoutManager.setColumnSize(columnWidth); mLayoutManager.setStrategy(GridAutoSpanLayoutManager.STRATEGY_SUITABLE_SIZE); + if (null == mThumbSpanSizeLookup) { + mThumbSpanSizeLookup = new ThumbSpanSizeLookup(); + } + mLayoutManager.setSpanSizeLookup(mThumbSpanSizeLookup); if (null == mGirdDecoration) { mGirdDecoration = new MarginItemDecoration( - mResources.getDimensionPixelOffset(R.dimen.list_content_interval)); + mResources.getDimensionPixelOffset(R.dimen.gallery_grid_margin) / 2); } mRecyclerView.addItemDecoration(mGirdDecoration); notifyDataSetChanged(); @@ -195,4 +204,17 @@ abstract class GalleryAdapter extends RecyclerView.Adapter implem setType(Settings.getListMode()); notifyDataSetChanged(); } + + private class ThumbSpanSizeLookup extends GridLayoutManager.SpanSizeLookup { + + @Override + public int getSpanSize(int position) { + GalleryInfo info = getDataAt(position); + if (info == null || info.thumbWidth <= info.thumbHeight) { + return 1; + } else { + return 2; + } + } + } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.java index f7ce76db1..b0c418afe 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.java @@ -53,6 +53,7 @@ import com.hippo.yorozuya.ViewUtils; import de.greenrobot.dao.query.LazyList; +// TODO No grid for history public class HistoryScene extends ToolbarScene implements EasyRecyclerView.OnItemClickListener, EasyRecyclerView.OnItemLongClickListener{ diff --git a/app/src/main/java/com/hippo/ehviewer/widget/TileThumb.java b/app/src/main/java/com/hippo/ehviewer/widget/TileThumb.java new file mode 100644 index 000000000..0cd7cfca9 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/widget/TileThumb.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016 Hippo Seven + * + * 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.hippo.ehviewer.widget; + +import android.content.Context; +import android.support.v7.widget.GridLayoutManager; +import android.util.AttributeSet; +import android.view.ViewGroup; + +import com.hippo.ehviewer.R; +import com.hippo.widget.LoadImageView; + +public class TileThumb extends LoadImageView { + + private int mMargin; + + public TileThumb(Context context) { + super(context); + init(context); + } + + public TileThumb(Context context, AttributeSet attrs) { + super(context, attrs); + init(context); + } + + public TileThumb(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(context); + } + + private void init(Context context) { + mMargin = context.getResources().getDimensionPixelOffset(R.dimen.gallery_grid_margin); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int width = MeasureSpec.getSize(widthMeasureSpec); + int spanSize = ((GridLayoutManager.LayoutParams) ((ViewGroup) getParent()).getLayoutParams()).getSpanSize(); + int height = (int) ((width - (mMargin * (spanSize - 1))) / spanSize / 0.67f); + setMeasuredDimension(width, height); + } +} diff --git a/app/src/main/java/com/hippo/widget/ContentLayout.java b/app/src/main/java/com/hippo/widget/ContentLayout.java index d473b7e47..0ac3057c6 100644 --- a/app/src/main/java/com/hippo/widget/ContentLayout.java +++ b/app/src/main/java/com/hippo/widget/ContentLayout.java @@ -50,8 +50,6 @@ import java.util.List; public class ContentLayout extends FrameLayout { - private static final String TAG = ContentLayout.class.getSimpleName(); - private ProgressView mProgressView; private TextView mTipView; private ViewGroup mContentView; @@ -208,7 +206,7 @@ public class ContentLayout extends FrameLayout { /** * Generate task id */ - private IntIdGenerator mIdGenerator = new IntIdGenerator(); + private final IntIdGenerator mIdGenerator = new IntIdGenerator(); /** * Store the page divider index @@ -359,6 +357,10 @@ public class ContentLayout extends FrameLayout { mEmptyString = str; } + public List getDat() { + return mData; + } + /** * @throws IndexOutOfBoundsException * if {@code location < 0 || location >= size()} diff --git a/app/src/main/res/layout/item_gallery_grid.xml b/app/src/main/res/layout/item_gallery_grid.xml index 53988adae..abf914710 100644 --- a/app/src/main/res/layout/item_gallery_grid.xml +++ b/app/src/main/res/layout/item_gallery_grid.xml @@ -22,14 +22,13 @@ android:layout_height="wrap_content" android:foreground="@drawable/tile_background"> - 320dp 100dp + 8dp + 100dp