Skip to content

Commit

Permalink
add holder
Browse files Browse the repository at this point in the history
  • Loading branch information
Jintin committed Dec 19, 2020
1 parent da4043b commit feb3b94
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
29 changes: 27 additions & 2 deletions app/src/main/java/com/jintin/bindingextension/app/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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<String>) :
RecyclerView.Adapter<MainAdapter.ViewHolder>() {
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<AdapterMainBinding>(parent, AdapterMainBinding::inflate) {

binding.button.setOnClickListener {
binding.button.setText(R.string.fragment_label)
fun bind(data: String) {
binding.name.text = data
}
}
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/adapter_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
19 changes: 3 additions & 16 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
<?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"
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
tools:context=".MainActivity" />
17 changes: 17 additions & 0 deletions lib/src/main/java/com/jintin/bindingextension/BindingHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jintin.bindingextension

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

open class BindingHolder<V : ViewBinding>(
val binding: V
) : RecyclerView.ViewHolder(binding.root) {

constructor(
parent: ViewGroup,
provider: (LayoutInflater, ViewGroup?, Boolean) -> V
) : this(provider.invoke(LayoutInflater.from(parent.context), parent, false))

}

0 comments on commit feb3b94

Please sign in to comment.