Skip to content

5.8. ํšŒ๊ณ 

mdb1217 edited this page Jan 15, 2021 · 2 revisions

WrittenBy

๊ตฌํ˜„ ๋ฐฉ๋ฒ•

ํ‰๊ฐ€ ๋ฐ ํšŒ๊ณ  Main

(1) ๋ฆฌํฌํŠธ ๋ทฐ์™€ ํšŒ๊ณ ๋ทฐ์˜ ์ „ํ™˜์„ ์œ„ํ•ด ViewPager2 ์‚ฌ์šฉ

๋ฆฌํฌํŠธ Main

(1) RecyclerView ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ Display

(2) ๋งŒ์กฑ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ์œ„ํ•ดSeekBar ์‚ฌ์šฉ

(3) RecyclerView์—์„œ Item touch์‹œ fragment ์ „ํ™˜(+๋ฐ์ดํ„ฐ ์ „๋‹ฌ)

๋ฆฌํฌํŠธ Detail

(1) RecyclerView (staggeredgrid layout)์‚ฌ์šฉํ•ด์„œ ํ•˜๋ฃจ๊ธฐ๋ก Display

ํšŒ๊ณ  Main

(1) EditText์˜ text๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ๋ฐ”์ดํŠธ ์ˆ˜๋ฅผ Displayํ•˜๊ธฐ ์œ„ํ•ด EditText.addTextChangedListener ์‚ฌ์šฉ

(2) RecyclerView ์‚ฌ์šฉ

ํšŒ๊ณ  Write

(1) EditText์˜ text๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ๋ฐ”์ดํŠธ ์ˆ˜๋ฅผ Displayํ•˜๊ธฐ ์œ„ํ•ด EditText.addTextChangedListener ์‚ฌ์šฉ

๊ตฌํ˜„ ์ฝ”๋“œ

ํ‰๊ฐ€ ๋ฐ ํšŒ๊ณ  Main

(1) ๋ฆฌํฌํŠธ ๋ทฐ์™€ ํšŒ๊ณ ๋ทฐ์˜ ์ „ํ™˜์„ ์œ„ํ•ด ViewPager2 ์‚ฌ์šฉ

  • fragment_remind.xml
   	<androidx.viewpager2.widget.ViewPager2
            android:id="@+id/vp_remind"
            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/cl_tab" />
  • RemindvpAdapter.kt
class RemindViewPagerAdapter(fragment : Fragment) : FragmentStateAdapter(fragment.activity!!) {
    var fragmentList = listOf<Fragment>()

    override fun getItemCount(): Int {
        return fragmentList.count()
    }

    override fun createFragment(position: Int): Fragment {
        return when(position){
            0 -> ReportFragment()
            else -> RemindWriteFragment()
        }
    }
}
  • RemindFragment.kt
fun createViewpager() {
    var tab_label = listOf("๋ฆฌํฌํŠธ", "ํšŒ๊ณ ")
    var fragmentList = listOf(ReportFragment(), RemindFragment())
    val remindAdapter = RemindViewPagerAdapter(this)
    remindAdapter.fragmentList = fragmentList

    vp_remind.adapter = remindAdapter

    TabLayoutMediator(tb_remind, vp_remind){tab, position->
        tab.text = tab_label[position]
    }.attach()
}

๋ฆฌํฌํŠธ Main

(1) RecyclerView ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ Display

  • ReportKeywordAdapter.kt
class ReportKeywordAdapter(private val context: Context, listener : OnItemClick) : RecyclerView.Adapter<ReportKeywordAdapter.ViewHolder>() {
    private lateinit var binding: KeywordListItemBinding
    var data = mutableListOf<ReportListData>()
    private val mCallback = listener

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context),
            R.layout.keyword_list_item, parent, false)
        return ViewHolder(binding)
    }

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

    override fun getItemCount(): Int = data.size

    inner class ViewHolder(private val binding: KeywordListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(data: ReportListData, pos : Int) {
            val tasknum = data.task_num.toString()
            binding.reportlistdata = data
            binding.tvTasknum.text = "์ด "+tasknum+"๊ฐœ"
            binding.tvRate.text = data.rate.toString()
            if(data.rate > 0) {
                binding.tvRate.setTextColor(Color.parseColor("#EC684A"))
            }
            else {
                binding.tvRate.setTextColor(Color.parseColor("#E5E5E5"))
            }
            binding.sbRate.setProgress(((data.rate * 10).toInt()),true)

            binding.sbRate.setOnTouchListener(object : View.OnTouchListener {
                override fun onTouch(v: View, event: MotionEvent): Boolean {
                    return true
                }
            })//์‹œํฌ๋ฐ” ํ„ฐ์น˜ prevent

            binding.root.setOnClickListener{
                mCallback.onClick(pos)
            }

        }
    }
}
  • ReportFragment.kt
