viewPager指示器滑動效果



override fun onPageSelected(pos: Int) {
        currentPagerItem = pos
        var selectedColor = ContextCompat.getColor(baseActivity, R.color.home_pager_text_selected)
        var unselectedColor =
            ContextCompat.getColor(baseActivity, R.color.home_pager_text_unselected)
        when (pos) {
            HomeFragmentPagerAdapter.HOME_FRAGMENT_PARTY -> {
                home_fragment_btn_party.setTextColor(selectedColor)
                home_fragment_btn_party_indicator_layout.isSelected = true
                home_fragment_btn_games.setTextColor(unselectedColor)
                home_fragment_btn_friends_indicator_layout.isSelected = false
                home_fragment_btn_friends.setTextColor(unselectedColor)
                home_fragment_btn_games_indicator_layout.isSelected = false
                home_fragment_btn_create_room.visibility = View.VISIBLE
                if (hasInvite) {
                    home_invite_v.visibility = View.VISIBLE
                }
                home_fragment_btn_message.visibility = View.INVISIBLE
                Bus.post(HomePartyTabClickEvent()) // 首頁tab切換的廣播
            }
            HomeFragmentPagerAdapter.HOME_FRAGMENT_GAMES -> {
                home_fragment_btn_party.setTextColor(unselectedColor)
                home_fragment_btn_party_indicator_layout.isSelected = false
                home_fragment_btn_games.setTextColor(selectedColor)
                home_fragment_btn_games_indicator_layout.isSelected = true
                home_fragment_btn_friends.setTextColor(unselectedColor)
                home_fragment_btn_friends_indicator_layout.isSelected = false
                home_fragment_btn_create_room.visibility = View.INVISIBLE
                home_invite_v.visibility = View.INVISIBLE
                home_fragment_btn_message.visibility = View.INVISIBLE
                Bus.post(HomeGamesTabClickEvent()) // 首頁tab切換的廣播
            }
            HomeFragmentPagerAdapter.HOME_FRAGMENT_FRIENDS -> {
                home_fragment_btn_party.setTextColor(unselectedColor)
                home_fragment_btn_party_indicator_layout.isSelected = false
                home_fragment_btn_games.setTextColor(unselectedColor)
                home_fragment_btn_games_indicator_layout.isSelected = false
                home_fragment_btn_friends.setTextColor(selectedColor)
                home_fragment_btn_friends_indicator_layout.isSelected = true
                home_fragment_btn_create_room.visibility = View.INVISIBLE
                home_invite_v.visibility = View.INVISIBLE
                home_fragment_btn_message.visibility = View.VISIBLE
                Bus.post(HomeFriendTabClickEvent()) // 首頁tab切換的廣播
            }
        }
    }

 

override fun onPageScrolled(pos: Int, posOffset: Float, posOffsetPixels: Int) {
        // Timber.tag("zivon").d("currentItem = ${homeViewPager.currentItem}, pos = $pos, posOffset = $posOffset, posOffsetPixels = $posOffsetPixels")
        when (homeViewPager.currentItem) {
            HomeFragmentPagerAdapter.HOME_FRAGMENT_PARTY -> {
                if (pos == homeViewPager.currentItem) {
                    home_fragment_btn_party_indicator_layout.translationX =
                        posOffset * home_fragment_btn_party_indicator.width
                    home_fragment_btn_party_indicator_layout.alpha = 1f - posOffset
                } else {
                    home_fragment_btn_party_indicator_layout.alpha = posOffset
                    home_fragment_btn_party_indicator_layout.translationX =
                        -((1f - posOffset) * home_fragment_btn_friends_indicator.width)
                }
            }
            HomeFragmentPagerAdapter.HOME_FRAGMENT_GAMES -> {
                if (pos == homeViewPager.currentItem) {
                    home_fragment_btn_games_indicator_layout.translationX =
                        posOffset * home_fragment_btn_games_indicator.width
                    home_fragment_btn_games_indicator_layout.alpha = 1f - posOffset
                } else {
                    home_fragment_btn_games_indicator_layout.alpha = posOffset
                    home_fragment_btn_games_indicator_layout.translationX =
                        -((1f - posOffset) * home_fragment_btn_friends_indicator.width)
                }
            }
            else -> {
                if (pos == homeViewPager.currentItem) {
                    home_fragment_btn_friends_indicator_layout.translationX =
                        posOffset * home_fragment_btn_games_indicator.width
                    home_fragment_btn_friends_indicator_layout.alpha = 1f - posOffset
                } else {
                    home_fragment_btn_friends_indicator_layout.alpha = posOffset
                    home_fragment_btn_friends_indicator_layout.translationX =
                        -((1f - posOffset) * home_fragment_btn_friends_indicator.width)
                }
            }
        }
    }
