View

2024.03.18

제롱구리 2024. 3. 18. 20:59
728x90

오늘 한일

문답 부분 UI작성

[Activity]

class MainActivity : AppCompatActivity(), CategoryRecyclerListener{

    private val categories: ArrayList<String> by lazy {
        arrayListOf("경제", "소통", "가치관", "생활", "자녀", "가족", "부부관계", "건강", "결혼식", "미래", "현재", "과거" )
    }
    private lateinit var categoryAdapter: CategoryAdapter

    private val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        init()
    }

    private fun init(){
        setCategoryAdapter(categories)
    }

    private fun setCategoryAdapter(categoryList: ArrayList<String>){
        categoryAdapter = CategoryAdapter(categoryList)
        val categoryManager = GridLayoutManager(this, 3)
        binding.rvCategory.apply {
            layoutManager = categoryManager
            adapter = categoryAdapter
        }
    }

    override fun onItemClick(position: Int) {
        //jetpack으로 데이터를 전송하는 로직
        Toast.makeText(this, "아이템 클릭", Toast.LENGTH_SHORT)
    }

}


[adapter]

class CategoryAdapter(
    private val categoryItems: ArrayList<String>
): RecyclerView.Adapter<CategoryViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
        val itemBinding = ItemCategoryBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return CategoryViewHolder(itemBinding)
    }

    override fun getItemCount(): Int {
        return categoryItems.size
    }

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

[ViewHolder]

class CategoryViewHolder(
    private val binding: ItemCategoryBinding
): RecyclerView.ViewHolder(binding.root){


    fun bind(category: String){
        binding.itemTv.text = category
    }
}

[RecyclerListener] (인터페이스)

interface CategoryRecyclerListener {
    fun onItemClick(position: Int)
}


현재 원인 모를 이유로 모듈 디바이스의 실행 오류가 나고 있다...코드에는 이상이 없어보인데 그냥 디바이스 오류인거 같다 추후에 컴퓨터 다시 켜보고 실행해서 해당 업무일지 수정할 예정!

'메리 업무일지' 카테고리의 다른 글

2024.03.25  (0) 2024.03.26
2024.03.21  (0) 2024.03.22
2024.03.20  (0) 2024.03.21
2024.03.13  (0) 2024.03.13
Share Link
reply
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31