private fun createReportAdapter() {
    reportKeywordAdapter = activity?.let { ReportKeywordAdapter(it, this) }!!
    binding.rcvReport.adapter = reportKeywordAdapter
    binding.rcvReport.layoutManager = LinearLayoutManager(activity)
}

(2) ๋งŒ์กฑ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ์œ„ํ•ดSeekBar ์‚ฌ์šฉ

  • keyword_list_item.xml
       <SeekBar
                android:id="@+id/sb_rate"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="13dp"
                android:layout_marginBottom="13dp"
                android:max="50"
                android:progress="25"
                android:progressDrawable="@drawable/sb_rate_custom"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tv_tasknum" />

(3) RecyclerView์—์„œ Item touch์‹œ fragment ์ „ํ™˜(+๋ฐ์ดํ„ฐ ์ „๋‹ฌ)

  • ReportFragment.kt

    override fun onClick(value: Int) {
        val bundle = Bundle()
        var reportDetailFragment = ReportDetailFragment()
        bundle.putInt("data_priority", value);
        reportDetailFragment.arguments = bundle
    
        activity!!.supportFragmentManager
            .beginTransaction()
            .replace(R.id.container_main, reportDetailFragment)
            .commit()
    }
    

๋ฆฌํฌํŠธ Detail

(1) RecyclerView (staggeredgrid layout)์‚ฌ์šฉํ•ด์„œ ํ•˜๋ฃจ๊ธฐ๋ก Display

  • ReportDetailAdapter.kt
class ReportDetailAdapter(private val context: Context) : RecyclerView.Adapter<ReportDetailAdapter.ViewHolder>() {
    private lateinit var binding: ReportListItemBinding
    var data = mutableListOf<ReportListData.Daily>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        binding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.report_list_item, parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ReportDetailAdapter.ViewHolder, position: Int) {
        holder.bind(data[position])
    }

    override fun getItemCount(): Int = data.size

    inner class ViewHolder(private val binding: ReportListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(data: ReportListData.Daily) {
            val rate = data.satisfaction.toString()
            binding.dailydata = data
            binding.tvTaskRate.text = "ํƒœ์Šคํฌ ๋งŒ์กฑ๋„: " + rate + "์ "
        }

    }
}
  • ReportDetailFragment.kt

    private fun createDetailAdapter() {
        detailAdapter = activity?.let { ReportDetailAdapter(it) }!!
        binding.rcvDailyRecord.adapter = detailAdapter
        binding.rcvDailyRecord.layoutManager = StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL)
    }
    

ํšŒ๊ณ  Main

  • RemindWriteFragment
class RemindWriteFragment : BaseFragment<FragmentRemindWriteBinding, RemindViewModel>() {
    override val layoutResourceId: Int
        get() = R.layout.fragment_remind_write
    override val viewModel: RemindViewModel by sharedViewModel()
    private var start: Long = 0
    private var end: Long = 0

    override fun initView() {
        initSetting()
        startRmdindDetailWrite()
        scrollEvent()
    }

    override fun initBeforeBinding() {
        binding.lifecycleOwner = activity
    }

    override fun initAfterBinding() {
        observeReview()
        observeStartEnd()
    }

    private fun startRemindDetailWriteActivityWithAction(
        tv_content: TextView,
        tV_title: TextView,
        subType: Int,
        start: Long,
        end: Long
    ) {
        val intent: Intent =
            Intent(requireActivity(), RemindDetailWriteActivity::class.java).apply {
                putExtra("remind", tv_content.text.toString())
                putExtra("title", tV_title.text.toString())
                putExtra("subType", subType)
                putExtra("start", start)
                putExtra("end", end)
            }
        if (tv_content.text.isNotEmpty()) {
            intent.putExtra("isWriten", true)
        } else {
            intent.putExtra("isWriten", false)
        }
        startActivity(intent)
    }

