Giter Site home page Giter Site logo

gzeinnumer / recyclerviewadapterbuilder Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 1 MB

Stop Using Recyclerview Adapter

Home Page: https://github.com/gzeinnumer#my-library-list

Java 100.00%
java kotlin java-library kotlin-library recyclerview recyclerview-adapter recyclerview-multi-type recyclerview-item-animation recyclerview-item-decoration adapter

recyclerviewadapterbuilder's Introduction

RecyclerViewAdapterBuilder


Say Good Bye To Adapter .


Content List


Download

Add maven jitpack.io and dependencies in build.gradle (Project) :

// build.gradle project
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

// build.gradle app/module
dependencies {
  ...
  implementation 'com.github.gzeinnumer:RecyclerViewAdapterBuilder:version'
}

// enable view binding
android {

    ...

    //Android Studio Version Until 4
    viewBinding {
        enabled = true
    }

    //Android Studio Version 4 -> gradle version 6.1.1 -> android gradle plugin version 4.0.0
    buildFeatures{
        viewBinding = true
    }
}

Read More For Viewbinding Java & Kotlin


Feature List


Tech stack and 3rd library


Usage

Make Builder Single Type

Note rv_item.xml should start with LinearLayout as Parent/ViewGroup.

Java See Code

Kotlin

//setup data
val list: MutableList<MyModel> = ArrayList()
for (i in 0..9) {
    list.add(MyModel(i, "Data Ke " + (i + 1)))
}

//setup adapter
val adapter = AdapterBuilder<MyModel>(R.layout.rv_item)
    .setList(list)
    .onBind { adapter, holder, data,  position ->
        //adapter.notifyDataSetChanged()

        //R.layout.rv_item = RvItemBinding
        val bindingItem = RvItemBinding.bind(holder)
        bindingItem.btn.text = data.id.toString() + "_" + data.name
        bindingItem.btn.setOnClickListener { Toast.makeText(this@MainActivity, "tekan $position", Toast.LENGTH_SHORT).show() }
    }

//setup RecyclerView
binding.rv.adapter = adapter
binding.rv.layoutManager = LinearLayoutManager(applicationContext)
binding.rv.hasFixedSize()

//after 5 second, new data will appear
object : CountDownTimer(5000, 1000) {
    override fun onTick(millisUntilFinished: Long) {}
    override fun onFinish() {
        for (i in 10..100) {
            list.add(MyModel(i, "Data Ke " + (i + 1)))
        }
        //add new list
        adapter.setList(list)
    }
}.start()

Make Builder Multi Type

Note rv_item.xml & rv_item_genap.xml should start with LinearLayout as Parent/ViewGroup.

To enable Multi ViewType you can change AdapterCreator<MyModel> adapter = new AdapterBuilder<MyModel>(R.layout.rv_item) to AdapterCreatorMultiType<MyModel> adapter = new AdapterBuilderMultiType<MyModel>() and change body of function onBind.

Note : You can use all function seems like AdapterBuilder. example .setList(), .onFilter(),.setCustomNoItem(),.setAnimation(), .setDivider()

Java See Code

Kotlin

...

val adapter = AdapterBuilderMultiType<MyModel>()
    .setList(list)
    .onBind(object : BindViewHolderMultiType<MyModel> {

        private val TYPE_GENAP = 1
        private val TYPE_GANJIL = 0

        override fun getItemViewType(position: Int): TypeViewItem {
            return if (position % 2 == 0)
                TypeViewItem(TYPE_GENAP, R.layout.rv_item_genap)
            else
                TypeViewItem(TYPE_GANJIL, R.layout.rv_item)
        }

        override fun bind(adapter: AdapterCreatorMultiType<MyModel>, holder: View, data: MyModel, position: Int, viewType: Int) {
            //adapter.notifyDataSetChanged()
            if (viewType == TYPE_GENAP) {
                //R.layout.rv_item_genap = RvItemGenapBinding
                val bindingItem = RvItemGenapBinding.bind(holder)
                bindingItem.btn.text = data.id.toString() + "_" + data.name + "_Genap"
            } else if (viewType == TYPE_GANJIL) {
                //R.layout.rv_item = RvItemBinding
                val bindingItem = RvItemBinding.bind(holder)
                bindingItem.btn.text = data.id.toString() + "_" + data.name + "_Ganjil"
            }
        }
    })

