Skip to content

Commit

Permalink
[optimize] Optimize land layout
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Apr 9, 2024
1 parent a575afb commit f14ce28
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 57 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
minSdk = 24
targetSdk = 34
versionCode = 10
versionName = "1.1-beta03"
versionName = "1.1-beta04"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import com.skyd.anivu.ui.adapter.variety.VarietyAdapter
import kotlin.math.roundToInt


class AniVuItemDecoration : RecyclerView.ItemDecoration() {
class AniVuItemDecoration(
private val hItemSpace: Int = H_ITEM_SPACE,
private val horizontalSpace: Int = HORIZONTAL_PADDING,
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
Expand All @@ -27,67 +30,63 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
// 注意这里使用getChildLayoutPosition的目的
// 如果使用getChildAdapterPosition,刷新的时候可能会(边框)闪动一下,(返回-1)
?.getOrNull(parent.getChildLayoutPosition(view))
if (needVerticalMargin(item?.javaClass)) {
outRect.top = 10.dp
outRect.bottom = 2.dp
}
if (spanSize == MAX_SPAN_SIZE) {
/**
* 只有一列
*/
if (noHorizontalMargin(item?.javaClass)) return
outRect.left = HORIZONTAL_PADDING
outRect.right = HORIZONTAL_PADDING
outRect.left = horizontalSpace
outRect.right = horizontalSpace
} else if (spanSize == MAX_SPAN_SIZE / 2) {
/**
* 只有两列,没有在中间的item
* 2x = ITEM_SPACING
* 2x = hItemSpace
*/
val x: Int = (ITEM_SPACING / 2f).roundToInt()
val x: Int = (hItemSpace / 2f).roundToInt()
if (spanIndex == 0) {
outRect.left = HORIZONTAL_PADDING
outRect.left = horizontalSpace
outRect.right = x
} else {
outRect.left = x
outRect.right = HORIZONTAL_PADDING
outRect.right = horizontalSpace
}
} else if (spanSize == MAX_SPAN_SIZE / 3) {
/**
* 只有三列,一个在中间的item
* HORIZONTAL_PADDING + x = 2y
* x + y = ITEM_SPACING
* horizontalSpace + x = 2y
* x + y = hItemSpace
*/
val y: Int = ((HORIZONTAL_PADDING + ITEM_SPACING) / 3f).roundToInt()
val x: Int = ITEM_SPACING - y
val y: Int = ((horizontalSpace + hItemSpace) / 3f).roundToInt()
val x: Int = hItemSpace - y
if (spanIndex == 0) {
outRect.left = HORIZONTAL_PADDING
outRect.left = horizontalSpace
outRect.right = x
} else if (spanIndex + spanSize == MAX_SPAN_SIZE) {
// 最右侧最后一个
outRect.left = x
outRect.right = HORIZONTAL_PADDING
outRect.right = horizontalSpace
} else {
outRect.left = y
outRect.right = y
}
} else if (spanSize == MAX_SPAN_SIZE / 5) {
/**
* 只有五列
* HORIZONTAL_PADDING + x = y + z
* x + y = ITEM_SPACING
* z + (HORIZONTAL_PADDING + x) / 2 = ITEM_SPACING
* horizontalSpace + x = y + z
* x + y = hItemSpace
* z + (horizontalSpace + x) / 2 = hItemSpace
*/
val x: Int = ((4 * ITEM_SPACING - 3 * HORIZONTAL_PADDING) / 5f).roundToInt()
val y: Int = ITEM_SPACING - x
val z: Int = HORIZONTAL_PADDING + x - y
val x: Int = ((4 * hItemSpace - 3 * horizontalSpace) / 5f).roundToInt()
val y: Int = hItemSpace - x
val z: Int = horizontalSpace + x - y
if (spanIndex == 0) {
// 最左侧第一个
outRect.left = HORIZONTAL_PADDING
outRect.left = horizontalSpace
outRect.right = x
} else if (spanIndex + spanSize == MAX_SPAN_SIZE) {
// 最右侧最后一个
outRect.left = x
outRect.right = HORIZONTAL_PADDING
outRect.right = horizontalSpace
} else if (spanIndex == spanSize) {
// 第二个
outRect.left = y
Expand All @@ -98,8 +97,8 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
outRect.right = y
} else {
// 最中间的
outRect.left = ((HORIZONTAL_PADDING + x) / 2f).roundToInt()
outRect.right = ((HORIZONTAL_PADDING + x) / 2f).roundToInt()
outRect.left = ((horizontalSpace + x) / 2f).roundToInt()
outRect.right = ((horizontalSpace + x) / 2f).roundToInt()
}
} else {
/**
Expand All @@ -108,28 +107,28 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
if ((MAX_SPAN_SIZE / spanSize) % 2 == 0) {
/**
* 偶数个item
* HORIZONTAL_PADDING + x = y + ITEM_SPACING / 2
* x + y = ITEM_SPACING
* horizontalSpace + x = y + hItemSpace / 2
* x + y = hItemSpace
*/
val y: Int = ((HORIZONTAL_PADDING + ITEM_SPACING / 2f) / 2f).roundToInt()
val x: Int = ITEM_SPACING - y
val y: Int = ((horizontalSpace + hItemSpace / 2f) / 2f).roundToInt()
val x: Int = hItemSpace - y
if (spanIndex == 0) {
// 最左侧第一个
outRect.left = HORIZONTAL_PADDING
outRect.left = horizontalSpace
outRect.right = x
} else if (spanIndex + spanSize == MAX_SPAN_SIZE) {
// 最右侧最后一个
outRect.left = x
outRect.right = HORIZONTAL_PADDING
outRect.right = horizontalSpace
} else {
// 中间的项目
if (spanIndex < MAX_SPAN_SIZE / 2) {
// 左侧部分
outRect.left = y
outRect.right = ITEM_SPACING / 2
outRect.right = hItemSpace / 2
} else {
// 右侧部分
outRect.left = ITEM_SPACING / 2
outRect.left = hItemSpace / 2
outRect.right = y
}
}
Expand All @@ -142,8 +141,9 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
}

companion object {
val ITEM_SPACING: Int = 12.dp
val H_ITEM_SPACE: Int = 12.dp
val HORIZONTAL_PADDING: Int = 16.dp
val VERTICAL_PADDING: Int = 16.dp

private val noHorizontalMarginType: Set<Class<*>> = setOf(

Expand All @@ -153,14 +153,5 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
clz ?: return true
return clz in noHorizontalMarginType
}

private val needVerticalMarginType: Set<Class<*>> = setOf(

)

fun needVerticalMargin(clz: Class<*>?): Boolean {
clz ?: return false
return clz in needVerticalMarginType
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.skyd.anivu.appContext
import com.skyd.anivu.ext.screenIsLand
import com.skyd.anivu.model.bean.FeedBean
import com.skyd.anivu.model.bean.MoreBean
import com.skyd.anivu.model.bean.OtherWorksBean

class AniSpanSize(
private val adapter: VarietyAdapter,
Expand All @@ -18,6 +19,7 @@ class AniSpanSize(
when (data) {
is FeedBean -> MAX_SPAN_SIZE
is MoreBean -> MAX_SPAN_SIZE / 3
is OtherWorksBean -> MAX_SPAN_SIZE / 2
else -> MAX_SPAN_SIZE
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/skyd/anivu/ui/fragment/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.navigation.NavigationBarView
import com.skyd.anivu.base.BaseFragment
import com.skyd.anivu.databinding.FragmentMainBinding
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -21,6 +22,6 @@ class MainFragment : BaseFragment<FragmentMainBinding>() {
val navHostFragment = binding.navHostFragment.getFragment<NavHostFragment>()
val navController = navHostFragment.navController

binding.bottomNavigation.setupWithNavController(navController)
(binding.bottomNavigation as NavigationBarView).setupWithNavController(navController)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.skyd.anivu.ext.getAppVersionName
import com.skyd.anivu.ext.openBrowser
import com.skyd.anivu.ext.popBackStackWithLifecycle
import com.skyd.anivu.model.bean.OtherWorksBean
import com.skyd.anivu.ui.adapter.decoration.AniVuItemDecoration
import com.skyd.anivu.ui.adapter.variety.AniSpanSize
import com.skyd.anivu.ui.adapter.variety.VarietyAdapter
import com.skyd.anivu.ui.adapter.variety.proxy.OtherWorks1Proxy
Expand Down Expand Up @@ -96,6 +97,7 @@ class AboutFragment : BaseFragment<FragmentAboutBinding>() {
).apply {
spanSizeLookup = AniSpanSize(adapter)
}
rvAboutFragment.addItemDecoration(AniVuItemDecoration(hItemSpace = 20.dp))
rvAboutFragment.adapter = adapter
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.skyd.anivu.ext.addInsetsByPadding
import com.skyd.anivu.ext.collectIn
import com.skyd.anivu.ext.findMainNavController
import com.skyd.anivu.ext.gone
import com.skyd.anivu.ext.screenIsLand
import com.skyd.anivu.ext.showSnackbar
import com.skyd.anivu.ext.startWith
import com.skyd.anivu.ui.adapter.variety.AniSpanSize
Expand Down Expand Up @@ -199,10 +200,11 @@ class FeedFragment : BaseFragment<FragmentFeedBinding>() {
}

override fun FragmentFeedBinding.setWindowInsets() {
ablFeedFragment.addInsetsByPadding(top = true, left = true, right = true)
fabFeedFragment.addInsetsByMargin(left = true, right = true)
val isLand = requireContext().screenIsLand
ablFeedFragment.addInsetsByPadding(top = true, left = !isLand, right = true)
fabFeedFragment.addInsetsByMargin(left = !isLand, right = true)
rvFeedFragment.addInsetsByPadding(
left = true,
left = !isLand,
right = true,
hook = ::addFabBottomPaddingHook,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.skyd.anivu.ext.addInsetsByPadding
import com.skyd.anivu.ext.collectIn
import com.skyd.anivu.ext.findMainNavController
import com.skyd.anivu.ext.popBackStackWithLifecycle
import com.skyd.anivu.ext.screenIsLand
import com.skyd.anivu.ext.showSnackbar
import com.skyd.anivu.ext.toUri
import com.skyd.anivu.model.bean.ParentDirBean
Expand Down Expand Up @@ -176,10 +177,11 @@ class MediaFragment : BaseFragment<FragmentMediaBinding>() {
}

override fun FragmentMediaBinding.setWindowInsets() {
ablMediaFragment.addInsetsByPadding(top = true, left = true, right = true)
fabMediaFragment.addInsetsByMargin(left = true, right = true, bottom = hasParentDir)
val leftPadding = hasParentDir || !requireContext().screenIsLand
ablMediaFragment.addInsetsByPadding(top = true, left = leftPadding, right = true)
fabMediaFragment.addInsetsByMargin(left = leftPadding, right = true, bottom = hasParentDir)
rvMediaFragment.addInsetsByPadding(
left = true,
left = leftPadding,
right = true,
hook = ::addFabBottomPaddingHook,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.skyd.anivu.base.BaseFragment
import com.skyd.anivu.databinding.FragmentMoreBinding
import com.skyd.anivu.ext.addInsetsByPadding
import com.skyd.anivu.ext.findMainNavController
import com.skyd.anivu.ext.screenIsLand
import com.skyd.anivu.model.bean.MoreBean
import com.skyd.anivu.ui.adapter.decoration.AniVuItemDecoration
import com.skyd.anivu.ui.adapter.variety.AniSpanSize
Expand Down Expand Up @@ -43,8 +44,9 @@ class MoreFragment : BaseFragment<FragmentMoreBinding>() {
}

override fun FragmentMoreBinding.setWindowInsets() {
ablMoreFragment.addInsetsByPadding(top = true, left = true, right = true)
rvMoreFragment.addInsetsByPadding(left = true, right = true)
val isLand = requireContext().screenIsLand
ablMoreFragment.addInsetsByPadding(top = true, left = !isLand, right = true)
rvMoreFragment.addInsetsByPadding(left = !isLand, right = true)
}

private fun getMoreBeanList(): MutableList<MoreBean> {
Expand Down
Loading

0 comments on commit f14ce28

Please sign in to comment.