Skip to content

Latest commit

 

History

History
133 lines (112 loc) · 5.14 KB

README.md

File metadata and controls

133 lines (112 loc) · 5.14 KB

Simple RecyclerView Adapter

License API

Getting Started

This is a library that abstracts, and completely hide the Adapter and ViewHolder class creation part. It is general purpose object that can be customized to each case (item layout) by the time of creation. Similar to what is done on Listview with default adapters. Compatible with androidx.

Pre-requisites

This library can be implemented on any android project with minimum API 14. You must have a list and an item_layout for each item of the list

Adding to your project

Lets start by adding a corresponding repository in your root build.gradle file. (prefer below all other)

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

The next task is to add the dependency to your app build.gradle file.

	dependencies {
          ...
	        implementation 'com.github.horaciocome1:simple-recyclerview-adapter:0.1.3'
	}

Now you ready to go. Except that you should sync your project first.

Do not use with app compat or support design

As mention on IO18, android support libraries will not be updated anymore, that's the main reason ive moved to androidx and new material design libraries. That's why if you have any appcompat or design support library as dependency the build will fail. Because the androidx on these lib will conflict with android support on your app. I am wondering if it is necessary to do a separate lib for support appcompat. Email me if you thinking on that.

How to use

Call the default constructor passing as Datatype the class of your list objects.

recyclerView.adapter = SimpleRecyclerViewAdapter<DataType>().apply {
    itemLayout = R.layout.your_item_layout
    list = yourList
    setOnBindViewHolder { holder, dataType -> /* bind data here */ }
}

Examples

Lets have a look at some common binding examples

recyclerView.adapter = SimpleRecyclerViewAdapter<User>().apply {
    itemLayout = R.layout.item_user
    list = users
    setOnBindViewHolder { holder, user ->
        holder.findViewById<Textview>(R.id.name).text = user.name
        holder.findViewById<Textview>(R.id.age).text = user.age
        ImageLoader.with(context)
            .load(user.photo)
            .into(holder.findViewById<ImageView>(R.id.photo))
    }
}

Java

SimpleRecyclerViewAdapter<DataType> adapter = new SimpleRecyclerViewAdapter<>();
        adapter.setItemLayout(R.layout.your_item_layot);
        adapter.setList(yourList);
        adapter.setOnBindViewHolder(new Function2<SimpleRecyclerViewAdapter.ViewHolder, DataType, Unit>() {
            @Override
            public Unit invoke(SimpleRecyclerViewAdapter.ViewHolder viewHolder, DataType data) {
                // bind data here
            }
        });
        recyclerView.setAdapter(adapter);

No casting need. I suggest you to use Android Studio code completion, to implement onBindViewHolder().

Adding items to the list

adapter.addItem(item)

Update items in the list

adapter.setItem(item, position)

Removing items from the list

adapter.removeItem(position)

Restoring previously removed item to the list

adapter.restoreItem(item, position)

Notice that these calls might require public adapter object/variable

Troubleshooting

Naturally, the adapter needs an item_layout, an list and an onBindViewHolder() implementation to display the list on screen. NONE OF THIS ARE OPTIONAL, so not setting them may lead to runtime app crash.

// this is what you need to a void
recyclerView.adapter = SimpleRecyclerViewAdapter<DataType>()

"Build or synchronization failed!"

This is might be a dependency matter. Please reference to the part on the start where i talked about support libraries.

Licenses

Copyright 2018 Horácio Flávio Comé Júnior

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

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

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

How to contribute

I am open to suggestions of any kind. Please be expressive, so others so we'all will be able to understand each other! Report any issues, please!

Simple RecyclerView Utils

This is part of a serie of libraries that pretend to make recyclerview usage more easy. For a touch listener please see Simple RecyclerView Touch Listener