다음과 같이 회원가입 시 약관을 출력하는 화면을 만들어보자!
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/sign_up_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/list_margin_top"
android:background="@drawable/custom_signup_edittext"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/sign_up_all_check_txt">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/sign_up_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="10"
tools:listitem="@layout/layout_sign_up_terms_list" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?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="wrap_content">
<CheckBox
android:id="@+id/terms_list_checkbox"
android:layout_width="@dimen/check_box_size"
android:layout_height="@dimen/check_box_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/terms_list_txt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/next_btn_margin"
android:text="[필수] 만 14세 이상입니다"
android:textColor="@color/primaryTextColor"
app:layout_constraintBottom_toBottomOf="@id/terms_list_checkbox"
app:layout_constraintEnd_toStartOf="@id/terms_list_btn"
app:layout_constraintStart_toEndOf="@id/terms_list_checkbox"
app:layout_constraintTop_toTopOf="@id/terms_list_checkbox" />
<ImageView
android:id="@+id/terms_list_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icons_next"
app:layout_constraintBottom_toBottomOf="@id/terms_list_checkbox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/terms_list_checkbox"
app:tint="@color/primaryTextColor" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/terms_list_inner_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@id/terms_list_txt"
app:layout_constraintTop_toBottomOf="@id/terms_list_txt"
tools:itemCount="3"
tools:listitem="@layout/layout_sign_up_inner_terms_list" />
</androidx.constraintlayout.widget.ConstraintLayout>
class SignUpTermsAdapter(
private val context: Context,
private val mainTermsList: ArrayList<TermsList>,
private val subTermsList: ArrayList<TermsList>
) :
RecyclerView.Adapter<SignUpTermsAdapter.SignUpTermsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SignUpTermsViewHolder {
val view =
LayoutSignUpTermsListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return SignUpTermsViewHolder(view)
}
override fun onBindViewHolder(holder: SignUpTermsViewHolder, position: Int) {
val curItem = mainTermsList[position]
// checkbox
if (position >= itemCount) {
mainTermsList.add(position, TermsList(false, curItem.txt, curItem.dialog, curItem.sub))
}
holder.checkBox.isChecked = curItem.check
holder.checkBox.setOnClickListener {
curItem.check = holder.checkBox.isChecked
}
// text
holder.txt.text = curItem.txt
// dialog
if (!curItem.dialog) {
holder.dialogBtn.visibility = View.INVISIBLE
}
// 리사이클러 뷰 안 리사이클러 뷰
if (curItem.sub) {
holder.subTerms.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
holder.subTerms.adapter = SignUpInnerAdapter(subTermsList)
}
}
override fun getItemCount(): Int {
return mainTermsList.size
}
inner class SignUpTermsViewHolder(binding: LayoutSignUpTermsListBinding) :
RecyclerView.ViewHolder(binding.root) {
val checkBox: CheckBox = binding.termsListCheckbox
val txt: TextView = binding.termsListTxt
val dialogBtn: ImageView = binding.termsListBtn
val subTerms: RecyclerView = binding.termsListInnerRecyclerView
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<CheckBox
android:id="@+id/terms_list_inner_checkbox"
android:layout_width="@dimen/check_box_size"
android:layout_height="@dimen/check_box_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/terms_list_inner_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/next_btn_margin"
android:text="[선택] 이메일 수신 동의"
android:textColor="@color/primaryTextColor"
app:layout_constraintStart_toEndOf="@id/terms_list_inner_checkbox"
app:layout_constraintTop_toTopOf="@id/terms_list_inner_checkbox"
app:layout_constraintBottom_toBottomOf="@id/terms_list_inner_checkbox"/>
</androidx.constraintlayout.widget.ConstraintLayout>
class SignUpInnerAdapter(private val dataList: ArrayList<TermsList>) : RecyclerView.Adapter<SignUpInnerAdapter.SignUpInnerViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): SignUpInnerAdapter.SignUpInnerViewHolder {
val view = LayoutSignUpInnerTermsListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return SignUpInnerViewHolder(view)
}
override fun onBindViewHolder(holder: SignUpInnerAdapter.SignUpInnerViewHolder, position: Int) {
val curItem = dataList[position]
if (position >= itemCount) {
dataList.add(position, TermsList(false, curItem.txt, curItem.dialog, curItem.sub))
}
holder.checkBox.isChecked = curItem.check // data 상태에 따라 체크박스 상태 변경
holder.checkBox.setOnClickListener {
curItem.check = holder.checkBox.isChecked
}
holder.txt.text = curItem.txt
}
override fun getItemCount(): Int {
return dataList.size
}
inner class SignUpInnerViewHolder(binding: LayoutSignUpInnerTermsListBinding) : RecyclerView.ViewHolder(binding.root) {
val checkBox: CheckBox = binding.termsListInnerCheckbox
val txt: TextView = binding.termsListInnerTxt
}
}
data class TermsList (
var check: Boolean,
val txt: String,
val dialog: Boolean, // dialog 알림이 존재하는지 여부
val sub: Boolean // inner ReyclerView가 존재하는지 여부
)
app:layout_scrollFlags
와 app:layout_behavior
를 활용하면 만들 수 있다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".src.main.home.HomeFragment">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways">
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="테스트"
android:textSize="400sp"/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
app:layout_scrollFlags
의 속성
<RelativeLayout
android:id="@+id/recommend_list_inner_price_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/list_margin_top">
<TextView
android:id="@+id/recommend_list_inner_origin_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="22,000원"
android:textSize="15sp" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignStart="@id/recommend_list_inner_origin_price"
android:layout_alignEnd="@id/recommend_list_inner_origin_price"
android:layout_centerVertical="true"
android:background="@color/primaryColor" />
</RelativeLayout>