Convert LayoutManagerUtils to kotlin

This commit is contained in:
FooIbar authored and tarsin committed 2023-06-22 10:01:41 +08:00
1 parent 862b3e5e46
commit e6312a37c7
2 files changed
+118 -118

No files matched your search

@@ -298,7 +298,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout
}
val firstVisibleItem: GalleryInfo?
get() = getDataAtEx(LayoutManagerUtils.getFirstVisibleItemPosition(binding.recyclerView.layoutManager))
get() = getDataAtEx(LayoutManagerUtils.getFirstVisibleItemPosition(binding.recyclerView.layoutManager!!))
fun size(): Int {
return mData.size
@@ -410,7 +410,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout
if (mCurrentTaskType == TYPE_PRE_PAGE_KEEP_POS) {
binding.recyclerView.stopScroll()
LayoutManagerUtils.scrollToPositionProperly(
binding.recyclerView.layoutManager,
binding.recyclerView.layoutManager!!,
context,
dataSize - 1,
mOnScrollToPositionListener,
@@ -573,7 +573,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout
private fun scrollToPosition(position: Int) {
binding.recyclerView.stopScroll()
LayoutManagerUtils.scrollToPositionWithOffset(
binding.recyclerView.layoutManager,
binding.recyclerView.layoutManager!!,
position,
0,
)
@@ -675,9 +675,9 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout
}
val pageForTop: Int
get() = getPageForPosition(LayoutManagerUtils.getFirstVisibleItemPosition(binding.recyclerView.layoutManager))
get() = getPageForPosition(LayoutManagerUtils.getFirstVisibleItemPosition(binding.recyclerView.layoutManager!!))
val pageForBottom: Int
get() = getPageForPosition(LayoutManagerUtils.getLastVisibleItemPosition(binding.recyclerView.layoutManager))
get() = getPageForPosition(LayoutManagerUtils.getLastVisibleItemPosition(binding.recyclerView.layoutManager!!))
fun canGoTo(): Boolean {
return isContentShowing
@@ -13,139 +13,139 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.legacy
package com.hippo.ehviewer.ui.legacy;
import android.content.Context
import android.graphics.PointF
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.hippo.ehviewer.yorozuya.MathUtils
import com.hippo.ehviewer.yorozuya.SimpleHandler
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import kotlin.math.abs
import android.content.Context;
import android.graphics.PointF;
object LayoutManagerUtils {
private var sCsdfp: Method? = null
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.hippo.ehviewer.yorozuya.MathUtils;
import com.hippo.ehviewer.yorozuya.SimpleHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public final class LayoutManagerUtils {
private static Method sCsdfp = null;
static {
init {
try {
sCsdfp = StaggeredGridLayoutManager.class.getDeclaredMethod("calculateScrollDirectionForPosition", int.class);
sCsdfp.setAccessible(true);
} catch (NoSuchMethodException e) {
sCsdfp = StaggeredGridLayoutManager::class.java.getDeclaredMethod(
"calculateScrollDirectionForPosition",
Int::class.javaPrimitiveType
).apply { isAccessible = true }
} catch (e: NoSuchMethodException) {
// Ignore
e.printStackTrace();
e.printStackTrace()
}
}
public static void scrollToPositionWithOffset(
RecyclerView.LayoutManager layoutManager, int position, int offset) {
if (layoutManager instanceof LinearLayoutManager) {
((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(position, offset);
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
((StaggeredGridLayoutManager) layoutManager).scrollToPositionWithOffset(position, offset);
} else {
throw new IllegalStateException("Can't do scrollToPositionWithOffset for " +
layoutManager.getClass().getName());
fun scrollToPositionWithOffset(
layoutManager: RecyclerView.LayoutManager, position: Int, offset: Int
) {
when (layoutManager) {
is LinearLayoutManager -> layoutManager.scrollToPositionWithOffset(position, offset)
is StaggeredGridLayoutManager ->
layoutManager.scrollToPositionWithOffset(position, offset)
else -> throw IllegalStateException("Can't do scrollToPositionWithOffset for " + layoutManager.javaClass.name)
}
}
public static void smoothScrollToPosition(
RecyclerView.LayoutManager layoutManager, Context context, int position) {
smoothScrollToPosition(layoutManager, context, position, -1);
}
public static void smoothScrollToPosition(
RecyclerView.LayoutManager layoutManager, Context context, int position,
int millisecondsPerInch) {
SimpleSmoothScroller smoothScroller;
if (layoutManager instanceof final LinearLayoutManager linearLayoutManager) {
smoothScroller = new SimpleSmoothScroller(context, millisecondsPerInch) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return linearLayoutManager.computeScrollVectorForPosition(targetPosition);
}
};
} else if (layoutManager instanceof final StaggeredGridLayoutManager staggeredGridLayoutManager) {
smoothScroller = new SimpleSmoothScroller(context, millisecondsPerInch) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
int direction = 0;
try {
direction = (Integer) sCsdfp.invoke(staggeredGridLayoutManager, targetPosition);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
@JvmOverloads
fun smoothScrollToPosition(
layoutManager: RecyclerView.LayoutManager, context: Context?, position: Int,
millisecondsPerInch: Int = -1
) {
val smoothScroller: SimpleSmoothScroller
when (layoutManager) {
is LinearLayoutManager -> {
smoothScroller =
object : SimpleSmoothScroller(context, millisecondsPerInch.toFloat()) {
override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
return layoutManager.computeScrollVectorForPosition(targetPosition)
}
}
if (direction == 0) {
return null;
}
if (staggeredGridLayoutManager.getOrientation() == StaggeredGridLayoutManager.HORIZONTAL) {
return new PointF(direction, 0);
} else {
return new PointF(0, direction);
}
}
};
} else {
throw new IllegalStateException("Can't do smoothScrollToPosition for " +
layoutManager.getClass().getName());
}
smoothScroller.setTargetPosition(position);
layoutManager.startSmoothScroll(smoothScroller);
}
public static void scrollToPositionProperly(final RecyclerView.LayoutManager layoutManager,
final Context context, final int position, final OnScrollToPositionListener listener) {
SimpleHandler.getInstance().postDelayed(() -> {
int first = getFirstVisibleItemPosition(layoutManager);
int last = getLastVisibleItemPosition(layoutManager);
int offset = Math.abs(position - first);
int max = last - first;
if (offset < max && max > 0) {
smoothScrollToPosition(layoutManager, context, position,
MathUtils.lerp(100, 25, (offset / max)));
} else {
scrollToPositionWithOffset(layoutManager, position, 0);
if (listener != null) {
listener.onScrollToPosition(position);
}
}
}, 200);
is StaggeredGridLayoutManager -> {
smoothScroller =
object : SimpleSmoothScroller(context, millisecondsPerInch.toFloat()) {
override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
var direction = 0
try {
direction = sCsdfp!!.invoke(layoutManager, targetPosition) as Int
} catch (e: IllegalAccessException) {
e.printStackTrace()
} catch (e: InvocationTargetException) {
e.printStackTrace()
}
if (direction == 0) {
return null
}
return if (layoutManager.orientation == StaggeredGridLayoutManager.HORIZONTAL) {
PointF(direction.toFloat(), 0f)
} else {
PointF(0f, direction.toFloat())
}
}
}
}
else -> throw IllegalStateException("Can't do smoothScrollToPosition for " + layoutManager.javaClass.name)
}
smoothScroller.targetPosition = position
layoutManager.startSmoothScroll(smoothScroller)
}
public static int getFirstVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof LinearLayoutManager) {
return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int[] positions = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null);
return MathUtils.min(positions);
} else {
throw new IllegalStateException("Can't do getFirstVisibleItemPosition for " +
layoutManager.getClass().getName());
fun scrollToPositionProperly(
layoutManager: RecyclerView.LayoutManager,
context: Context?, position: Int, listener: OnScrollToPositionListener?
) {
SimpleHandler.postDelayed({
val first = getFirstVisibleItemPosition(layoutManager)
val last = getLastVisibleItemPosition(layoutManager)
val offset = abs(position - first)
val max = last - first
if (offset < max && max > 0) {
smoothScrollToPosition(
layoutManager, context, position,
MathUtils.lerp(100, 25, (offset / max).toFloat())
)
} else {
scrollToPositionWithOffset(layoutManager, position, 0)
listener?.onScrollToPosition(position)
}
}, 200)
}
fun getFirstVisibleItemPosition(layoutManager: RecyclerView.LayoutManager): Int {
return when (layoutManager) {
is LinearLayoutManager -> layoutManager.findFirstVisibleItemPosition()
is StaggeredGridLayoutManager -> {
val positions = layoutManager.findFirstVisibleItemPositions(null)
MathUtils.min(*positions)
}
else -> throw IllegalStateException("Can't do getFirstVisibleItemPosition for " + layoutManager.javaClass.name)
}
}
public static int getLastVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof LinearLayoutManager) {
return ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int[] positions = ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(null);
return MathUtils.max(positions);
} else {
throw new IllegalStateException("Can't do getLastVisibleItemPosition for " +
layoutManager.getClass().getName());
fun getLastVisibleItemPosition(layoutManager: RecyclerView.LayoutManager): Int {
return when (layoutManager) {
is LinearLayoutManager -> layoutManager.findLastVisibleItemPosition()
is StaggeredGridLayoutManager -> {
val positions = layoutManager.findLastVisibleItemPositions(null)
MathUtils.max(*positions)
}
else -> throw IllegalStateException("Can't do getLastVisibleItemPosition for " + layoutManager.javaClass.name)
}
}
public interface OnScrollToPositionListener {
void onScrollToPosition(int position);
fun interface OnScrollToPositionListener {
fun onScrollToPosition(position: Int)
}
}