728x90

MainActivity iniRecyclerView() 

binding.recyclerView.apply {

            adapter = productAdapter
            layoutManager = LinearLayoutManager(baseContext, LinearLayoutManager.VERTICAL, false)
            val divider =
                DividerItemDecoration(baseContext, LinearLayoutManager.VERTICAL)//divider 추가
            addItemDecoration(divider)

            //애니매이션 효과
            val fadeIn = AlphaAnimation(0f, 1f).apply { duration = 500 }
            val fadeOut = AlphaAnimation(1f, 0f).apply { duration = 500 }
            var isTop = true

            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)

                    /*
                    canScrollVertically(int direction) 메서드는 RecyclerView가 주어진 방향으로 스크롤할 수 있는지 여부를 나타냄. direction -> -1 상단, 1 하단
                    canScrollVertically(1)은 하단 스크롤할 수 있는지를 확인, canScrollVertically(-1) 상단 스크롤할 수 있는지를 확인
                    !recyclerView.canScrollVertically(-1)는 상단으로 더 이상 스크롤할 수 없다는 것, newState == RecyclerView.SCROLL_STATE_IDLE는 현재 스크롤 상태가 대기 상태인지를 확인.
                    */

                    if (!recyclerView.canScrollVertically(-1) && newState == RecyclerView.SCROLL_STATE_IDLE) {
                        binding.apply {
                            fbScrollButton.startAnimation(fadeOut)
                            fbScrollButton.isVisible = false
                            isTop = true
                        }
                    } else {
                        if (isTop) {
                            binding.apply {
                                fbScrollButton.isVisible = true
                                fbScrollButton.startAnimation(fadeIn)
                                isTop = false
                            }
                        }
                    }

                }
            })

        }

 

 

addOnScrollListener()를 이용해서 스크롤을 감지 

 

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="36dp"
        android:text="@string/region"
        android:textColor="@color/black"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginStart="5dp"
        android:src="@drawable/bottom_arrow"
        app:layout_constraintBottom_toBottomOf="@id/tv_Text"
        app:layout_constraintStart_toEndOf="@id/tv_Text"
        app:layout_constraintTop_toTopOf="@id/tv_Text" />

    <ImageView
        android:id="@+id/iv_notification"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="36dp"
        android:layout_marginEnd="18dp"
        android:src="@drawable/notification"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_Text" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fb_ScrollButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="36dp"
        android:backgroundTint="@color/white"
        android:clickable="true"
        android:elevation="0dp"
        android:hapticFeedbackEnabled="true"
        android:src="@drawable/top_arrow"
        android:tint="@null"
        android:visibility="invisible"
        app:borderWidth="0dp"
        app:fabCustomSize="40dp"
        app:fabSize="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:maxImageSize="40dp"
        app:rippleColor="#2962FF" />

</androidx.constraintlayout.widget.ConstraintLayout>
        
        
        ///xml///

 

구현화면

+ Recent posts