From aed691156be699481de539fb7d2a52efbe621cce Mon Sep 17 00:00:00 2001 From: seunggyu Date: Sun, 12 Mar 2023 22:34:42 +0900 Subject: [PATCH] =?UTF-8?q?#32=20:=20RecyclerView=20=EC=8A=A4=ED=81=AC?= =?UTF-8?q?=EB=A1=A4=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95,=20NestedSr?= =?UTF-8?q?ollView=20=EC=A0=81=EC=9A=A9,=20FloatingButton=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/seunggyu/stitch/MainActivity.kt | 55 ++- .../stitch/ui/fragment/home/HomeFragment.kt | 78 ++-- app/src/main/res/layout/activity_main.xml | 12 + app/src/main/res/layout/frag_main_home.xml | 363 ++++++++++-------- 4 files changed, 306 insertions(+), 202 deletions(-) diff --git a/app/src/main/java/com/seunggyu/stitch/MainActivity.kt b/app/src/main/java/com/seunggyu/stitch/MainActivity.kt index 125ef36..1f09919 100644 --- a/app/src/main/java/com/seunggyu/stitch/MainActivity.kt +++ b/app/src/main/java/com/seunggyu/stitch/MainActivity.kt @@ -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 @@ -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 @@ -31,18 +34,13 @@ 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()) @@ -50,10 +48,8 @@ class MainActivity : BasicActivity() { 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()) @@ -61,10 +57,8 @@ class MainActivity : BasicActivity() { 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()) @@ -72,10 +66,8 @@ class MainActivity : BasicActivity() { 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()) @@ -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) + } + } + } \ No newline at end of file diff --git a/app/src/main/java/com/seunggyu/stitch/ui/fragment/home/HomeFragment.kt b/app/src/main/java/com/seunggyu/stitch/ui/fragment/home/HomeFragment.kt index f50ae47..f92ce56 100644 --- a/app/src/main/java/com/seunggyu/stitch/ui/fragment/home/HomeFragment.kt +++ b/app/src/main/java/com/seunggyu/stitch/ui/fragment/home/HomeFragment.kt @@ -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 @@ -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 @@ -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 @@ -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 @@ -69,7 +76,7 @@ class HomeFragment : Fragment() { super.onViewCreated(view, savedInstanceState) val toolbar = binding.toolbar - (activity as AppCompatActivity).setSupportActionBar(toolbar) + (activity as BasicActivity).setSupportActionBar(toolbar) } private fun initRecyclerView() { @@ -77,6 +84,7 @@ class HomeFragment : Fragment() { layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) addItemDecoration(SpaceItemDecoration(16)) adapter = recommendRecyclerViewAdapter + isNestedScrollingEnabled = false } binding.rvMainContentsNewmatch.apply { @@ -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() { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 788278a..166561f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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"> + + - - - + - - + android:layout_height="wrap_content" + android:background="@android:color/transparent" + android:fitsSystemWindows="false"> - - + app:contentScrim="@android:color/transparent" + app:expandedTitleGravity="top" + app:layout_scrollFlags="scroll|exitUntilCollapsed" + app:statusBarScrim="@android:color/transparent"> + android:background="@color/gray_14" + android:fitsSystemWindows="false" + app:layout_collapseMode="pin"> - + + - - - - - + app:layout_constraintTop_toBottomOf="@id/vp_banner" /> + + + + + + + + + + + + - - + + + + + + + + + - - + + + app:layout_constraintTop_toTopOf="parent"> - + - + - - + - + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> - + - + - - + - + + + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/home_view_divider"> + + + + + + - + + + -