From feb3b949b33d4c4075f7478c5183fe627bc28f07 Mon Sep 17 00:00:00 2001 From: Jintin Date: Sat, 19 Dec 2020 17:55:38 +0800 Subject: [PATCH 1/2] add holder --- .../bindingextension/app/MainFragment.kt | 29 +++++++++++++++++-- app/src/main/res/layout/adapter_main.xml | 6 ++++ app/src/main/res/layout/fragment_main.xml | 19 ++---------- .../jintin/bindingextension/BindingHolder.kt | 17 +++++++++++ 4 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 app/src/main/res/layout/adapter_main.xml create mode 100644 lib/src/main/java/com/jintin/bindingextension/BindingHolder.kt diff --git a/app/src/main/java/com/jintin/bindingextension/app/MainFragment.kt b/app/src/main/java/com/jintin/bindingextension/app/MainFragment.kt index bcdc5eb..9c1b5b2 100644 --- a/app/src/main/java/com/jintin/bindingextension/app/MainFragment.kt +++ b/app/src/main/java/com/jintin/bindingextension/app/MainFragment.kt @@ -2,16 +2,41 @@ package com.jintin.bindingextension.app import android.os.Bundle import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.jintin.bindingextension.BindingFragment +import com.jintin.bindingextension.BindingHolder +import com.jintin.bindingextension.app.databinding.AdapterMainBinding import com.jintin.bindingextension.app.databinding.FragmentMainBinding class MainFragment : BindingFragment(FragmentMainBinding::inflate) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + binding.recyclerView.layoutManager = + LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) + binding.recyclerView.adapter = MainAdapter((1..100).map { "#$it" }) + } + + class MainAdapter(private val list: List) : + RecyclerView.Adapter() { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(parent) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.bind(list[position]) + } + + override fun getItemCount() = list.size + + class ViewHolder(parent: ViewGroup) : + BindingHolder(parent, AdapterMainBinding::inflate) { - binding.button.setOnClickListener { - binding.button.setText(R.string.fragment_label) + fun bind(data: String) { + binding.name.text = data + } } } } \ No newline at end of file diff --git a/app/src/main/res/layout/adapter_main.xml b/app/src/main/res/layout/adapter_main.xml new file mode 100644 index 0000000..e927fa5 --- /dev/null +++ b/app/src/main/res/layout/adapter_main.xml @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml index bb0cb77..337202a 100644 --- a/app/src/main/res/layout/fragment_main.xml +++ b/app/src/main/res/layout/fragment_main.xml @@ -1,20 +1,7 @@ - - -