Giter VIP home page Giter VIP logo

horaciocome1 / simple-recyclerview-adapter Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 79 KB

General purpose object that can be costumized to each case (item layout) by the time of creation. Similar to what is done on Listview with default adapters. Compatible with androidx.

License: Apache License 2.0

Java 26.89% Kotlin 73.11%
recyclerview recyclerview-adapter android-support listview androidx appcompat adapter android android-library android-development mozambique africa kotlin kotlin-android kotlin-library kotlin-language

simple-recyclerview-adapter's Introduction

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

simple-recyclerview-adapter's People

Contributors

horaciocome1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.