Use StaggeredGridLayoutManager for gallery thumb list

This commit is contained in:
seven332 committed 2016-03-21 00:49:28 +08:00
1 parent 5706771c88
commit 0d8054b59d
8 files changed
+172 -73

No files matched your search

@@ -23,6 +23,7 @@ import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.DrawerLayout;
@@ -75,7 +76,6 @@ import com.hippo.util.DrawableManager;
import com.hippo.widget.ContentLayout;
import com.hippo.widget.FabLayout;
import com.hippo.widget.SearchBarMover;
import com.hippo.widget.recyclerview.GridAutoSpanLayoutManager;
import com.hippo.widget.refreshlayout.RefreshLayout;
import com.hippo.yorozuya.AssertUtils;
import com.hippo.yorozuya.ObjectUtils;
@@ -263,8 +263,8 @@ public class FavoritesScene extends BaseScene implements
contentLayout.setHelper(mHelper);
contentLayout.getFastScroller().setOnDragHandlerListener(this);
GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(context, 0);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new FavoritesAdapter(inflater, resources, mRecyclerView, Settings.getListMode());
mAdapter.register();
mRecyclerView.setSelector(RippleSalon.generateRippleDrawable(false));
mRecyclerView.setDrawSelectorOnTop(true);
mRecyclerView.hasFixedSize();
@@ -273,10 +273,6 @@ public class FavoritesScene extends BaseScene implements
mRecyclerView.setOnItemLongClickListener(this);
mRecyclerView.setChoiceMode(EasyRecyclerView.CHOICE_MODE_MULTIPLE_CUSTOM);
mRecyclerView.setCustomCheckedListener(this);
mAdapter = new FavoritesAdapter(getLayoutInflater2(),
getResources2(), mRecyclerView, layoutManager, Settings.getListMode());
mRecyclerView.setAdapter(mAdapter);
mAdapter.register();
mRecyclerView.setPadding(mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop() + paddingTopSB,
mRecyclerView.getPaddingRight(), mRecyclerView.getPaddingBottom());
@@ -955,9 +951,9 @@ public class FavoritesScene extends BaseScene implements
private class FavoritesAdapter extends GalleryAdapter {
public FavoritesAdapter(LayoutInflater inflater, Resources resources,
RecyclerView recyclerView, GridAutoSpanLayoutManager layoutManager, int type) {
super(inflater, resources, recyclerView, layoutManager, type);
public FavoritesAdapter(@NonNull LayoutInflater inflater, @NonNull Resources resources,
@NonNull RecyclerView recyclerView, int type) {
super(inflater, resources, recyclerView, type);
}
@Override
@@ -23,8 +23,8 @@ 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.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -38,8 +38,10 @@ import com.hippo.ehviewer.Settings;
import com.hippo.ehviewer.client.EhCacheKeyFactory;
import com.hippo.ehviewer.client.EhUtils;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.widget.TileThumb;
import com.hippo.util.ApiHelper;
import com.hippo.widget.recyclerview.GridAutoSpanLayoutManager;
import com.hippo.widget.recyclerview.AutoGridLayoutManager;
import com.hippo.widget.recyclerview.AutoStaggeredGridLayoutManager;
import com.hippo.yorozuya.Messenger;
import java.lang.annotation.Retention;
@@ -58,19 +60,19 @@ abstract class GalleryAdapter extends RecyclerView.Adapter<GalleryHolder> implem
private final LayoutInflater mInflater;
private final Resources mResources;
private final RecyclerView mRecyclerView;
private final GridAutoSpanLayoutManager mLayoutManager;
private final AutoStaggeredGridLayoutManager 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,
@NonNull RecyclerView recyclerView, @NonNull GridAutoSpanLayoutManager layoutManager, int type) {
@NonNull RecyclerView recyclerView, int type) {
mInflater = inflater;
mResources = resources;
mRecyclerView = recyclerView;
mLayoutManager = layoutManager;
mDefaultSpanSizeLookup = mLayoutManager.getSpanSizeLookup();
mLayoutManager = new AutoStaggeredGridLayoutManager(0, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setAdapter(this);
mRecyclerView.setLayoutManager(mLayoutManager);
int paddingH = mResources.getDimensionPixelOffset(R.dimen.list_content_margin_h);
int paddingV = mResources.getDimensionPixelOffset(R.dimen.list_content_margin_v);
@@ -93,8 +95,7 @@ abstract class GalleryAdapter extends RecyclerView.Adapter<GalleryHolder> implem
case GalleryAdapter.TYPE_LIST: {
int columnWidth = mResources.getDimensionPixelOffset(R.dimen.gallery_list_column_width);
mLayoutManager.setColumnSize(columnWidth);
mLayoutManager.setStrategy(GridAutoSpanLayoutManager.STRATEGY_MIN_SIZE);
mLayoutManager.setSpanSizeLookup(mDefaultSpanSizeLookup);
mLayoutManager.setStrategy(AutoGridLayoutManager.STRATEGY_MIN_SIZE);
if (null != mGirdDecoration) {
mRecyclerView.removeItemDecoration(mGirdDecoration);
}
@@ -104,11 +105,7 @@ abstract class GalleryAdapter extends RecyclerView.Adapter<GalleryHolder> implem
case GalleryAdapter.TYPE_GRID: {
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);
mLayoutManager.setStrategy(AutoGridLayoutManager.STRATEGY_SUITABLE_SIZE);
if (null == mGirdDecoration) {
mGirdDecoration = new MarginItemDecoration(
mResources.getDimensionPixelOffset(R.dimen.gallery_grid_margin) / 2);
@@ -169,6 +166,7 @@ abstract class GalleryAdapter extends RecyclerView.Adapter<GalleryHolder> implem
break;
}
case TYPE_GRID: {
((TileThumb) holder.thumb).setThumbSize(gi.thumbWidth, gi.thumbHeight);
holder.thumb.load(EhCacheKeyFactory.getThumbKey(gi.gid), gi.thumb);
View category = holder.category;
Drawable drawable = category.getBackground();
@@ -204,17 +202,4 @@ abstract class GalleryAdapter extends RecyclerView.Adapter<GalleryHolder> 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;
}
}
}
}
@@ -79,7 +79,6 @@ import com.hippo.util.DrawableManager;
import com.hippo.view.ViewTransition;
import com.hippo.widget.ContentLayout;
import com.hippo.widget.SearchBarMover;
import com.hippo.widget.recyclerview.GridAutoSpanLayoutManager;
import com.hippo.widget.refreshlayout.RefreshLayout;
import com.hippo.yorozuya.AnimationUtils;
import com.hippo.yorozuya.AssertUtils;
@@ -363,18 +362,15 @@ public final class GalleryListScene extends BaseScene
contentLayout.setHelper(mHelper);
contentLayout.getFastScroller().setOnDragHandlerListener(this);
GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(context, 0);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new GalleryListAdapter(inflater, resources,
mRecyclerView, Settings.getListMode());
mAdapter.register();
mRecyclerView.setSelector(RippleSalon.generateRippleDrawable(false));
mRecyclerView.setDrawSelectorOnTop(true);
mRecyclerView.hasFixedSize();
mRecyclerView.setClipToPadding(false);
mRecyclerView.setOnItemClickListener(this);
mRecyclerView.setOnItemLongClickListener(this);
mAdapter = new GalleryListAdapter(getLayoutInflater2(), getResources2(),
mRecyclerView, layoutManager, Settings.getListMode());
mRecyclerView.setAdapter(mAdapter);
mAdapter.register();
mRecyclerView.setPadding(mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop() + paddingTopSB,
mRecyclerView.getPaddingRight(), mRecyclerView.getPaddingBottom());
@@ -994,9 +990,9 @@ public final class GalleryListScene extends BaseScene
private class GalleryListAdapter extends GalleryAdapter {
public GalleryListAdapter(LayoutInflater inflater, Resources resources,
RecyclerView recyclerView, GridAutoSpanLayoutManager layoutManager, int type) {
super(inflater, resources, recyclerView, layoutManager, type);
public GalleryListAdapter(@NonNull LayoutInflater inflater,
@NonNull Resources resources, @NonNull RecyclerView recyclerView, int type) {
super(inflater, resources, recyclerView, type);
}
@Override
@@ -51,7 +51,7 @@ import com.hippo.scene.SceneFragment;
import com.hippo.widget.ContentLayout;
import com.hippo.widget.LoadImageView;
import com.hippo.widget.Slider;
import com.hippo.widget.recyclerview.GridAutoSpanLayoutManager;
import com.hippo.widget.recyclerview.AutoGridLayoutManager;
import com.hippo.yorozuya.AssertUtils;
import com.hippo.yorozuya.LayoutUtils;
import com.hippo.yorozuya.SimpleHandler;
@@ -141,8 +141,8 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi
mAdapter = new GalleryPreviewAdapter();
recyclerView.setAdapter(mAdapter);
int columnWidth = resources.getDimensionPixelOffset(R.dimen.preview_grid_column_width);
GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(context, columnWidth);
layoutManager.setStrategy(GridAutoSpanLayoutManager.STRATEGY_SUITABLE_SIZE);
AutoGridLayoutManager layoutManager = new AutoGridLayoutManager(context, columnWidth);
layoutManager.setStrategy(AutoGridLayoutManager.STRATEGY_SUITABLE_SIZE);
recyclerView.setLayoutManager(layoutManager);
int padding = LayoutUtils.dp2pix(context, 4);
recyclerView.setPadding(padding, padding, padding, padding);
@@ -17,41 +17,36 @@
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;
import com.hippo.yorozuya.MathUtils;
public class TileThumb extends LoadImageView {
private int mMargin;
private static final float MIN_ASPECT = 0.3f;
private static final float MAX_ASPECT = 2.0f;
private static final float DEFAULT_ASPECT = 0.67f;
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);
public void setThumbSize(int thumbWidth, int thumbHeight) {
float aspect;
if (thumbWidth > 0 && thumbHeight > 0) {
aspect = MathUtils.clamp(thumbWidth / (float) thumbHeight, MIN_ASPECT, MAX_ASPECT);
} else {
aspect = DEFAULT_ASPECT;
}
setAspect(aspect);
}
}
@@ -23,7 +23,7 @@ import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class GridAutoSpanLayoutManager extends GridLayoutManager {
public class AutoGridLayoutManager extends GridLayoutManager {
public static final int STRATEGY_MIN_SIZE = 0;
public static final int STRATEGY_SUITABLE_SIZE = 1;
@@ -34,12 +34,12 @@ public class GridAutoSpanLayoutManager extends GridLayoutManager {
private List<OnUpdateSpanCountListener> mListeners;
public GridAutoSpanLayoutManager(Context context, int columnSize) {
public AutoGridLayoutManager(Context context, int columnSize) {
super(context, 1);
setColumnSize(columnSize);
}
public GridAutoSpanLayoutManager(Context context, int columnSize, int orientation, boolean reverseLayout) {
public AutoGridLayoutManager(Context context, int columnSize, int orientation, boolean reverseLayout) {
super(context, 1, orientation, reverseLayout);
setColumnSize(columnSize);
}
@@ -0,0 +1,127 @@
/*
* 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.widget.recyclerview;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class AutoStaggeredGridLayoutManager extends StaggeredGridLayoutManager {
public static final int STRATEGY_MIN_SIZE = 0;
public static final int STRATEGY_SUITABLE_SIZE = 1;
private int mColumnSize = -1;
private boolean mColumnSizeChanged = true;
private int mStrategy;
private List<OnUpdateSpanCountListener> mListeners;
public AutoStaggeredGridLayoutManager(int columnSize, int orientation) {
super(1, orientation);
setColumnSize(columnSize);
}
public void setColumnSize(int columnSize) {
if (columnSize == mColumnSize) {
return;
}
mColumnSize = columnSize;
mColumnSizeChanged = true;
}
public void setStrategy(int strategy) {
if (strategy == mStrategy) {
return;
}
mStrategy = strategy;
mColumnSizeChanged = true;
}
public static int getSpanCountForSuitableSize(int total, int single) {
int span = total / single;
if (span <= 0) {
return 1;
}
int span2 = span + 1;
float deviation = Math.abs(1 - (total / span / (float) single));
float deviation2 = Math.abs(1 - (total / span2 / (float) single));
return deviation < deviation2 ? span : span2;
}
public static int getSpanCountForMinSize(int total, int single) {
return Math.max(1, total / single);
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
if (mColumnSizeChanged && mColumnSize > 0) {
int totalSpace;
if (getOrientation() == VERTICAL) {
if (View.MeasureSpec.EXACTLY != View.MeasureSpec.getMode(widthSpec)) {
throw new IllegalStateException("RecyclerView need a fixed width for AutoStaggeredGridLayoutManager");
}
totalSpace = View.MeasureSpec.getSize(widthSpec) - getPaddingRight() - getPaddingLeft();
} else {
if (View.MeasureSpec.EXACTLY != View.MeasureSpec.getMode(heightSpec)) {
throw new IllegalStateException("RecyclerView need a fixed height for AutoStaggeredGridLayoutManager");
}
totalSpace = View.MeasureSpec.getSize(heightSpec) - getPaddingTop() - getPaddingBottom();
}
int spanCount;
switch (mStrategy) {
default:
case STRATEGY_MIN_SIZE:
spanCount = getSpanCountForMinSize(totalSpace, mColumnSize);
break;
case STRATEGY_SUITABLE_SIZE:
spanCount = getSpanCountForSuitableSize(totalSpace, mColumnSize);
break;
}
setSpanCount(spanCount);
mColumnSizeChanged = false;
if (null != mListeners) {
for (int i = 0, n = mListeners.size(); i < n; i++) {
mListeners.get(i).onUpdateSpanCount(spanCount);
}
}
}
super.onMeasure(recycler, state, widthSpec, heightSpec);
}
public void addOnUpdateSpanCountListener(OnUpdateSpanCountListener listener) {
if (null == mListeners) {
mListeners = new ArrayList<>();
}
mListeners.add(listener);
}
public void removeOnUpdateSpanCountListener(OnUpdateSpanCountListener listener) {
if (null != mListeners) {
mListeners.remove(listener);
}
}
public interface OnUpdateSpanCountListener {
void onUpdateSpanCount(int spanCount);
}
}