...

Enable Filter

Use onFilter after onBind.

Java See Code

Kotlin

val adapter: AdapterCreator<MyModel> = AdapterBuilder<MyModel>(R.layout.rv_item)
    .onBind { ... }
    .onFilter { constraint, listFilter ->
        val fildteredList: MutableList<MyModel> = ArrayList()

        if (constraint.isNotEmpty()) {
            val filterPattern = constraint.toString().toLowerCase().trim { it <= ' ' }
            for (item in listFilter) {
                //filter by id
                if (item.id.toString().toLowerCase().contains(filterPattern)) {
                    fildteredList.add(item)
                }
                //filter by name
                if (item.name.toString().toLowerCase().contains(filterPattern)) {
                    fildteredList.add(item)
                }
            }
        }
        fildteredList
    }

//use filter on TextWatcher
binding.ed.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}

    override fun afterTextChanged(s: Editable) {
        //call the filter
        adapter.filter.filter(s)
    }
})

Here is sample code in AdapterRv extends RecyclerView.Adapter<> that you can use RecyclerViewSearchMultiItem and here is for simple TextWacher MyLibSimpleTextWatcher that you can use

Preview :

Full Code MainActivity.java & MyModel.java & activity_main.xml & rv_item.xml

If list size = 0 Your Custom Item View

Customize

  • You can customize Empty item Message or list size = 0 with

Java

new AdapterBuilder<MyModel>(R.layout.rv_item)
    .setCustomNoItem(R.layout.custom_empty_item)

Kotlin

AdapterBuilder<MyModel>(R.layout.rv_item)
    .setCustomNoItem(R.layout.custom_empty_item)

You can Bind Empty item View to

new AdapterBuilder<MyModel>(R.layout.rv_item)
    .setCustomNoItem(R.layout.custom_empty_item, new BindViewHolderEmpty() {
        @Override
        public void bind(View holder) {
            //R.layout.custom_empty_item -> CustomEmptyItemBinding
            CustomEmptyItemBinding itemBinding = CustomEmptyItemBinding.bind(holder);
            itemBinding.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Tekan", Toast.LENGTH_SHORT).show();
                }
            });
        }
    })
.setCustomNoItem(R.layout.custom_empty_item) { holder -> //rv_item = RvItemBinding
    val bindingItem = CustomEmptyItemBinding.bind(holder)
    bindingItem.img.setOnClickListener {
        Toast.makeText(this@MainActivity, "tekan", Toast.LENGTH_SHORT).show()
    }
}

Sample code custom_empty_item.xml

  • You can customize animation in recycler view with

Java

new AdapterBuilder<MyModel>(R.layout.rv_item)
    .setAnimation(R.anim.anim_two)

Kotlin

AdapterBuilder<MyModel>(R.layout.rv_item)
    .setAnimation(R.anim.anim_two)

here is animation that you can use RecyclerViewAnimation

  • Custom Divider

Java

AdapterCreator<MyModel> adapter = new AdapterBuilder<MyModel>(R.layout.rv_item)
    .setDivider(R.layout.custom_divider)

Kotlin

val adapter: AdapterCreator<MyModel> = BuildAdapter<MyModel>(R.layout.rv_item)
    .setDivider(R.layout.custom_divider)

Sample code custom_divider.xml


Example Code/App

Sample APP, just clone it Java & Kotlin

Sample Code And App


Version

  • 1.0.1
    • First Release
  • 1.1.0
    • Add Filter Function
  • 1.2.0
    • Bug Fixing
  • 1.3.1
    • Add Multi Type
  • 2.0.0
    • Support SDK 16
  • 2.0.1
    • Adapter CallBack
  • 2.2.0
    • No Item CallBack

Contribution

You can sent your constibution to branch open-pull.


Copyright 2021 M. Fadli Zein

recyclerviewadapterbuilder's People

Contributors

gzeinnumer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.