Last active
August 19, 2021 05:14
-
-
Save hosseiniSeyRo/de0ca3f73ca35e62145da99e8fc54bc9 to your computer and use it in GitHub Desktop.
DataBinding BaseAdapter for RecyclerView by Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<data> | |
</data> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="16dp" | |
tools:context=".MyActivity"> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/rv" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:listitem="@layout/row_user_item" /> | |
</LinearLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class BaseAdapter<T>(private val itemClickListener: OnItemClickListener<T>) : | |
RecyclerView.Adapter<BaseAdapter<T>.MyViewHolder>() { | |
interface OnItemClickListener<T> { | |
fun onItemClick(item: T) | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { | |
val binding = DataBindingUtil.inflate<ViewDataBinding>( | |
LayoutInflater.from(parent.context), | |
viewType, | |
parent, | |
false | |
) | |
return MyViewHolder(binding) | |
} | |
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { | |
val item = getItemForPosition(position) | |
holder.itemView.setOnClickListener { itemClickListener.onItemClick(item) } | |
holder.bind(item) | |
} | |
abstract override fun getItemCount(): Int | |
override fun getItemViewType(position: Int): Int { | |
return getLayoutIdForPosition(position) | |
} | |
abstract fun getItemForPosition(position: Int): T | |
abstract fun getLayoutIdForPosition(position: Int): Int | |
inner class MyViewHolder(private val binding: ViewDataBinding) : | |
RecyclerView.ViewHolder(binding.root) { | |
fun bind(obj: T) { | |
binding.setVariable(BR.obj, obj) | |
binding.executePendingBindings() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyActivity : BaseActivity() { | |
private lateinit var binding: ActivityMyBinding | |
private val userList = ArrayList<User>() | |
private lateinit var mAdapter: UserAdapter | |
private lateinit var recyclerView: RecyclerView | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_my) | |
binding.lifecycleOwner = this | |
setDummyData() | |
initRecyclerView() | |
} | |
private fun setDummyData() { | |
userList.addAll( | |
listOf( | |
User("tehran", "021", 28, false), | |
User("ali ahmadi", "[email protected]", 5, false), | |
User("rastin samsami", "[email protected]", 8, true), | |
User("samad hosseini", "[email protected]", 25, false), | |
User("pooyan hosseini", "[email protected]", 33, true), | |
User("yalda soleimani", "[email protected]", 54, false) | |
) | |
) | |
} | |
private fun initRecyclerView() { | |
recyclerView = binding.rv | |
mAdapter = UserAdapter(object : BaseAdapter.OnItemClickListener<User> { | |
override fun onItemClick(item: User) { | |
Toast.makeText(this@MyActivity, ""+item.name, Toast.LENGTH_SHORT).show() | |
} | |
}) | |
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) | |
recyclerView.adapter = mAdapter | |
mAdapter.addData(userList) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<data> | |
<variable | |
name="obj" | |
type="com.rhosseini.databindingsample.data.User" /> | |
</data> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="64dp" | |
android:clickable="true" | |
android:background="?attr/selectableItemBackground" | |
android:gravity="center_vertical" | |
android:orientation="horizontal" | |
android:padding="8dp"> | |
<TextView | |
android:id="@+id/age" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="@style/TextAppearance.AppCompat.Large" | |
android:layout_marginStart="16dp" | |
android:text="@{String.valueOf(obj.age)}" | |
tools:text="36"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginStart="16dp" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="@style/TextAppearance.AppCompat.Medium" | |
android:text="@{obj.name}" | |
tools:text="samad samsami"/> | |
<TextView | |
android:id="@+id/email" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@{obj.email}" | |
tools:text="[email protected]"/> | |
</LinearLayout> | |
</LinearLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserAdapter(itemClickListener: OnItemClickListener<User>) : | |
BaseAdapter<User>(itemClickListener) { | |
private var data: ArrayList<User> = ArrayList() | |
override fun getItemCount(): Int = data.size | |
override fun getItemForPosition(position: Int): User = data[position] | |
override fun getLayoutIdForPosition(position: Int): Int { | |
return R.layout.row_user_item | |
} | |
fun addData(data: ArrayList<User>) { | |
this.data.addAll(data) | |
notifyDataSetChanged() | |
} | |
fun updateData(data: ArrayList<User>) { | |
this.data = data | |
notifyDataSetChanged() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this available with diffutils library