项目作者: vipafattal

项目描述 :
RecyclerView Adapter library using Kotlin
高级语言: Kotlin
项目地址: git://github.com/vipafattal/KotlinRecycler.git
创建时间: 2019-12-04T21:54:17Z
项目社区:https://github.com/vipafattal/KotlinRecycler

开源协议:MIT License

下载



(http://www.apache.org/licenses/LICENSE-2.0)

KotlinRecycler

A set of Kotlin extensions that simplify RecyclerView and eliminate the Adapter boilerplate code.

ReyclerView made simple as one line of code

  1. recyclerView.withSimpleAdapter(dummyData, R.layout.item_recipe) { data ->
  2. recipe_img.setImageResource(data.drawable)
  3. recipe_name.text = data.name
  4. }

Setup

Step 1

add in your root build.gradle at the end of repositories

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Step 2

add the dependency to the app module

  1. implementation 'com.github.vipafattal:KotlinRecycler:1.2.1'

Step 3

add this line to the app module to enable view extensions in RecyclrView Holder

  1. androidExtensions {
  2. experimental = true
  3. }

for more info about the implementation see see this atricle

Example :

  1. <androidx.recyclerview.widget.RecyclerView
  2. android:id="@+id/recyclerView"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
  7. tools:itemCount="3"
  8. tools:listitem="@layout/item_recipe" ></androidx.recyclerview.widget.RecyclerView>

1.now all you need is to call withSimpleAdapter() function on recyclerView then you could call anything from the holder like itemView as shown below:

  1. class MainActivity : AppCompatActivity() {
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContentView(R.layout.activity_main)
  5. val dummyData = listOf(
  6. Recipe("Chocolate", R.drawable.a),
  7. Recipe("French Fries", R.drawable.b),
  8. Recipe("milkshake", R.drawable.c),
  9. Recipe("Mushroom soup", R.drawable.g),
  10. Recipe("Cool juice", R.drawable.f),
  11. Recipe("Cool Watermelon drink", R.drawable.h)
  12. )
  13. //Without item clickListener.
  14. recyclerView.withSimpleAdapter(dummyData, R.layout.item_recipe) { data ->
  15. recipe_img.setImageResource(data.drawable)
  16. recipe_name.text = data.name
  17. }
  18. //Set onItemClickListener (Avoid object creation each time view onBindViewHolderCalled).
  19. recyclerView.withSimpleAdapter(dummyData, R.layout.item_recipe,
  20. onBindView = { data ->
  21. recipe_img.setImageResource(data.drawable)
  22. recipe_name.text = data.name
  23. },
  24. onItemClick = { position ->
  25. Toast.makeText(context, dummyData[position].name, Toast.LENGTH_LONG).show()
  26. }
  27. )
  28. //OR
  29. recyclerView.withSimpleAdapter(dummyData, R.layout.item_recipe) { data ->
  30. recipe_img.setImageResource(data.drawable)
  31. recipe_name.text = data.name
  32. //set onItemClickListener each time view get binned to recyclerView (Each time create onClickListener object)
  33. onItemClickListener { position->
  34. Toast.makeText(context, dummyData[position].name, Toast.LENGTH_LONG).show()
  35. }
  36. }
  37. }
  38. }
you’re free to make pull request and contribute, happy coding!