🥄
One Library
  • 🤖onelib
  • 🏡Home
    • Overview
    • Setup Project
    • What's New
  • ☀️Fundamental
    • Basic Android
    • Data Flow
    • Dependency Injection
      • Hilt
      • Koin
  • 📿Layers
    • Data Layer
    • Domain Layer
    • Presentation Layer
  • 🪶Features
    • Views
      • Activity
      • Fragment
      • Bottom Sheet
      • Dialog
    • Adapters
      • Recycler Adapter
      • Filterable Adapter
      • Paging Adapter
      • Selectable Adapter
      • ViewPager Adapter
    • Firebase
      • One Firebase Auth
      • One Firebase Firestore
      • One Firebase Realtime
      • One Firebase Storage
    • Extension Functions
      • Any
      • Common
      • Context
      • Edittext
      • Image View
      • LiveData
      • OneMap
      • State Flow (Jetpack Compose)
      • View
    • Notification
    • One State View
    • Local Database
    • Permission
    • Validation
      • Passive Validator
      • Reactive Validator
      • Extension Validation
Powered by GitBook
On this page
  • For Example:
  • Recyclerview and event listener
  1. Features
  2. Adapters

Recycler Adapter

This recyclerview is a simplified version of the recyclerview that is often used to create lists, which only requires a few lines of code that are already prepared and can be customized.

For Example:

The first step is to create a view of the recyclerview using an xml file

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingHorizontal="16dp"
    android:paddingTop="16dp"
    android:background="?selectableItemBackground"
    xmlns:tools="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="This is a text" />

    <com.google.android.material.divider.MaterialDivider
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title"
        android:layout_marginTop="12dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Then, you can create an class of recyclerview and extend to BaseAsyncRecyclerAdapter. For example:

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.wahidabd.library.presentation.adapter.BaseAsyncRecyclerAdapter
import com.wahidabd.library.presentation.adapter.viewholder.BaseAsyncItemViewHolder
import com.wahidabd.onelibrary.databinding.ItemRecyclerBinding

class TestBaseAsyncRecyclerAdapter(
    private val context: Context
) : BaseAsyncRecyclerAdapter<String, TestBaseAsyncRecyclerAdapter.ViewHolder>(){

    override fun getViewBinding(parent: ViewGroup, viewType: Int): ViewBinding =
        ItemRecyclerBinding.inflate(LayoutInflater.from(context), parent, false)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ViewHolder(getViewBinding(parent, viewType))

    inner class ViewHolder(binding: ViewBinding) : BaseAsyncItemViewHolder<String>(binding = binding){
        override fun bind(data: String) {
            with(binding as ItemRecyclerBinding){
                tvTitle.text = data
            }
        }
    }

}

Recyclerview and event listener

If you need events to handle clicks and other things, you can use lambda expressions as parameters. For example:

class TestBaseAsyncRecyclerAdapter(
    private val context: Context,
    private val onItemClicked: (String) -> Unit
) : BaseAsyncRecyclerAdapter<String, TestBaseAsyncRecyclerAdapter.ViewHolder>(){

    override fun getViewBinding(parent: ViewGroup, viewType: Int): ViewBinding =
        ItemRecyclerBinding.inflate(LayoutInflater.from(context), parent, false)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ViewHolder(getViewBinding(parent, viewType))

    inner class ViewHolder(binding: ViewBinding) : BaseAsyncItemViewHolder<String>(binding = binding){
        override fun bind(data: String) {
            with(binding as ItemRecyclerBinding){
                tvTitle.text = data

                tvTitle.onClick {
                    onItemClicked.invoke(data)
                }
            }
        }
    }

}
// Activity/Fragment
private val asyncAdapter: TestBaseAsyncRecyclerAdapter by lazy {
    TestBaseAsyncRecyclerAdapter(
        context = this,
        onItemClicked = {}
    )
}

// and insert data 
override fun initObservers() {
    asyncAdapter.setData = Constant.dataList()
}

Last updated 1 year ago

After the adapter is successfully created, you can call the adapter on the Activity/Fragment to enter data according to your needs, to use it you can use kotlin . For example:

🪶
lazy