    private fun startRmdindDetailWrite() {
        binding.tvWeekGoodContent.setOnClickListener {
            startRemindDetailWriteActivityWithAction(
                binding.tvWeekGoodContent,
                binding.tvWeekGoodTitle,
                1,
                start,
                end
            )
        }
        binding.tvWeekBadContent.setOnClickListener {
            startRemindDetailWriteActivityWithAction(
                binding.tvWeekBadContent,
                binding.tvWeekBadTitle,
                2,
                start,
                end
            )
        }
        binding.tvNextWeekContent.setOnClickListener {
            startRemindDetailWriteActivityWithAction(
                binding.tvNextWeekContent,
                binding.tvNextWeekTitle,
                3,
                start,
                end
            )
        }
    }

    private fun observeStartEnd() {
        viewModel.startEnd.observe(requireActivity(), {
            start = it[0]
            end = it[1]
        })
    }

    private fun observeReview() {
        viewModel.reviewList.observe(requireActivity(), {
            if (it.isWritten) {
                if (it.review.good != null) {
                    if(it.review.good.isNotEmpty()) {
                        binding.tvWeekGoodContent.text =
                            Editable.Factory.getInstance().newEditable(it.review.good)
                        binding.tvWeekGoodContent.setBackgroundResource(R.drawable.rectangle_cherry_radius_15)
                        binding.tvGoodCount.text = it.review.good.length.toString()
                    }
                    else {
                        binding.tvWeekGoodContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                        binding.tvWeekGoodContent.text = null
                        binding.tvGoodCount.text = getString(R.string.zero)
                    }
                } else {
                    binding.tvWeekGoodContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                    binding.tvWeekGoodContent.text = null
                    binding.tvGoodCount.text = getString(R.string.zero)
                }

                if (it.review.bad != null) {
                    if(it.review.bad.isNotEmpty()) {
                        binding.tvWeekBadContent.text =
                            Editable.Factory.getInstance().newEditable(it.review.bad)
                        binding.tvWeekBadContent.setBackgroundResource(R.drawable.rectangle_cherry_radius_15)
                        binding.tvBadCount.text = it.review.bad.length.toString()
                    }
                    else {
                        binding.tvWeekBadContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                        binding.tvBadCount.text = getString(R.string.zero)
                        binding.tvWeekBadContent.text = null
                    }
                } else {
                    binding.tvWeekBadContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                    binding.tvBadCount.text = getString(R.string.zero)
                    binding.tvWeekBadContent.text = null
                }

                if (it.review.next != null) {
                    if(it.review.next.isNotEmpty()) {
                        binding.tvNextWeekContent.text =
                            Editable.Factory.getInstance().newEditable(it.review.next)
                        binding.tvNextWeekContent.setBackgroundResource(R.drawable.rectangle_cherry_radius_15)
                        binding.tvNextWeekCount.text = it.review.next.length.toString()
                    }
                    else {
                        binding.tvNextWeekContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                        binding.tvNextWeekContent.text = null
                        binding.tvNextWeekCount.text = getString(R.string.zero)
                    }
                } else {
                    binding.tvNextWeekContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                    binding.tvNextWeekContent.text = null
                    binding.tvNextWeekCount.text = getString(R.string.zero)
                }
            } else {
                binding.tvNextWeekContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                binding.tvWeekGoodContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                binding.tvWeekBadContent.setBackgroundResource(R.drawable.rectangle_fill_main_light_gray_15)
                binding.tvNextWeekContent.text = null
                binding.tvWeekGoodContent.text = null
                binding.tvWeekBadContent.text = null
                binding.tvGoodCount.text = getString(R.string.zero)
                binding.tvBadCount.text = getString(R.string.zero)
                binding.tvNextWeekCount.text = getString(R.string.zero)
            }
        })
    }

    private fun initSetting() {
        binding.tvWeekGoodContent.movementMethod = ScrollingMovementMethod()
        binding.tvWeekBadContent.movementMethod = ScrollingMovementMethod()
        binding.tvNextWeekContent.movementMethod = ScrollingMovementMethod()
    }

