Viewpager2 + Fragment + RecyclerView

ํ‘ธ๋ฅธํ•˜๋Š˜ยท2022๋…„ 5์›” 27์ผ
0

<์‹œํ˜„ ์—ฐ์ƒ>

๐ŸŽ๊ตฌ์กฐ ์„ค๋ช…

๐ŸŽ๊ฐ„๋‹จํ•œ ์„ค๋ช…

  • Single Activity / Single Fragment
  • ๋ฐฐ๋‹ฌ์˜ ๋ฏผ์กฑ ์•ฑ์„ ๊ฐ„๋‹จํ•œ ๊ธฐ๋Šฅ์„ ๋ชจ๋ฐฉํ•ด์„œ ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
  • Fragment ์•ˆ์— newInstance Bundle์„ ํ™œ์šฉํ•˜์—ฌ Fragment์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ํ™œ์šฉ ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • adapter ์— ๋“ค์–ด์˜ค๋Š” choice(Int) ๋งˆ๋‹ค Adapter์— ๋‹ค๋ฅธ DB๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๐ŸŽ๊ตฌํ˜„ ์ˆœ์„œ

  1. acivity_main.xml
  2. fragment_my.xml
  3. menu_items.xml
  4. RecipeCateogory
  5. MyFragment
  6. PagerFragmentStateAdapter
  7. MainActivity
  8. MyFragmentAdapter
  9. Menu_lists

๐ŸŽํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

 viewBinding{
        enabled true
    }

๐ŸŽactivity_main

<?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=".screen.main.MainActivity">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/toolbarAppbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        android:elevation="0dp">

    </com.google.android.material.appbar.AppBarLayout>
   <androidx.appcompat.widget.Toolbar
       android:id="@+id/toolbar"
       style="?attr/toolbarStyle"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:elevation="0dp"
       android:fitsSystemWindows="false"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:contentInsetStart="0dp"

       app:contentInsetEnd="0dp">

       <ImageView
           android:id="@+id/backBtn"

           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/ic_baseline_arrow_back_24"/>
       <TextView
           android:text="ํ…Œ๋งˆ๋ณ„ ๋ ˆ์‹œํ”ผ"
           android:textStyle="bold"
           android:textSize="20sp"
           android:layout_gravity="center_horizontal"
           android:layout_width="wrap_content"

           android:layout_height="wrap_content"/>

       <ImageView
           android:src="@drawable/ic_baseline_search_24"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="right"
           android:layout_marginRight="10dp"
           />
   </androidx.appcompat.widget.Toolbar>
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbarlayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        >

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/white"
            app:tabGravity="fill"
            app:tabIndicatorFullWidth="false"
            app:tabMaxWidth="0dp"
            app:tabMode="auto"
            app:tabIndicatorColor="@color/design_default_color_primary"
            app:tabRippleColor="@color/design_default_color_primary"
            app:tabSelectedTextColor="@color/design_default_color_primary"
            app:tabTextColor="@color/black" />

    </com.google.android.material.appbar.AppBarLayout>
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appbarlayout" />


</androidx.constraintlayout.widget.ConstraintLayout>

๐ŸŽfragment_my

<?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=".screen.myFragment.MyFragment">



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

</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">


    <ImageView
        android:id="@+id/menuImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@drawable/ic_baseline_search_24" />

    <TextView
        android:id="@+id/menuTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        app:layout_constraintStart_toEndOf="@id/menuImageView"
        app:layout_constraintTop_toTopOf="@id/menuImageView"
        tools:text="hello" />

    <TextView
        android:id="@+id/menuDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/menuTextView"
        app:layout_constraintTop_toTopOf="@id/menuImageView"
        tools:text="hello my name is ck" />

</androidx.constraintlayout.widget.ConstraintLayout>

๐ŸŽRecipeCategory

enum class RecipeCateogry(
    @StringRes val categoryNameId: Int,
    @StringRes val categoryTypeId: Int
) {
    MAIN_FOOD(R.string.main_food, R.string.main_food_type),
    SUB_FOOD (R.string.sub_food, R.string.sub_food_type),
    DUMPLING_FOOD(R.string.dumpling_food, R.string.dumpling_food_type),
    KOREAN_FOOD(R.string.korean_food, R.string.korean_food_type),
    CHINESE_FOOD(R.string.chinese_food, R.string.chinese_food_type),
    ASIAN_EUROPE_FOOD(R.string.asian_europe_food, R.string.asian_europe_food_type),
    JAPANESE_FOOD(R.string.japanese_food, R.string.japanese_food_type),
    CAFE_DESSERT(R.string.cafe_dessert, R.string.cafe_dessert_type),



}

