Coder Social home page Coder Social logo

seongl / simplerecyclerview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sjjeong/simplerecyclerview

0.0 0.0 0.0 117 KB

Do not create RecyclerView.Adapter and RecyclerView.ViewHolder anymore.

License: Apache License 2.0

Kotlin 100.00%

simplerecyclerview's Introduction

SimpleRecyclerView

SimpleRecyclerView aims to help produce an easily usable implementation of a RecyclerView.Adapter and RecyclerView.ViewHolder

Dependency

Download

Then, add the library to your module build.gradle

dependencies {
    implementation 'com.dino.library:simplerecyclerview:x.y.z'
}

Enable DataBinding

SimpleRecyclerView use DataBinding

To configure your app to use data binding, add the dataBinding element to your build.gradle file in the app module (not root build.gradle file), as shown in the following example:

android {
    ...
    dataBinding {
        enabled = true
    }
}

Usage

There is a sample. Sample include architectures pattern such as mvp and mvvm.

item_layout.xml

<data>
    <variable
        name="item" <- very important
        type="com.your.package.YourItem"/>
</data>

<TextView
    android:text="@{item.title}"/>
<TextView
    android:text="@{item.content}"/>

if your item name of item_layout.xml is not "item", can't bind item in ViewHolder

RecyclerView.Adapter

<data>
    <import type="java.util.List" />
    <import type="com.your.package.YourItem" />
    <variable
        name="items"
        type="List&lt;YourItem>" />
</data>

...

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    bind:dino_itemLayout="@{@layout/item_layout}"
    bind:dino_items="@{items}" />

RecyclerView.SimpleAdapter

<data>
    <import type="java.util.List" />
    <import type="com.your.package.YourItem" />
    <variable
        name="items"
        type="List&lt;YourItem>" />
    <variable
        name="diffCallback"
        type="androidx.recyclerview.widget.DiffUtil.ItemCallback&lt;Object>" />
</data>

...

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    bind:dino_diffCallback="@{diffCallback}"
    bind:dino_itemLayout="@{@layout/item_layout}"
    bind:dino_items="@{items}" />

Don't set item, just set adapter

Just set null value in bind:items attribute.

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    bind:dino_itemLayout="@{@layout/item_layout}"
    bind:dino_items="@{null}" />

And item set programmatically(Not Recommended)

kotlin

recyclerView.setItems(items)

java

RecyclerViewExtKt.setItems(recyclerView, items)

This method has a timing issue. If you call a setItems function in (onCreate, onStart, onResume), the set of Adapters in the RecyclerView is called afterwards.

MVVM Sample

1. create item_layout.xml

CustomModel is enum class

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="item"
            type="com.dino.simplerecyclerview.model.CustomModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{item.title}" />

    </LinearLayout>
</layout>

2. create CustomViewModel

class CustomViewModel : ViewModel() {

    val customModelItems = ObservableField<List<CustomModel>>()

    init {
        createNewItem()
    }

    fun createNewItem() {
        customModelItems.set(CustomModel.values().toList())
    }
}

3. set list item in activity.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="vm"
            type="com.dino.simplerecyclerview.ui.CustomViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            bind:dino_itemLayout="@{@layout/item_layout}"
            bind:dino_items="@{vm.customModelItems}" />

    </LinearLayout>
</layout>

License

Copyright 2019 Dino

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.

simplerecyclerview's People

Contributors

sjjeong 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.