    private fun scrollEvent() {
        binding.tvWeekGoodContent.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                binding.svEntire.requestDisallowInterceptTouchEvent(true)
                return false
            }
        })

        binding.tvWeekBadContent.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                binding.svEntire.requestDisallowInterceptTouchEvent(true)
                return false
            }
        })

        binding.tvNextWeekContent.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                binding.svEntire.requestDisallowInterceptTouchEvent(true)
                return false
            }
        })
    }
}

ํšŒ๊ณ  Write

  • RemindDetailWriteActivity.kt
class RemindDetailWriteActivity : BaseActivity<ActivityRemindDetailWriteBinding, RemindViewModel>() {
    override val layoutResourceId: Int
        get() = R.layout.activity_remind_detail_write
    override val viewModel: RemindViewModel by viewModel()
    private var isContentEntered = false
    private var isWriten: Boolean = true

    override fun initView() {
        initViewSetting()
        initToolbar()
        initEditText()
        initWriteCompleteButton()
        initBtnSetting()
    }

    override fun initBeforeBinding() {

    }

    override fun initAfterBinding() {

    }

    private fun initToolbar() {
        setSupportActionBar(binding.tbRemindDetailWrite)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        binding.tvTitle.text = getString(R.string.menu_remind)
        binding.tbRemindDetailWrite.setNavigationOnClickListener {
            if(isContentEntered) {
                val builder = AlertDialog.Builder(this)
                if(!isWriten) {
                    builder.setTitle(getString(R.string.remind_back_really))
                    builder.setMessage(getString(R.string.remind_back_alert))
                    builder.setPositiveButton(
                    getString(R.string.cancel)
                    ) { dialogInterface: DialogInterface?, i: Int -> }
                    builder.setNegativeButton(getString(R.string.remove)) { dialogInterface: DialogInterface?, i: Int ->
                        finish()
                    }
                }
                else {
                    builder.setTitle(getString(R.string.remind_back_really))
                    builder.setMessage(getString(R.string.remind_back_edit_alert))
                    builder.setPositiveButton(
                        getString(R.string.cancel)
                    ) { dialogInterface: DialogInterface?, i: Int -> }
                    builder.setNegativeButton(getString(R.string.back)) { dialogInterface: DialogInterface?, i: Int ->
                        finish()
                    }
                }
                builder.show()
            }
            else {
                finish()
            }
        }
    }

    private fun initEditText() {
        binding.tvRemindCount.text = binding.etRemindText.length().toString()
        binding.etRemindText.addTextChangedListener {
            val length = binding.etRemindText.length()
            binding.tvRemindCount.text = length.toString()
            if(!isWriten) {
                isContentEntered = length > 0
                binding.btWriteComplete.isEnabled = isSaveButtonEnabled()
            }
            else {
                isContentEntered = intent.getStringExtra("remind") != binding.etRemindText.text.toString()
                binding.btWriteComplete.isEnabled = isSaveButtonEnabled()
            }
            if(isContentEntered)
                binding.btWriteComplete.setBackgroundResource(R.drawable.btn_write_complete)
            else {
                binding.btWriteComplete.setBackgroundResource(R.drawable.btn_write_incomplete)
            }
        }
    }

    private fun isSaveButtonEnabled(): Boolean = isContentEntered

    private fun initWriteCompleteButton() {
        binding.btWriteComplete.setOnClickListener {
            viewModel.postReview(
                ReqReviewAdd(
                    intent.getLongExtra("start", 0),
                    intent.getLongExtra("end", 0),
                    Calendar.getInstance().timeInMillis,
                    intent.getIntExtra("subType", 0),
                    binding.etRemindText.text.toString()
                )
            )
            finish()
        }
    }

    private fun initViewSetting(){
        binding.tvRemindTitle.text = intent.getStringExtra("title")
        isWriten = intent.getBooleanExtra("isWriten", true)
        binding.etRemindText.text = Editable.Factory.getInstance().newEditable(intent.getStringExtra("remind"))
    }

    private fun initBtnSetting(){
        if(!isWriten)
            binding.btWriteComplete.text = getString(R.string.msg_add_complete)
        else {
            binding.btWriteComplete.text = getString(R.string.remind_restore)
        }
    }
}

์‹คํ–‰ ํ™”๋ฉด

๋ฆฌํฌํŠธ Main
img
Clone this wiki locally