Convert AutoGridLayoutManager to kotlin

This commit is contained in:
FooIbar authored and tarsin committed 2023-03-10 11:00:24 +08:00
1 parent 3f8e26644a
commit 15ae3cecb1
6 files changed
+55 -115

No files matched your search

@@ -66,6 +66,7 @@ import com.hippo.widget.FabLayout
import com.hippo.widget.FabLayout.OnClickFabListener
import com.hippo.widget.FabLayout.OnExpandListener
import com.hippo.widget.recyclerview.AutoStaggeredGridLayoutManager
import com.hippo.widget.recyclerview.STRATEGY_MIN_SIZE
import com.hippo.yorozuya.FileUtils
import com.hippo.yorozuya.ObjectUtils
import com.hippo.yorozuya.ViewUtils
@@ -244,7 +245,7 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe
val layoutManager =
AutoStaggeredGridLayoutManager(0, StaggeredGridLayoutManager.VERTICAL)
layoutManager.setColumnSize(resources.getDimensionPixelOffset(Settings.detailSizeResId))
layoutManager.setStrategy(AutoStaggeredGridLayoutManager.STRATEGY_MIN_SIZE)
layoutManager.setStrategy(STRATEGY_MIN_SIZE)
layoutManager.supportsPredictiveItemAnimations = false
recyclerView.layoutManager = layoutManager
recyclerView.clipToPadding = false
@@ -30,6 +30,8 @@ import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.databinding.ItemGalleryGridBinding
import com.hippo.ehviewer.databinding.ItemGalleryListBinding
import com.hippo.widget.recyclerview.AutoStaggeredGridLayoutManager
import com.hippo.widget.recyclerview.STRATEGY_MIN_SIZE
import com.hippo.widget.recyclerview.STRATEGY_SUITABLE_SIZE
import com.hippo.yorozuya.ViewUtils
internal abstract class GalleryAdapter(
@@ -59,7 +61,7 @@ internal abstract class GalleryAdapter(
TYPE_LIST -> {
val columnWidth = mResources.getDimensionPixelOffset(Settings.detailSizeResId)
mLayoutManager.setColumnSize(columnWidth)
mLayoutManager.setStrategy(AutoStaggeredGridLayoutManager.STRATEGY_MIN_SIZE)
mLayoutManager.setStrategy(STRATEGY_MIN_SIZE)
if (null != mGirdDecoration) {
recyclerView.removeItemDecoration(mGirdDecoration!!)
}
@@ -80,7 +82,7 @@ internal abstract class GalleryAdapter(
TYPE_GRID -> {
val columnWidth = Settings.thumbSize
mLayoutManager.setColumnSize(columnWidth)
mLayoutManager.setStrategy(AutoStaggeredGridLayoutManager.STRATEGY_SUITABLE_SIZE)
mLayoutManager.setStrategy(STRATEGY_SUITABLE_SIZE)
if (null != mListDecoration) {
recyclerView.removeItemDecoration(mListDecoration!!)
}
@@ -36,6 +36,7 @@ import com.hippo.ehviewer.databinding.SceneGalleryPreviewsBinding
import com.hippo.util.getParcelableCompat
import com.hippo.widget.ContentLayout.ContentHelper
import com.hippo.widget.recyclerview.AutoGridLayoutManager
import com.hippo.widget.recyclerview.STRATEGY_SUITABLE_SIZE
import com.hippo.yorozuya.LayoutUtils
import java.util.Locale
@@ -93,7 +94,7 @@ class GalleryPreviewsScene : BaseToolbarScene() {
val columnWidth = Settings.thumbSize
val layoutManager =
AutoGridLayoutManager(context, columnWidth, LayoutUtils.dp2pix(context, 16f))
layoutManager.setStrategy(AutoGridLayoutManager.STRATEGY_SUITABLE_SIZE)
layoutManager.setStrategy(STRATEGY_SUITABLE_SIZE)
binding.contentLayout.recyclerView.layoutManager = layoutManager
binding.contentLayout.recyclerView.clipToPadding = false
val padding = LayoutUtils.dp2pix(context, 4f)
@@ -59,6 +59,7 @@ import com.hippo.ehviewer.ui.dialog.SelectItemWithIconAdapter
import com.hippo.ehviewer.ui.widget.ListInfoCard
import com.hippo.view.ViewTransition
import com.hippo.widget.recyclerview.AutoStaggeredGridLayoutManager
import com.hippo.widget.recyclerview.STRATEGY_MIN_SIZE
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.withUIContext
@@ -145,7 +146,7 @@ class HistoryScene : BaseToolbarScene() {
binding.recyclerView.adapter = mAdapter
val layoutManager = AutoStaggeredGridLayoutManager(0, StaggeredGridLayoutManager.VERTICAL)
layoutManager.setColumnSize(resources.getDimensionPixelOffset(Settings.detailSizeResId))
layoutManager.setStrategy(AutoStaggeredGridLayoutManager.STRATEGY_MIN_SIZE)
layoutManager.setStrategy(STRATEGY_MIN_SIZE)
layoutManager.supportsPredictiveItemAnimations = false
binding.recyclerView.layoutManager = layoutManager
val itemTouchHelper = ItemTouchHelper(HistoryItemTouchHelperCallback())
@@ -13,117 +13,58 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.widget.recyclerview
package com.hippo.widget.recyclerview;
import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Recycler
import android.content.Context;
class AutoGridLayoutManager(context: Context?, columnSize: Int, private var fakePadding: Int = 0) :
GridLayoutManager(context, 1) {
private var mColumnSize = columnSize
private var mColumnSizeChanged = true
private var mStrategy = 0
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class AutoGridLayoutManager extends GridLayoutManager {
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 int fakePadding;
private List<OnUpdateSpanCountListener> mListeners;
public AutoGridLayoutManager(Context context, int columnSize, int fakePadding) {
super(context, 1);
this.fakePadding = fakePadding;
setColumnSize(columnSize);
}
public AutoGridLayoutManager(Context context, int columnSize, int orientation, boolean reverseLayout) {
super(context, 1, orientation, reverseLayout);
setColumnSize(columnSize);
}
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);
}
public void setColumnSize(int columnSize) {
fun setColumnSize(columnSize: Int) {
if (columnSize == mColumnSize) {
return;
return
}
mColumnSize = columnSize;
mColumnSizeChanged = true;
mColumnSize = columnSize
mColumnSizeChanged = true
}
public void setStrategy(int strategy) {
fun setStrategy(strategy: Int) {
if (strategy == mStrategy) {
return;
return
}
mStrategy = strategy;
mColumnSizeChanged = true;
mStrategy = strategy
mColumnSizeChanged = true
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
override fun onLayoutChildren(recycler: Recycler, state: RecyclerView.State) {
if (mColumnSizeChanged && mColumnSize > 0) {
int totalSpace;
if (getOrientation() == RecyclerView.VERTICAL) {
totalSpace = getWidth() - getPaddingRight() - getPaddingLeft() - fakePadding;
val totalSpace = if (orientation == RecyclerView.VERTICAL) {
width - paddingRight - paddingLeft - fakePadding
} else {
totalSpace = getHeight() - getPaddingTop() - getPaddingBottom() - fakePadding;
height - paddingTop - paddingBottom - fakePadding
}
val spanCount = when (mStrategy) {
STRATEGY_MIN_SIZE -> getSpanCountForMinSize(
totalSpace,
mColumnSize
)
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;
STRATEGY_SUITABLE_SIZE -> getSpanCountForSuitableSize(
totalSpace,
mColumnSize
)
if (null != mListeners) {
for (int i = 0, n = mListeners.size(); i < n; i++) {
mListeners.get(i).onUpdateSpanCount(spanCount);
}
else -> getSpanCountForMinSize(totalSpace, mColumnSize)
}
setSpanCount(spanCount)
mColumnSizeChanged = false
}
super.onLayoutChildren(recycler, state);
super.onLayoutChildren(recycler, state)
}
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);
}
}
}
@@ -22,15 +22,11 @@ import kotlin.math.roundToInt
class AutoStaggeredGridLayoutManager(columnSize: Int, orientation: Int) :
StaggeredGridLayoutManager(1, orientation) {
private var mColumnSize = -1
private var mColumnSize = columnSize
private var mColumnSizeChanged = true
private var mStrategy = 0
var supportsPredictiveItemAnimations = true
init {
setColumnSize(columnSize)
}
fun setColumnSize(columnSize: Int) {
if (columnSize == mColumnSize) {
return
@@ -94,16 +90,14 @@ class AutoStaggeredGridLayoutManager(columnSize: Int, orientation: Int) :
}
super.onMeasure(recycler, state, widthSpec, heightSpec)
}
}
companion object {
const val STRATEGY_MIN_SIZE = 0
const val STRATEGY_SUITABLE_SIZE = 1
private fun getSpanCountForSuitableSize(total: Int, single: Int): Int {
return (total / single.toFloat()).roundToInt().coerceAtLeast(1)
}
const val STRATEGY_MIN_SIZE = 0
const val STRATEGY_SUITABLE_SIZE = 1
fun getSpanCountForSuitableSize(total: Int, single: Int): Int {
return (total / single.toFloat()).roundToInt().coerceAtLeast(1)
}
private fun getSpanCountForMinSize(total: Int, single: Int): Int {
return (total / single).coerceAtLeast(1)
}
}
fun getSpanCountForMinSize(total: Int, single: Int): Int {
return (total / single).coerceAtLeast(1)
}