<color name="home_pager_text_unselected">@android:color/white</color>
    <color name="home_pager_text_selected">#FF00FF7B</color>
<?xml version="1.0" encoding="utf-8"?>
<!-- 首頁viewPager的按鈕 -->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/home_pager_indicator_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/home_pager_first_tab_start_margin"
    android:layout_marginEnd="@dimen/home_pager_first_tab_start_margin">

    <TextView
        android:id="@+id/home_fragment_btn_party"
        style="@style/home_fragment_btn_text_style"
        android:text="@string/home_fragment_party_title"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/home_fragment_btn_games"
        style="@style/home_fragment_btn_text_style"
        android:text="@string/home_fragment_games_title"
        android:layout_marginStart="@dimen/home_fragment_pager_text_space"
        app:layout_constraintStart_toEndOf="@id/home_fragment_btn_party"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/home_fragment_btn_friends"
        style="@style/home_fragment_btn_text_style"
        android:text="@string/home_fragment_friends_title"
        android:layout_marginStart="@dimen/home_fragment_pager_text_space"
        app:layout_constraintStart_toEndOf="@id/home_fragment_btn_games"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <!-- 消息小紅點 -->
    <ImageView
        android:id="@+id/chatMessageUnreadRedDot"
        android:layout_width="@dimen/home_pager_message_no_read_size"
        android:layout_height="@dimen/home_pager_message_no_read_size"
        android:background="@drawable/shape_message_notify"
        android:visibility="gone"
        app:layout_constraintTop_toTopOf="@id/home_fragment_btn_friends"
        app:layout_constraintStart_toEndOf="@id/home_fragment_btn_friends"/>

    <ImageView
        android:id="@+id/home_invite_v"
        android:layout_marginRight="9.3dp"
        android:src="@drawable/invite_box"
        android:layout_height="@dimen/create_chat_room_btn_height"
        android:layout_width="@dimen/create_chat_room_btn_height"
        android:visibility="invisible"
        app:layout_constraintEnd_toStartOf="@+id/home_fragment_btn_create_room"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <ImageView
        android:id="@+id/home_fragment_btn_create_room"
        android:layout_marginEnd="@dimen/create_chat_room_btn_extra_end_space"
        android:src="@drawable/ic_create_room_btn"
        android:layout_height="@dimen/create_chat_room_btn_height"
        android:layout_width="@dimen/home_create_chat_room_btn_width"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <ImageView
        android:id="@+id/home_fragment_btn_message"
        android:layout_marginEnd="@dimen/create_chat_room_btn_extra_end_space"
        android:src="@drawable/ic_message"
        android:layout_height="@dimen/home_fragment_message_btn_width"
        android:layout_width="@dimen/home_fragment_message_btn_width"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.cardview.widget.CardView
        android:id="@+id/home_fragment_btn_party_indicator_layout"
        style="@style/home_fragment_btn_indicator_parent_style"
        app:layout_constraintTop_toBottomOf="@id/home_fragment_btn_party"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@id/home_fragment_btn_party"
        app:layout_constraintEnd_toEndOf="@id/home_fragment_btn_party">

        <View
            android:id="@+id/home_fragment_btn_party_indicator"
            style="@style/home_fragment_btn_indicator_style"/>

    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/home_fragment_btn_games_indicator_layout"
        style="@style/home_fragment_btn_indicator_parent_style"
        app:layout_constraintTop_toBottomOf="@id/home_fragment_btn_games"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@id/home_fragment_btn_games"
        app:layout_constraintEnd_toEndOf="@id/home_fragment_btn_games">

        <View
            android:id="@+id/home_fragment_btn_games_indicator"
            style="@style/home_fragment_btn_indicator_style"/>

    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/home_fragment_btn_friends_indicator_layout"
        style="@style/home_fragment_btn_indicator_parent_style"
        app:layout_constraintTop_toBottomOf="@id/home_fragment_btn_friends"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@id/home_fragment_btn_friends"
        app:layout_constraintEnd_toEndOf="@id/home_fragment_btn_friends">

        <View
            android:id="@+id/home_fragment_btn_friends_indicator"
            style="@style/home_fragment_btn_indicator_style"/>

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章