Skip to content

5.5. 키워드 초기 설정

JiHwan edited this page Jan 15, 2021 · 6 revisions

구현 방법

✔️ KeywordDefineAddView

Main

  • 그 전 Fragment에서 Text를 받아와서 뿌려줌

✔️ KeywordDefineDetailFragment

Main

  • 그 전 Fragment에서 Text를 받아와서 뿌려줌

✔️ KeywordDefine

Main

  • 그 전 Fragment에서 Text를 받아와서 뿌려줌

✔️ KeywordPriorityFragment

Main

  • 그 전 Fragment에서 Text를 받아와서 뿌려줌

구현 코드

✔️ KeywordDefineAddFragment.kt

private fun initEditText() {
    binding.tvKeyword.text = keyword

    binding.etDefine.addTextChangedListener {
        val length = binding.etDefine.length()
        binding.tvDefineByte.text = length.toString()
        binding.btnSetPriority.isEnabled = length > 0
    }
}

private fun initButton() {
    binding.btnSetPriority.isEnabled = false
    binding.btnSetPriority.setOnClickListener {
        viewModel.postKeywordDefinition(name = keyword!!, definition = binding.etDefine.text.toString())
        viewModel.isDefineSet[position] = true
        popBackStack()
    }
}

✔️ KeywordDefineDetailFragment.kt

private fun initToolbar() {
        binding.tbKeywordDefineDetail.setNavigationOnClickListener {
            popBackStack()
        }
    }

    private fun observeKeywordDefinition() {
        viewModel.keywordDefinition.observe(viewLifecycleOwner, {
            binding.tvKeyword.text = it.name
            binding.etDefine.setText(it.definition)
        })
    }

✔️ KeywordDefineFragment.kt

        keywordDefineAdapter.setKeywordExistListener { keyword, position ->
            //정의 없을때
            val bundle = Bundle().apply {
                putString("keyword", keyword.name)
                putInt("position", position)
            }
            val keywordDefineAddFragment = KeywordDefineAddFragment().apply {
                arguments = bundle
            }
            replaceAndAddBackStack(
               R.id.container_keyword_settings,
                keywordDefineAddFragment,
                "add"
            )
        }

✔️ KeywordPriorityFragment.kt

    private fun initRecyclerView() {
        val itemTouchHelper = ItemTouchHelper(ItemTouchHelperCallback(keywordPriorityAdapter))

        val dividerItemDecoration = DividerItemDecoration(requireContext(),LinearLayoutManager.VERTICAL)
        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(requireContext(),R.drawable.divider_recyclerview)!!)

        binding.rvPriority.apply {
            adapter = keywordPriorityAdapter
            layoutManager = LinearLayoutManager(requireContext())
            addItemDecoration(dividerItemDecoration)
            setHasFixedSize(true)
            itemTouchHelper.attachToRecyclerView(this)
        }

        keywordPriorityAdapter.data = keywordList?.toList() ?: listOf()
    }

실행 화면

image image image image