๐ŸR.string.recipe_cateory.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="main_food">๋ฉ”์ธ์š”๋ฆฌ</string>
    <string name="sub_food">์„œ๋ธŒ์š”๋ฆฌ</string>
    <string name="dumpling_food">๋ถ„์‹</string>
    <string name="korean_food">ํ•œ์‹</string>
    <string name="chinese_food">์ค‘์‹</string>
    <string name="asian_europe_food">์•„์‹œ์•ˆโ€ข์–‘์‹</string>
    <string name="japanese_food">์ผ์‹โ€ข๋ˆ๊นŒ์Šคโ€ขํšŒ</string>
    <string name="cafe_dessert">์นดํŽ˜โ€ข๋””์ €ํŠธ</string>


    <string name="main_food_type">๋ฉ”์ธ์š”๋ฆฌ</string>
    <string name="sub_food_type">์„œ๋ธŒ์š”๋ฆฌ</string>
    <string name="dumpling_food_type">๋ถ„์‹</string>
    <string name="korean_food_type">ํ•œ์‹</string>
    <string name="chinese_food_type">์ค‘์‹</string>
    <string name="asian_europe_food_type">์•„์‹œ์•ˆ;์–‘์‹</string>
    <string name="japanese_food_type">์ผ์‹;๋ˆ๊นŒ์Šค;ํšŒ</string>
    <string name="cafe_dessert_type">์นดํŽ˜;๋””์ €ํŠธ</string>

</resources>

๐ŸŽMyFragment

class MyFragment : BaseFragment<FragmentMyBinding>(FragmentMyBinding::inflate) {



    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val choice =arguments?.getInt(MENUCATEGORY_ITEM)
        val viewpager = binding.recyclerView

        viewpager.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)


        choice?.let {
            when(choice){
                0 -> viewpager.adapter = MyFragmentAdapter(MenuLists.mainFood)
                1 -> viewpager.adapter = MyFragmentAdapter(MenuLists.subFood)
                2 -> viewpager.adapter = MyFragmentAdapter(MenuLists.mainFood)
                3 -> viewpager.adapter = MyFragmentAdapter(MenuLists.subFood)
                4 -> viewpager.adapter = MyFragmentAdapter(MenuLists.mainFood)
                5 -> viewpager.adapter = MyFragmentAdapter(MenuLists.subFood)
                6 -> viewpager.adapter = MyFragmentAdapter(MenuLists.mainFood)
                7 -> viewpager.adapter = MyFragmentAdapter(MenuLists.subFood)
                else -> viewpager.adapter = MyFragmentAdapter(MenuLists.mainFood)


            }
        }



    }


    companion object {
        val MENUCATEGORY_ITEM = "menucateogry_item"
        @JvmStatic
        fun newInstance(position : Int) =
            MyFragment().apply {
                arguments = Bundle().apply {
                    putInt(MENUCATEGORY_ITEM, position)
                }
            }
    }
}

๐ŸŽPagerFragmentStateAdapter

class PagerFragmentStateAdapter(fa: FragmentActivity): FragmentStateAdapter(fa) {
    private val NUM_PAGES = 8

    override fun getItemCount(): Int = NUM_PAGES

    override fun createFragment(position: Int) = MyFragment.newInstance(position)
}

๐ŸŽMainActivity

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val tabIcon = listOf(
    // ์ž„์˜๋กœ ์›ํ•˜๋Š” ์•„์ด์ฝ˜(์‚ฌ์ง„) ์ฐพ์•„์„œ ์‚ฝ์ž…
        R.drawable.ic_launcher_foreground,
        R.drawable.ic_baseline_access_time_filled_24,
        R.drawable.ic_baseline_60fps_24
    )
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.viewpager.apply{
            adapter = PagerFragmentStateAdapter(context as FragmentActivity)
        }
		//tablayout์— ๋“ค์–ด๊ฐˆ recipeGategory ์‚ฝ์ž…
        TabLayoutMediator(binding.tabs,binding.viewpager){tab, position ->
           tab.setText(RecipeCateogry.values()[position].categoryNameId)
        }.attach()

    }
}

