Skip to content

Commit f14ce28

Browse files
committed
[optimize] Optimize land layout
1 parent a575afb commit f14ce28

File tree

12 files changed

+283
-57
lines changed

12 files changed

+283
-57
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ android {
2121
minSdk = 24
2222
targetSdk = 34
2323
versionCode = 10
24-
versionName = "1.1-beta03"
24+
versionName = "1.1-beta04"
2525

2626
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2727

app/src/main/java/com/skyd/anivu/ui/adapter/decoration/AniVuItemDecoration.kt

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import com.skyd.anivu.ui.adapter.variety.VarietyAdapter
1010
import kotlin.math.roundToInt
1111

1212

13-
class AniVuItemDecoration : RecyclerView.ItemDecoration() {
13+
class AniVuItemDecoration(
14+
private val hItemSpace: Int = H_ITEM_SPACE,
15+
private val horizontalSpace: Int = HORIZONTAL_PADDING,
16+
) : RecyclerView.ItemDecoration() {
1417
override fun getItemOffsets(
1518
outRect: Rect,
1619
view: View,
@@ -27,67 +30,63 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
2730
// 注意这里使用getChildLayoutPosition的目的
2831
// 如果使用getChildAdapterPosition,刷新的时候可能会(边框)闪动一下,(返回-1)
2932
?.getOrNull(parent.getChildLayoutPosition(view))
30-
if (needVerticalMargin(item?.javaClass)) {
31-
outRect.top = 10.dp
32-
outRect.bottom = 2.dp
33-
}
3433
if (spanSize == MAX_SPAN_SIZE) {
3534
/**
3635
* 只有一列
3736
*/
3837
if (noHorizontalMargin(item?.javaClass)) return
39-
outRect.left = HORIZONTAL_PADDING
40-
outRect.right = HORIZONTAL_PADDING
38+
outRect.left = horizontalSpace
39+
outRect.right = horizontalSpace
4140
} else if (spanSize == MAX_SPAN_SIZE / 2) {
4241
/**
4342
* 只有两列,没有在中间的item
44-
* 2x = ITEM_SPACING
43+
* 2x = hItemSpace
4544
*/
46-
val x: Int = (ITEM_SPACING / 2f).roundToInt()
45+
val x: Int = (hItemSpace / 2f).roundToInt()
4746
if (spanIndex == 0) {
48-
outRect.left = HORIZONTAL_PADDING
47+
outRect.left = horizontalSpace
4948
outRect.right = x
5049
} else {
5150
outRect.left = x
52-
outRect.right = HORIZONTAL_PADDING
51+
outRect.right = horizontalSpace
5352
}
5453
} else if (spanSize == MAX_SPAN_SIZE / 3) {
5554
/**
5655
* 只有三列,一个在中间的item
57-
* HORIZONTAL_PADDING + x = 2y
58-
* x + y = ITEM_SPACING
56+
* horizontalSpace + x = 2y
57+
* x + y = hItemSpace
5958
*/
60-
val y: Int = ((HORIZONTAL_PADDING + ITEM_SPACING) / 3f).roundToInt()
61-
val x: Int = ITEM_SPACING - y
59+
val y: Int = ((horizontalSpace + hItemSpace) / 3f).roundToInt()
60+
val x: Int = hItemSpace - y
6261
if (spanIndex == 0) {
63-
outRect.left = HORIZONTAL_PADDING
62+
outRect.left = horizontalSpace
6463
outRect.right = x
6564
} else if (spanIndex + spanSize == MAX_SPAN_SIZE) {
6665
// 最右侧最后一个
6766
outRect.left = x
68-
outRect.right = HORIZONTAL_PADDING
67+
outRect.right = horizontalSpace
6968
} else {
7069
outRect.left = y
7170
outRect.right = y
7271
}
7372
} else if (spanSize == MAX_SPAN_SIZE / 5) {
7473
/**
7574
* 只有五列
76-
* HORIZONTAL_PADDING + x = y + z
77-
* x + y = ITEM_SPACING
78-
* z + (HORIZONTAL_PADDING + x) / 2 = ITEM_SPACING
75+
* horizontalSpace + x = y + z
76+
* x + y = hItemSpace
77+
* z + (horizontalSpace + x) / 2 = hItemSpace
7978
*/
80-
val x: Int = ((4 * ITEM_SPACING - 3 * HORIZONTAL_PADDING) / 5f).roundToInt()
81-
val y: Int = ITEM_SPACING - x
82-
val z: Int = HORIZONTAL_PADDING + x - y
79+
val x: Int = ((4 * hItemSpace - 3 * horizontalSpace) / 5f).roundToInt()
80+
val y: Int = hItemSpace - x
81+
val z: Int = horizontalSpace + x - y
8382
if (spanIndex == 0) {
8483
// 最左侧第一个
85-
outRect.left = HORIZONTAL_PADDING
84+
outRect.left = horizontalSpace
8685
outRect.right = x
8786
} else if (spanIndex + spanSize == MAX_SPAN_SIZE) {
8887
// 最右侧最后一个
8988
outRect.left = x
90-
outRect.right = HORIZONTAL_PADDING
89+
outRect.right = horizontalSpace
9190
} else if (spanIndex == spanSize) {
9291
// 第二个
9392
outRect.left = y
@@ -98,8 +97,8 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
9897
outRect.right = y
9998
} else {
10099
// 最中间的
101-
outRect.left = ((HORIZONTAL_PADDING + x) / 2f).roundToInt()
102-
outRect.right = ((HORIZONTAL_PADDING + x) / 2f).roundToInt()
100+
outRect.left = ((horizontalSpace + x) / 2f).roundToInt()
101+
outRect.right = ((horizontalSpace + x) / 2f).roundToInt()
103102
}
104103
} else {
105104
/**
@@ -108,28 +107,28 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
108107
if ((MAX_SPAN_SIZE / spanSize) % 2 == 0) {
109108
/**
110109
* 偶数个item
111-
* HORIZONTAL_PADDING + x = y + ITEM_SPACING / 2
112-
* x + y = ITEM_SPACING
110+
* horizontalSpace + x = y + hItemSpace / 2
111+
* x + y = hItemSpace
113112
*/
114-
val y: Int = ((HORIZONTAL_PADDING + ITEM_SPACING / 2f) / 2f).roundToInt()
115-
val x: Int = ITEM_SPACING - y
113+
val y: Int = ((horizontalSpace + hItemSpace / 2f) / 2f).roundToInt()
114+
val x: Int = hItemSpace - y
116115
if (spanIndex == 0) {
117116
// 最左侧第一个
118-
outRect.left = HORIZONTAL_PADDING
117+
outRect.left = horizontalSpace
119118
outRect.right = x
120119
} else if (spanIndex + spanSize == MAX_SPAN_SIZE) {
121120
// 最右侧最后一个
122121
outRect.left = x
123-
outRect.right = HORIZONTAL_PADDING
122+
outRect.right = horizontalSpace
124123
} else {
125124
// 中间的项目
126125
if (spanIndex < MAX_SPAN_SIZE / 2) {
127126
// 左侧部分
128127
outRect.left = y
129-
outRect.right = ITEM_SPACING / 2
128+
outRect.right = hItemSpace / 2
130129
} else {
131130
// 右侧部分
132-
outRect.left = ITEM_SPACING / 2
131+
outRect.left = hItemSpace / 2
133132
outRect.right = y
134133
}
135134
}
@@ -142,8 +141,9 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
142141
}
143142

144143
companion object {
145-
val ITEM_SPACING: Int = 12.dp
144+
val H_ITEM_SPACE: Int = 12.dp
146145
val HORIZONTAL_PADDING: Int = 16.dp
146+
val VERTICAL_PADDING: Int = 16.dp
147147

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

@@ -153,14 +153,5 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
153153
clz ?: return true
154154
return clz in noHorizontalMarginType
155155
}
156-
157-
private val needVerticalMarginType: Set<Class<*>> = setOf(
158-
159-
)
160-
161-
fun needVerticalMargin(clz: Class<*>?): Boolean {
162-
clz ?: return false
163-
return clz in needVerticalMarginType
164-
}
165156
}
166157
}

app/src/main/java/com/skyd/anivu/ui/adapter/variety/AniSpanSize.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.skyd.anivu.appContext
55
import com.skyd.anivu.ext.screenIsLand
66
import com.skyd.anivu.model.bean.FeedBean
77
import com.skyd.anivu.model.bean.MoreBean
8+
import com.skyd.anivu.model.bean.OtherWorksBean
89

910
class AniSpanSize(
1011
private val adapter: VarietyAdapter,
@@ -18,6 +19,7 @@ class AniSpanSize(
1819
when (data) {
1920
is FeedBean -> MAX_SPAN_SIZE
2021
is MoreBean -> MAX_SPAN_SIZE / 3
22+
is OtherWorksBean -> MAX_SPAN_SIZE / 2
2123
else -> MAX_SPAN_SIZE
2224
}
2325
} else {

app/src/main/java/com/skyd/anivu/ui/fragment/MainFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.view.View
66
import android.view.ViewGroup
77
import androidx.navigation.fragment.NavHostFragment
88
import androidx.navigation.ui.setupWithNavController
9+
import com.google.android.material.navigation.NavigationBarView
910
import com.skyd.anivu.base.BaseFragment
1011
import com.skyd.anivu.databinding.FragmentMainBinding
1112
import dagger.hilt.android.AndroidEntryPoint
@@ -21,6 +22,6 @@ class MainFragment : BaseFragment<FragmentMainBinding>() {
2122
val navHostFragment = binding.navHostFragment.getFragment<NavHostFragment>()
2223
val navController = navHostFragment.navController
2324

24-
binding.bottomNavigation.setupWithNavController(navController)
25+
(binding.bottomNavigation as NavigationBarView).setupWithNavController(navController)
2526
}
2627
}

app/src/main/java/com/skyd/anivu/ui/fragment/about/AboutFragment.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.skyd.anivu.ext.getAppVersionName
1818
import com.skyd.anivu.ext.openBrowser
1919
import com.skyd.anivu.ext.popBackStackWithLifecycle
2020
import com.skyd.anivu.model.bean.OtherWorksBean
21+
import com.skyd.anivu.ui.adapter.decoration.AniVuItemDecoration
2122
import com.skyd.anivu.ui.adapter.variety.AniSpanSize
2223
import com.skyd.anivu.ui.adapter.variety.VarietyAdapter
2324
import com.skyd.anivu.ui.adapter.variety.proxy.OtherWorks1Proxy
@@ -96,6 +97,7 @@ class AboutFragment : BaseFragment<FragmentAboutBinding>() {
9697
).apply {
9798
spanSizeLookup = AniSpanSize(adapter)
9899
}
100+
rvAboutFragment.addItemDecoration(AniVuItemDecoration(hItemSpace = 20.dp))
99101
rvAboutFragment.adapter = adapter
100102
}
101103

app/src/main/java/com/skyd/anivu/ui/fragment/feed/FeedFragment.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.skyd.anivu.ext.addInsetsByPadding
2222
import com.skyd.anivu.ext.collectIn
2323
import com.skyd.anivu.ext.findMainNavController
2424
import com.skyd.anivu.ext.gone
25+
import com.skyd.anivu.ext.screenIsLand
2526
import com.skyd.anivu.ext.showSnackbar
2627
import com.skyd.anivu.ext.startWith
2728
import com.skyd.anivu.ui.adapter.variety.AniSpanSize
@@ -199,10 +200,11 @@ class FeedFragment : BaseFragment<FragmentFeedBinding>() {
199200
}
200201

201202
override fun FragmentFeedBinding.setWindowInsets() {
202-
ablFeedFragment.addInsetsByPadding(top = true, left = true, right = true)
203-
fabFeedFragment.addInsetsByMargin(left = true, right = true)
203+
val isLand = requireContext().screenIsLand
204+
ablFeedFragment.addInsetsByPadding(top = true, left = !isLand, right = true)
205+
fabFeedFragment.addInsetsByMargin(left = !isLand, right = true)
204206
rvFeedFragment.addInsetsByPadding(
205-
left = true,
207+
left = !isLand,
206208
right = true,
207209
hook = ::addFabBottomPaddingHook,
208210
)

app/src/main/java/com/skyd/anivu/ui/fragment/media/MediaFragment.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.skyd.anivu.ext.addInsetsByPadding
2323
import com.skyd.anivu.ext.collectIn
2424
import com.skyd.anivu.ext.findMainNavController
2525
import com.skyd.anivu.ext.popBackStackWithLifecycle
26+
import com.skyd.anivu.ext.screenIsLand
2627
import com.skyd.anivu.ext.showSnackbar
2728
import com.skyd.anivu.ext.toUri
2829
import com.skyd.anivu.model.bean.ParentDirBean
@@ -176,10 +177,11 @@ class MediaFragment : BaseFragment<FragmentMediaBinding>() {
176177
}
177178

178179
override fun FragmentMediaBinding.setWindowInsets() {
179-
ablMediaFragment.addInsetsByPadding(top = true, left = true, right = true)
180-
fabMediaFragment.addInsetsByMargin(left = true, right = true, bottom = hasParentDir)
180+
val leftPadding = hasParentDir || !requireContext().screenIsLand
181+
ablMediaFragment.addInsetsByPadding(top = true, left = leftPadding, right = true)
182+
fabMediaFragment.addInsetsByMargin(left = leftPadding, right = true, bottom = hasParentDir)
181183
rvMediaFragment.addInsetsByPadding(
182-
left = true,
184+
left = leftPadding,
183185
right = true,
184186
hook = ::addFabBottomPaddingHook,
185187
)

app/src/main/java/com/skyd/anivu/ui/fragment/more/MoreFragment.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.skyd.anivu.base.BaseFragment
1010
import com.skyd.anivu.databinding.FragmentMoreBinding
1111
import com.skyd.anivu.ext.addInsetsByPadding
1212
import com.skyd.anivu.ext.findMainNavController
13+
import com.skyd.anivu.ext.screenIsLand
1314
import com.skyd.anivu.model.bean.MoreBean
1415
import com.skyd.anivu.ui.adapter.decoration.AniVuItemDecoration
1516
import com.skyd.anivu.ui.adapter.variety.AniSpanSize
@@ -43,8 +44,9 @@ class MoreFragment : BaseFragment<FragmentMoreBinding>() {
4344
}
4445

4546
override fun FragmentMoreBinding.setWindowInsets() {
46-
ablMoreFragment.addInsetsByPadding(top = true, left = true, right = true)
47-
rvMoreFragment.addInsetsByPadding(left = true, right = true)
47+
val isLand = requireContext().screenIsLand
48+
ablMoreFragment.addInsetsByPadding(top = true, left = !isLand, right = true)
49+
rvMoreFragment.addInsetsByPadding(left = !isLand, right = true)
4850
}
4951

5052
private fun getMoreBeanList(): MutableList<MoreBean> {

0 commit comments

Comments
 (0)