Skip to content

Commit

Permalink
#32 : RecyclerView 스크롤 버그 수정, NestedSrollView 적용, FloatingButton 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
seunggyu97 committed Mar 12, 2023
1 parent 53e8ded commit aed6911
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 202 deletions.
55 changes: 36 additions & 19 deletions app/src/main/java/com/seunggyu/stitch/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.seunggyu.stitch

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -14,6 +16,7 @@ import androidx.databinding.DataBindingUtil
import androidx.navigation.Navigation
import com.seunggyu.stitch.Util.SnackBarCustom
import com.seunggyu.stitch.databinding.ActivityMainBinding
import com.seunggyu.stitch.ui.CreateNewMatch
import com.seunggyu.stitch.ui.fragment.category.CategoryFragment
import com.seunggyu.stitch.ui.fragment.home.HomeFragment
import com.seunggyu.stitch.ui.fragment.mymatch.MymatchFragment
Expand All @@ -31,51 +34,40 @@ class MainActivity : BasicActivity() {
}

fun init() {
val originDecorView = window.decorView.systemUiVisibility
with(binding) {
navigationView.run {
setOnItemSelectedListener {
when (it.itemId) {
R.id.action_home -> {
window.apply {
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
statusBarColor = Color.TRANSPARENT
}
makeStatusBarTransParent()

supportFragmentManager
.beginTransaction()
.replace(binding.frameLayout.id, HomeFragment())
.commitAllowingStateLoss()
Log.e("NaviFrag", "actionHome")
}
R.id.action_category -> {
window.apply {
decorView.systemUiVisibility = originDecorView
clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
}
clearStatusBarTransParent()

supportFragmentManager
.beginTransaction()
.replace(binding.frameLayout.id, CategoryFragment())
.commitAllowingStateLoss()
Log.e("NaviFrag", "actionCategory")
}
R.id.action_mymatch -> {
window.apply {
decorView.systemUiVisibility = originDecorView
clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
}
clearStatusBarTransParent()

supportFragmentManager
.beginTransaction()
.replace(binding.frameLayout.id, MymatchFragment())
.commitAllowingStateLoss()
Log.e("NaviFrag", "actionMymatch")
}
R.id.action_mymenu -> {
window.apply {
decorView.systemUiVisibility = originDecorView
clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
}
clearStatusBarTransParent()

supportFragmentManager
.beginTransaction()
.replace(binding.frameLayout.id, MymenuFragment())
Expand All @@ -87,6 +79,31 @@ class MainActivity : BasicActivity() {
}
selectedItemId = R.id.action_home
}

floatingButton.setOnClickListener {
startActivity(Intent(this@MainActivity, CreateNewMatch::class.java))
}
}
}

fun Activity.makeStatusBarTransParent() {
window.apply {
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
statusBarColor = Color.TRANSPARENT
}
}

fun Activity.clearStatusBarTransParent() {
val originDecorView = window.decorView.systemUiVisibility

window.apply {
decorView.systemUiVisibility = originDecorView
clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

statusBarColor = ContextCompat.getColor(context, R.color.gray_14)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
Expand All @@ -20,6 +22,7 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.seunggyu.stitch.BasicActivity
import com.seunggyu.stitch.GlobalApplication
import com.seunggyu.stitch.R
import com.seunggyu.stitch.Util.SnackBarCustom
Expand All @@ -29,6 +32,7 @@ import com.seunggyu.stitch.data.model.response.NetworkResponse
import com.seunggyu.stitch.databinding.FragMainHomeBinding
import com.seunggyu.stitch.viewModel.MainViewModel
import java.util.*
import kotlin.math.abs

class HomeFragment : Fragment() {
private lateinit var binding: FragMainHomeBinding
Expand Down Expand Up @@ -56,10 +60,13 @@ class HomeFragment : Fragment() {
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.frag_main_home, container, false)

initClickListener()
initInsetMargin()
initData()
initRecyclerView()
initBannerViewPager()
initAppBar()
initObserver()

return binding.root
Expand All @@ -69,14 +76,15 @@ class HomeFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)

val toolbar = binding.toolbar
(activity as AppCompatActivity).setSupportActionBar(toolbar)
(activity as BasicActivity).setSupportActionBar(toolbar)
}
private fun initRecyclerView() {

binding.rvMainContentsRecommend.apply {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
addItemDecoration(SpaceItemDecoration(16))
adapter = recommendRecyclerViewAdapter
isNestedScrollingEnabled = false
}

binding.rvMainContentsNewmatch.apply {
Expand Down Expand Up @@ -196,35 +204,51 @@ class HomeFragment : Fragment() {

private fun initAppBar() {

binding.nsvMain.setOnScrollChangeListener { _, _, scrollY, _, _ ->
// 스크롤 뷰의 스크롤 위치를 기반으로 alpha 값을 계산
val alpha = if (scrollY > 0) {
val maxAlphaOffset = binding.toolbar.height
(scrollY / maxAlphaOffset.toFloat()).coerceAtMost(1.0f)
} else {
0.0f
binding.appBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
val topPadding = binding.toolbarBackgroundView.height.toFloat() * 3
val realAlphaScrollHeight = appBarLayout.measuredHeight - appBarLayout.totalScrollRange
val abstractOffset = abs(verticalOffset)

val realAlphaVerticalOffset =
if (abstractOffset - topPadding < 0) 0f else abstractOffset - topPadding
if (abstractOffset < topPadding) {
binding.toolbarBackgroundView.alpha = 0f
return@OnOffsetChangedListener
}
val percentage = realAlphaVerticalOffset / realAlphaScrollHeight
binding.toolbarBackgroundView.alpha =
1 - (if (1 - percentage * 2 < 0) 0f else 1 - percentage * 2)

})
initActionBar()
}

private fun initActionBar() = with(binding) {
toolbar.navigationIcon = null
toolbar.setContentInsetsAbsolute(0, 0)
(activity as BasicActivity).setSupportActionBar(toolbar)
(activity as BasicActivity).supportActionBar?.let {
it.setHomeButtonEnabled(false)
it.setDisplayHomeAsUpEnabled(false)
it.setDisplayShowHomeEnabled(false)
}
}

private fun initInsetMargin() = with(binding) {
ViewCompat.setOnApplyWindowInsetsListener(coordinator) { v: View, insets: WindowInsetsCompat ->
val params = v.layoutParams as ViewGroup.MarginLayoutParams
params.bottomMargin = insets.systemWindowInsetBottom
toolbarContainer.layoutParams =
(toolbarContainer.layoutParams as ViewGroup.MarginLayoutParams).apply {
setMargins(16.dpToPx(), insets.systemWindowInsetTop, 16.dpToPx(), 0)
}
collapsingToolbarContainer.layoutParams =
(collapsingToolbarContainer.layoutParams as ViewGroup.MarginLayoutParams).apply {
setMargins(0, 0, 0, 0)
}

// 계산된 alpha 값으로 툴바의 알파값 설정
binding.toolbar.alpha = alpha
insets.consumeSystemWindowInsets()
}
// binding.nsvMain.setOnScrollChangeListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
// val topPadding = 300f.dpToPx(this)
// val realAlphaScrollHeight = appBarLayout.measuredHeight - appBarLayout.totalScrollRange
// val abstractOffset = Math.abs(verticalOffset)
//
// val realAlphaVerticalOffset =
// if (abstractOffset - topPadding < 0) 0f else abstractOffset - topPadding
//
// if (abstractOffset < topPadding) {
// binding.toolbarBackgroundView.alpha = 0f
// return@OnOffsetChangedListener
// }
// val percentage = realAlphaVerticalOffset / realAlphaScrollHeight
// binding.toolbarBackgroundView.alpha =
// 1 - (if (1 - percentage * 2 < 0) 0f else 1 - percentage * 2)
// })
// initActionBar()
}

private fun timerStart() {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:id="@+id/cl_mainview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.Main"
tools:context=".MainActivity">

<FrameLayout
Expand All @@ -29,6 +30,17 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/navigation_item" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating_button"
android:layout_width="56dp"
android:layout_height="56dp"
app:layout_constraintBottom_toTopOf="@id/navigation_view"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="24dp"
android:backgroundTint="@color/primary"
android:src="@drawable/ic_plus"
android:layout_marginEnd="16dp"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
Loading

0 comments on commit aed6911

Please sign in to comment.