728x90
화면이 스크롤 되어도 따라다닐 수 있게 AppBarlayout 으로 감싸준 다음 ViewPager와 탭을 연결
activity_detail.xml
<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
tools:context=".ui.detail_activity.DetailActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/detail_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_color"
app:tabIndicatorColor="@color/primary_color"
app:tabRippleColor="@null"
app:tabTextColor="@color/white">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/detail_ViewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/detail_appbar" />
</androidx.constraintlayout.widget.ConstraintLayout>
DetailActivity
package com.nbc.shownect.ui.detail_activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.tabs.TabLayoutMediator
import com.nbc.shownect.databinding.ActivityDetailBinding
import com.nbc.shownect.ui.detail_activity.adapter.DetailPagerAdapter
import com.nbc.shownect.util.Constants
class DetailActivity : AppCompatActivity() {
private lateinit var binding: ActivityDetailBinding
private val detailViewModel by lazy { ViewModelProvider(this)[DetailViewModel::class.java] }
private val adapter: DetailPagerAdapter by lazy { DetailPagerAdapter(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDetailBinding.inflate(layoutInflater)
setContentView(binding.root)
//뷰 페이저 탭 연결 하는 부분 //
with(binding) {
detailViewPager.adapter = adapter
TabLayoutMediator(tabLayoutDetail, detailViewPager) { tab, position ->
detailViewPager.currentItem = tab.position
tab.text = when (position) {
0 -> "상세 정보"
1 -> "소개 이미지"
2 -> "공연장 위치"
3 -> "기대평/리뷰"
else -> ""
}
}.attach()
}
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
//TicketFragment에서 공연 id,공연장 id 받아 오는 코드
val showId = intent.getStringExtra(Constants.SHOW_ID)
val facilityId = intent.getStringExtra(Constants.FACILITY_ID)
if (showId != null && facilityId != null) {
detailViewModel.sharedId(showId, facilityId)
}
}
}