Custom Adapter RecyclerView

Additional

Language
Kotlin
Version
2.0.3 (Oct 16, 2021)
Created
Jan 2, 2019
Updated
Oct 16, 2021
Owner
Farshid Roohi (FarshidRoohi)
Contributors
Farshid Roohi (FarshidRoohi)
farshad (farshadrezaee)
2
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

Custom Adapter RecyclerView

Very simple use android recyclerView adapter and endlessScrolled in android support library recyclerView

  • Clean uses
  • Ability add custom layout in progress pagination and default layout
  • Support linear, Grid, StaggeredGrid LayoutManger for endless
  • Manage Loading and Error Layout in The List Row
screenShot:

gradle :
  implementation 'androidx.recyclerview:recyclerview:$VERSION'
  implementation 'io.github.farshidroohi:customAdapterRecycleView:2.0.3'

Example

Sample Adapter


class MyAdapter : AdapterRecyclerView<String?>(R.layout.my_item, R.layout.progress_view, R.layout.item_error,R.id.btnTrayAgain) {

    override fun onBindView(viewHolder: ItemViewHolder, position: Int, context: Context, element: String?) {
        val itemView = viewHolder.itemView
        itemView.txt_title.setText("$element $position")
    }

}
    val myAdapter = MyAdapter()

           // onClick Error Button
           myAdapter.onRetryClicked = {
               myAdapter.loadingState()
               // fake request or load other items...
               recyclerView.postDelayed({
                   myAdapter.loadedState(tempItems)
               }, 2000)
           }

           // set Data in List
           myAdapter.loadedState(tempItems)

           recyclerView.layoutManager = GridLayoutManager(this@MainActivity, 2)
           recyclerView.adapter = myAdapter


           // set onLoad More Listener for Pagination
           recyclerView.onLoadMoreListener {
               if (myAdapter.mustLoad) {
                   myAdapter.loadingState()
                   // failed state
                   recyclerView.postDelayed({
                       myAdapter.failedState()
                   }, 2000)
               }
           }


           // Set onClick Item Listener
           recyclerView.onItemClickListener({ position ->
               if (position == myAdapter.itemCount - 1 && !myAdapter.mustLoad) {
                   return@onItemClickListener
               }
               Toast.makeText(applicationContext, "item click :  ${myAdapter.getItem(position)}$position", Toast.LENGTH_SHORT).show()
           }, { position ->
               if (position == myAdapter.itemCount - 1 && !myAdapter.mustLoad) {
                   return@onItemClickListener
               }
               Toast.makeText(applicationContext, "item long click :  ${myAdapter.getItem(position)}$position", Toast.LENGTH_SHORT).show()
           })

    
    // Generate Fake Data For List
    private val tempItems: List<String>
      get() {
        val items: MutableList<String> = ArrayList()
        for (i in 0..21) {
          items.add("item ")
        }
        return items
      }