MyFragmentAdapter

class MyFragmentAdapter(private val menuList : List<Menu>) : RecyclerView.Adapter<MyFragmentAdapter.MenuViewHolder>(){

    inner class MenuViewHolder(private val binding: MenuItemsBinding): RecyclerView.ViewHolder(binding.root){
        fun bind(menu : Menu){
            binding.menuImageView.setImageResource(menu.menuImage)
            binding.menuDescription.text = menu.description
            binding.menuTextView.text = menu.title
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MenuViewHolder {
        return  MenuViewHolder(
            MenuItemsBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        )
    }

    override fun onBindViewHolder(holder: MenuViewHolder, position: Int) {
      holder.bind(menuList[position])
    }

    override fun getItemCount(): Int  = menuList.size
}
object MenuLists {

    val mainFood = listOf(
        Menu(R.drawable.ic_baseline_60fps_24,"๋ฉ”์ธํ‘ธ๋“œ","๋„ˆ๋ฌด๋„ˆ๋ฌด ๋ง›์žˆ๊ฒŒ  ๋งŒ๋“  ๊น€์น˜ ์ฐŒ๊ฐœ ๋จน์„์‚ฌ๋ž‘?"),
        Menu(R.drawable.ic_baseline_rowing_24,"์ œ์œก๋ณถ์Œ","์ œ์œก๋ณถ์Œ"),
        Menu(R.drawable.ic_baseline_60fps_24,"๊น€์น˜์ฐŒ๊ฐœ","๋„ˆ๋ฌด๋„ˆ๋ฌด ๋ง›์žˆ๊ฒŒ  ๋งŒ๋“  ๊น€์น˜ ์ฐŒ๊ฐœ ๋จน์„์‚ฌ๋ž‘?"),
        Menu(R.drawable.ic_baseline_rowing_24,"์ œ์œก๋ณถ์Œ","์ œ์œก๋ณถ์Œ"),
        Menu(R.drawable.ic_baseline_60fps_24,"๊น€์น˜์ฐŒ๊ฐœ","๋„ˆ๋ฌด๋„ˆ๋ฌด ๋ง›์žˆ๊ฒŒ  ๋งŒ๋“  ๊น€์น˜ ์ฐŒ๊ฐœ ๋จน์„์‚ฌ๋ž‘?"),
        Menu(R.drawable.ic_baseline_rowing_24,"์ œ์œก๋ณถ์Œ","์ œ์œก๋ณถ์Œ"),
        )

    val subFood = listOf(
        Menu(R.drawable.ic_baseline_60fps_24,"์„œ๋ธŒํ‘ธ๋“œ","๋„ˆ๋ฌด๋„ˆ๋ฌด ๋ง›์žˆ๊ฒŒ  ๋งŒ๋“  ๊น€์น˜ ์ฐŒ๊ฐœ ๋จน์„์‚ฌ๋ž‘?"),
        Menu(R.drawable.ic_baseline_rowing_24,"์ œ์œก๋ณถ์Œ","์ œ์œก๋ณถ์Œ"),
        Menu(R.drawable.ic_baseline_60fps_24,"๊น€์น˜์ฐŒ๊ฐœ","๋„ˆ๋ฌด๋„ˆ๋ฌด ๋ง›์žˆ๊ฒŒ  ๋งŒ๋“  ๊น€์น˜ ์ฐŒ๊ฐœ ๋จน์„์‚ฌ๋ž‘?"),
        Menu(R.drawable.ic_baseline_rowing_24,"์ œ์œก๋ณถ์Œ","์ œ์œก๋ณถ์Œ"),
        Menu(R.drawable.ic_baseline_60fps_24,"๊น€์น˜์ฐŒ๊ฐœ","๋„ˆ๋ฌด๋„ˆ๋ฌด ๋ง›์žˆ๊ฒŒ  ๋งŒ๋“  ๊น€์น˜ ์ฐŒ๊ฐœ ๋จน์„์‚ฌ๋ž‘?"),
        Menu(R.drawable.ic_baseline_rowing_24,"์ œ์œก๋ณถ์Œ","์ œ์œก๋ณถ์Œ"),
    )
}

gitHub ์†Œ์Šค (์ด๋ฏธ์ง€ ํด๋ฆญ)

0๊ฐœ์˜ ๋Œ“๊ธ€