├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable │ │ │ │ ├── ic_loading_0.png │ │ │ │ ├── ic_loading_1.png │ │ │ │ ├── ic_loading_2.png │ │ │ │ ├── ic_loading_3.png │ │ │ │ ├── ic_loading_4.png │ │ │ │ ├── ic_loading_5.png │ │ │ │ ├── ic_loading_6.png │ │ │ │ ├── ic_loading_7.png │ │ │ │ ├── ic_loading_8.png │ │ │ │ ├── ic_loading_9.png │ │ │ │ ├── ic_loading_10.png │ │ │ │ ├── ic_loading_11.png │ │ │ │ ├── ic_loading_12.png │ │ │ │ ├── ic_loading_13.png │ │ │ │ ├── ic_loading_14.png │ │ │ │ ├── ic_loading_15.png │ │ │ │ ├── ic_loading_16.png │ │ │ │ ├── ic_loading_17.png │ │ │ │ ├── ic_loading_18.png │ │ │ │ ├── ic_loading_19.png │ │ │ │ ├── ic_loading_20.png │ │ │ │ ├── ic_loading_21.png │ │ │ │ ├── ic_loading_22.png │ │ │ │ ├── ic_loading_23.png │ │ │ │ ├── ic_loading_24.png │ │ │ │ ├── ic_loading_25.png │ │ │ │ ├── ic_loading_26.png │ │ │ │ ├── ic_loading_27.png │ │ │ │ ├── ic_loading_28.png │ │ │ │ ├── ic_loading_29.png │ │ │ │ ├── color_selector.xml │ │ │ │ ├── ic_simple.xml │ │ │ │ ├── ic_endless.xml │ │ │ │ ├── ic_loading.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── menu │ │ │ │ └── main_menu.xml │ │ │ ├── layout │ │ │ │ ├── loading_view.xml │ │ │ │ ├── fragment_simple.xml │ │ │ │ ├── fragment_endless.xml │ │ │ │ ├── item_view.xml │ │ │ │ └── activity_main.xml │ │ │ └── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── ciandt │ │ │ │ └── recyclerviewbinding │ │ │ │ ├── presentation │ │ │ │ ├── DrawableExtensions.kt │ │ │ │ ├── simple │ │ │ │ │ ├── SimpleViewModel.kt │ │ │ │ │ └── SimpleFragment.kt │ │ │ │ ├── Events.kt │ │ │ │ ├── main │ │ │ │ │ ├── ViewPagerAdapter.kt │ │ │ │ │ └── MainActivity.kt │ │ │ │ ├── endless │ │ │ │ │ ├── EndlessFragment.kt │ │ │ │ │ ├── EndlessViewModel.kt │ │ │ │ │ └── Endless.kt │ │ │ │ └── items │ │ │ │ │ └── ItemsAdapter.kt │ │ │ │ └── repository │ │ │ │ └── ItemsRepository.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── ciandt │ │ │ └── recyclerviewbinding │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── ciandt │ │ └── recyclerviewbinding │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── images ├── simple.gif └── endless.gif ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── LICENSE ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /images/simple.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/images/simple.gif -------------------------------------------------------------------------------- /images/endless.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/images/endless.gif -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_0.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_1.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_2.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_3.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_4.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_5.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_6.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_7.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_8.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_10.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_11.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_12.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_13.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_14.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_15.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_16.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_17.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_18.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_19.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_19.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_20.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_21.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_22.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_23.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_23.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_24.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_25.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_26.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_27.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_28.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_28.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading_29.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/drawable/ic_loading_29.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Recyclerview Binding 3 | Simple 4 | Endless 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jun 27 17:43:49 BRT 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/color_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/DrawableExtensions.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation 2 | 3 | import android.graphics.drawable.AnimationDrawable 4 | import android.graphics.drawable.Drawable 5 | 6 | fun Drawable.startAnimation() { 7 | (this as? AnimationDrawable)?.start() 8 | } -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #FFFFFF 7 | #f5303f9f 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_simple.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/ciandt/recyclerviewbinding/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/simple/SimpleViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation.simple 2 | 3 | import android.arch.lifecycle.LiveData 4 | import android.arch.lifecycle.MutableLiveData 5 | import android.arch.lifecycle.ViewModel 6 | import com.ciandt.recyclerviewbinding.repository.ItemsRepository 7 | 8 | class SimpleViewModel : ViewModel() { 9 | 10 | val items: LiveData> = 11 | MutableLiveData>().apply { value = ItemsRepository().getItemsPage() } 12 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/repository/ItemsRepository.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.repository 2 | 3 | class ItemsRepository { 4 | 5 | private var nextItem = 1 6 | 7 | fun getItemsPage(pageSize: Int = 20): List { 8 | 9 | val items = mutableListOf() 10 | val lastItem = nextItem + pageSize - 1 11 | 12 | for (i in nextItem..lastItem) { 13 | items.add("Item $i") 14 | } 15 | 16 | nextItem = lastItem + 1 17 | 18 | return items 19 | } 20 | } -------------------------------------------------------------------------------- /app/src/main/res/menu/main_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 10 | 16 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/Events.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation 2 | 3 | import android.arch.lifecycle.LifecycleOwner 4 | import android.arch.lifecycle.LiveData 5 | import android.arch.lifecycle.Observer 6 | 7 | open class Event(val data: T) { 8 | 9 | var hasBeenHandled = false 10 | protected set 11 | 12 | fun trigger(body: Event.() -> Unit) { 13 | if (!hasBeenHandled) { 14 | hasBeenHandled = true 15 | body(this) 16 | } 17 | } 18 | } 19 | 20 | class SimpleEvent : Event(null) 21 | 22 | fun > LiveData.subscribe(owner: LifecycleOwner, body: T.() -> Unit) { 23 | observe(owner, Observer { 24 | it?.trigger { body(it) } 25 | }) 26 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/loading_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 17 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/ciandt/recyclerviewbinding/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.ciandt.recyclerviewbinding", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_simple.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | 15 | 16 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_endless.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_endless.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | 15 | 16 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/main/ViewPagerAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation.main 2 | 3 | import android.support.v4.app.Fragment 4 | import android.support.v4.app.FragmentManager 5 | import android.support.v4.app.FragmentStatePagerAdapter 6 | import com.ciandt.recyclerviewbinding.presentation.endless.EndlessFragment 7 | import com.ciandt.recyclerviewbinding.presentation.simple.SimpleFragment 8 | 9 | class ViewPagerAdapter(manager: FragmentManager) : FragmentStatePagerAdapter(manager) { 10 | 11 | private val fragments = mutableListOf() 12 | 13 | override fun getItem(position: Int): Fragment { 14 | 15 | return if (fragments.indices.contains(position)) { 16 | fragments[position] 17 | } else { 18 | 19 | val frg = if (position == 0) { 20 | SimpleFragment() 21 | } else { 22 | EndlessFragment() 23 | } 24 | 25 | fragments.add(position, frg) 26 | 27 | frg 28 | } 29 | } 30 | 31 | override fun getCount() = 2 32 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 CI&T Mobile Team 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/main/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation.main 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import com.ciandt.recyclerviewbinding.R 6 | import kotlinx.android.synthetic.main.activity_main.* 7 | 8 | class MainActivity : AppCompatActivity() { 9 | 10 | override fun onCreate(savedInstanceState: Bundle?) { 11 | super.onCreate(savedInstanceState) 12 | 13 | setContentView(R.layout.activity_main) 14 | 15 | viewpager.adapter = ViewPagerAdapter(supportFragmentManager) 16 | 17 | bottomNavigation.setOnNavigationItemSelectedListener { 18 | 19 | val result = when(it.itemId) { 20 | R.id.action_simple -> { 21 | viewpager.setCurrentItem(0, false) 22 | true 23 | } 24 | R.id.action_endless -> { 25 | viewpager.setCurrentItem(1, false) 26 | true 27 | } 28 | else -> false 29 | } 30 | 31 | result 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/item_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 16 | 17 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 30 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-kapt' 6 | 7 | apply plugin: 'kotlin-android-extensions' 8 | 9 | android { 10 | compileSdkVersion 27 11 | defaultConfig { 12 | applicationId "com.ciandt.recyclerviewbinding" 13 | minSdkVersion 16 14 | targetSdkVersion 27 15 | versionCode 1 16 | versionName "1.0" 17 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | dataBinding { 26 | enabled = true 27 | } 28 | } 29 | 30 | dependencies { 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | 34 | //Android Support 35 | implementation "com.android.support:appcompat-v7:$android_support_version" 36 | implementation "com.android.support:recyclerview-v7:$android_support_version" 37 | implementation "com.android.support.constraint:constraint-layout:$constraint_version" 38 | implementation "com.android.support:design:$android_support_version" 39 | 40 | //Data Binding 41 | implementation 'com.android.support:support-v4:27.1.1' 42 | kapt "com.android.databinding:compiler:3.1.3" 43 | 44 | //Android Architecture Components 45 | implementation "android.arch.lifecycle:extensions:$arch_version" 46 | 47 | // Unit test 48 | testImplementation "junit:junit:$junit_version" 49 | 50 | // UI Test 51 | androidTestImplementation "com.android.support.test:runner:$android_support_test_version" 52 | androidTestImplementation "com.android.support.test.espresso:espresso-core:$espresso_version" 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/simple/SimpleFragment.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation.simple 2 | 3 | 4 | import android.arch.lifecycle.ViewModelProviders 5 | import android.databinding.DataBindingUtil 6 | import android.os.Bundle 7 | import android.support.v4.app.Fragment 8 | import android.support.v7.widget.DividerItemDecoration 9 | import android.support.v7.widget.LinearLayoutManager 10 | import android.view.LayoutInflater 11 | import android.view.View 12 | import android.view.ViewGroup 13 | import com.ciandt.recyclerviewbinding.R 14 | import com.ciandt.recyclerviewbinding.databinding.FragmentSimpleBinding 15 | import com.ciandt.recyclerviewbinding.presentation.items.ItemsAdapter 16 | import kotlinx.android.synthetic.main.fragment_simple.* 17 | 18 | class SimpleFragment : Fragment() { 19 | 20 | private lateinit var binding: FragmentSimpleBinding 21 | private lateinit var viewModel: SimpleViewModel 22 | 23 | override fun onCreateView( 24 | inflater: LayoutInflater, container: ViewGroup?, 25 | savedInstanceState: Bundle? 26 | ): View? { 27 | binding = DataBindingUtil.inflate(inflater, R.layout.fragment_simple, container, false) 28 | 29 | binding.setLifecycleOwner(this) 30 | 31 | return binding.root 32 | } 33 | 34 | override fun onActivityCreated(savedInstanceState: Bundle?) { 35 | super.onActivityCreated(savedInstanceState) 36 | 37 | viewModel = ViewModelProviders.of(this)[SimpleViewModel::class.java] 38 | 39 | val layoutManager = LinearLayoutManager(context) 40 | 41 | recyclerView.layoutManager = layoutManager 42 | recyclerView.hasFixedSize() 43 | recyclerView.adapter = ItemsAdapter() 44 | recyclerView.addItemDecoration(DividerItemDecoration(context, layoutManager.orientation)) 45 | 46 | binding.viewModel = viewModel 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/endless/EndlessFragment.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation.endless 2 | 3 | 4 | import android.arch.lifecycle.ViewModelProviders 5 | import android.databinding.DataBindingUtil 6 | import android.os.Bundle 7 | import android.support.v4.app.Fragment 8 | import android.support.v7.widget.DividerItemDecoration 9 | import android.support.v7.widget.LinearLayoutManager 10 | import android.view.LayoutInflater 11 | import android.view.View 12 | import android.view.ViewGroup 13 | 14 | import com.ciandt.recyclerviewbinding.R 15 | import com.ciandt.recyclerviewbinding.databinding.FragmentEndlessBinding 16 | import com.ciandt.recyclerviewbinding.presentation.items.ItemsAdapter 17 | import com.ciandt.recyclerviewbinding.presentation.subscribe 18 | import kotlinx.android.synthetic.main.fragment_endless.* 19 | 20 | class EndlessFragment : Fragment() { 21 | 22 | private lateinit var binding: FragmentEndlessBinding 23 | private lateinit var viewModel: EndlessViewModel 24 | 25 | override fun onCreateView( 26 | inflater: LayoutInflater, container: ViewGroup?, 27 | savedInstanceState: Bundle? 28 | ): View? { 29 | binding = DataBindingUtil.inflate(inflater, R.layout.fragment_endless, container, false) 30 | 31 | binding.setLifecycleOwner(this) 32 | 33 | return binding.root 34 | } 35 | 36 | override fun onActivityCreated(savedInstanceState: Bundle?) { 37 | super.onActivityCreated(savedInstanceState) 38 | 39 | viewModel = ViewModelProviders.of(this)[EndlessViewModel::class.java] 40 | 41 | val layoutManager = LinearLayoutManager(context) 42 | 43 | recyclerView.layoutManager = layoutManager 44 | recyclerView.hasFixedSize() 45 | recyclerView.adapter = ItemsAdapter() 46 | recyclerView.addItemDecoration(DividerItemDecoration(context, layoutManager.orientation)) 47 | 48 | recyclerView.endless { viewModel.fetchItems() } 49 | 50 | viewModel.addItems.subscribe(this) { 51 | recyclerView.adapter.notifyItemRangeInserted(data.positionStart, data.count) 52 | } 53 | viewModel.removeItem.subscribe(this) { 54 | recyclerView.adapter.notifyItemRemoved(data) 55 | } 56 | 57 | binding.viewModel = viewModel 58 | } 59 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/endless/EndlessViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation.endless 2 | 3 | import android.arch.lifecycle.LiveData 4 | import android.arch.lifecycle.MutableLiveData 5 | import android.arch.lifecycle.ViewModel 6 | import android.os.Handler 7 | import com.ciandt.recyclerviewbinding.presentation.Event 8 | import com.ciandt.recyclerviewbinding.repository.ItemsRepository 9 | 10 | data class Page(val positionStart: Int, val count: Int) 11 | 12 | class EndlessViewModel : ViewModel() { 13 | 14 | private val repository = ItemsRepository() 15 | private val list = mutableListOf() 16 | 17 | private val _items = MutableLiveData>().apply { value = list } 18 | private val _addItems = MutableLiveData>() 19 | private val _removeItem = MutableLiveData>() 20 | 21 | private val handler = Handler() 22 | 23 | private var loading = false 24 | 25 | private var delay = 0L 26 | 27 | val items: LiveData> 28 | get() = _items 29 | 30 | val addItems: LiveData> 31 | get() = _addItems 32 | 33 | val removeItem: LiveData> 34 | get() = _removeItem 35 | 36 | init { 37 | fetchItems() 38 | } 39 | 40 | fun fetchItems() { 41 | if (!loading) { 42 | loading = true 43 | 44 | showLoading() 45 | 46 | // Simulate delay 47 | 48 | handler.postDelayed({ 49 | 50 | val position = list.size 51 | val newItems = repository.getItemsPage() 52 | list.addAll(newItems) 53 | 54 | addItems(position, newItems.size) 55 | removeLoading(position-1) 56 | 57 | delay = if (delay == 0L) 3000 else 0 58 | loading = false 59 | }, delay) 60 | } 61 | } 62 | 63 | private fun showLoading() { 64 | list.add("loading") 65 | addItems(list.size - 1, 1) 66 | } 67 | 68 | private fun removeLoading(position: Int) { 69 | list.removeAt(position) 70 | removeItems(position) 71 | } 72 | 73 | private fun addItems(positionStart: Int, count: Int) { 74 | _addItems.value = Event(Page(positionStart, count)) 75 | } 76 | 77 | private fun removeItems(positionStart: Int) { 78 | _removeItem.value = Event(positionStart) 79 | } 80 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_loading.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/items/ItemsAdapter.kt: -------------------------------------------------------------------------------- 1 | @file:JvmName("ItemsAdapter") 2 | 3 | package com.ciandt.recyclerviewbinding.presentation.items 4 | 5 | import android.databinding.BindingAdapter 6 | import android.databinding.DataBindingUtil 7 | import android.support.v7.widget.RecyclerView 8 | import android.view.LayoutInflater 9 | import android.view.View 10 | import android.view.ViewGroup 11 | import com.ciandt.recyclerviewbinding.R 12 | import com.ciandt.recyclerviewbinding.databinding.ItemViewBinding 13 | import com.ciandt.recyclerviewbinding.presentation.startAnimation 14 | import kotlinx.android.synthetic.main.loading_view.view.* 15 | 16 | class ItemsAdapter : RecyclerView.Adapter() { 17 | 18 | private var items: List = emptyList() 19 | 20 | private val loading = 0 21 | private val item = 1 22 | 23 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { 24 | return if (viewType == loading) { 25 | LoadingViewHolder(parent) 26 | } else { 27 | ItemViewHolder(parent) 28 | } 29 | } 30 | 31 | override fun getItemCount() = items.size 32 | 33 | override fun onBindViewHolder(holder: ViewHolder, position: Int) { 34 | if (holder is ItemViewHolder && items.size > position) { 35 | holder.bind(items[position]) 36 | } 37 | } 38 | 39 | fun update(items: List) { 40 | this.items = items 41 | notifyDataSetChanged() 42 | } 43 | 44 | companion object { 45 | @JvmStatic 46 | @BindingAdapter("items") 47 | fun RecyclerView.bindItems(items: List) { 48 | val adapter = adapter as ItemsAdapter 49 | adapter.update(items) 50 | } 51 | } 52 | 53 | override fun getItemViewType(position: Int) = 54 | if (items[position] == "loading") loading else item 55 | 56 | abstract class ViewHolder(view: View) : RecyclerView.ViewHolder(view) 57 | 58 | class ItemViewHolder( 59 | private val parent: ViewGroup, 60 | private val binding: ItemViewBinding = DataBindingUtil.inflate( 61 | LayoutInflater.from(parent.context), 62 | R.layout.item_view, 63 | parent, 64 | false 65 | ) 66 | ) : ViewHolder(binding.root) { 67 | 68 | fun bind(item: String) { 69 | binding.item = item 70 | } 71 | } 72 | 73 | class LoadingViewHolder( 74 | private val parent: ViewGroup, 75 | view: View = LayoutInflater.from(parent.context).inflate( 76 | R.layout.loading_view, 77 | parent, 78 | false 79 | ) 80 | ) : 81 | ViewHolder(view) { 82 | 83 | init { 84 | view.imgLoading.post { view.imgLoading.drawable.startAnimation() } 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ciandt/recyclerviewbinding/presentation/endless/Endless.kt: -------------------------------------------------------------------------------- 1 | package com.ciandt.recyclerviewbinding.presentation.endless 2 | 3 | import android.support.v4.view.accessibility.AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED 4 | import android.support.v7.widget.LinearLayoutManager 5 | import android.support.v7.widget.RecyclerView 6 | import android.support.v7.widget.RecyclerViewAccessibilityDelegate 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import android.view.accessibility.AccessibilityEvent 10 | 11 | class EndlessScroll( 12 | private val recyclerView: RecyclerView, 13 | val visibleThreshold: Int = 10, 14 | private val loadMore: () -> Unit 15 | ) : 16 | RecyclerView.OnScrollListener() { 17 | 18 | private var previousTotal = 1 19 | private var loading = true 20 | 21 | class AccessibilityDelegate(recyclerView: RecyclerView, var changeRow: () -> Unit) : 22 | RecyclerViewAccessibilityDelegate(recyclerView) { 23 | 24 | override fun onRequestSendAccessibilityEvent( 25 | host: ViewGroup?, 26 | child: View?, 27 | event: AccessibilityEvent? 28 | ): Boolean { 29 | event?.let { 30 | if (it.eventType == TYPE_VIEW_ACCESSIBILITY_FOCUSED) { 31 | changeRow() 32 | } 33 | } 34 | 35 | return super.onRequestSendAccessibilityEvent(host, child, event) 36 | } 37 | } 38 | 39 | init { 40 | recyclerView.setAccessibilityDelegateCompat( 41 | AccessibilityDelegate( 42 | recyclerView, 43 | ::changeRowByAccessibility 44 | ) 45 | ) 46 | } 47 | 48 | private fun changeRowByAccessibility() { 49 | if (this.recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE) { 50 | return 51 | } 52 | 53 | loading() 54 | } 55 | 56 | override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) { 57 | super.onScrolled(recyclerView, dx, dy) 58 | if (this.recyclerView.scrollState == RecyclerView.SCROLL_STATE_IDLE) { 59 | return 60 | } 61 | 62 | loading() 63 | } 64 | 65 | private fun loading() { 66 | val visibleItemCount = recyclerView.childCount 67 | val totalItemCount = recyclerView.layoutManager.itemCount 68 | val firstVisibleItem = 69 | (recyclerView.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() 70 | 71 | if (loading) { 72 | if (totalItemCount > previousTotal) { 73 | loading = false 74 | previousTotal = totalItemCount 75 | } 76 | } 77 | 78 | if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 79 | loadMore() 80 | loading = true 81 | } 82 | } 83 | } 84 | 85 | fun RecyclerView.endless(visibleThreshold: Int = 10, loadMore: () -> Unit) { 86 | this.addOnScrollListener(EndlessScroll(this, visibleThreshold, loadMore)) 87 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RecyclerView + Data Binding Sample 2 | 3 | ## Simple sample 4 | 5 | ![](https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/master/images/simple.gif) 6 | 7 | ### Use LiveData directly with data binding 8 | 9 | ```xml 10 | 11 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | 24 | 25 | 30 | 31 | 32 | ``` 33 | 34 | ```kotlin 35 | class SimpleViewModel : ViewModel() { 36 | 37 | val items: LiveData> = 38 | MutableLiveData>().apply { value = ItemsRepository().getItemsPage() } 39 | } 40 | ``` 41 | 42 | ### Simple RecyclerView Setup 43 | 44 | ```kotlin 45 | class SimpleFragment : Fragment() { 46 | 47 | ... 48 | 49 | override fun onActivityCreated(savedInstanceState: Bundle?) { 50 | super.onActivityCreated(savedInstanceState) 51 | 52 | viewModel = ViewModelProviders.of(this)[SimpleViewModel::class.java] 53 | 54 | val layoutManager = LinearLayoutManager(context) 55 | 56 | recyclerView.layoutManager = layoutManager 57 | recyclerView.hasFixedSize() 58 | recyclerView.adapter = ItemsAdapter() 59 | recyclerView.addItemDecoration(DividerItemDecoration(context, layoutManager.orientation)) 60 | 61 | binding.viewModel = viewModel 62 | } 63 | 64 | } 65 | ``` 66 | 67 | ## Endless Scroll sample (accessibility support) 68 | 69 | ![](https://raw.githubusercontent.com/ciandt-mobile/android-recyclerview-binding/master/images/endless.gif) 70 | 71 | ```kotlin 72 | class EndlessFragment : Fragment() { 73 | 74 | ... 75 | 76 | override fun onActivityCreated(savedInstanceState: Bundle?) { 77 | super.onActivityCreated(savedInstanceState) 78 | 79 | viewModel = ViewModelProviders.of(this)[EndlessViewModel::class.java] 80 | 81 | val layoutManager = LinearLayoutManager(context) 82 | 83 | recyclerView.layoutManager = layoutManager 84 | recyclerView.hasFixedSize() 85 | recyclerView.adapter = ItemsAdapter() 86 | recyclerView.addItemDecoration(DividerItemDecoration(context, layoutManager.orientation)) 87 | 88 | // USE EXTENSION TO SIMPLIFY ENDLESS SETUP 89 | recyclerView.endless { viewModel.fetchItems() } 90 | 91 | // USE EVENT WRAPPER TO NOTIFY CHANGES TO ADAPTER 92 | //https://bit.ly/2NeeTMP [LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)] 93 | viewModel.updateList.subscribe(this) { 94 | recyclerView.adapter.notifyDataSetChanged() 95 | } 96 | 97 | binding.viewModel = viewModel 98 | } 99 | } 100 | ``` 101 | 102 | ``` 103 | MIT License 104 | 105 | Copyright (c) 2018 CI&T Mobile Team 106 | 107 | Permission is hereby granted, free of charge, to any person obtaining a copy 108 | of this software and associated documentation files (the "Software"), to deal 109 | in the Software without restriction, including without limitation the rights 110 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 111 | copies of the Software, and to permit persons to whom the Software is 112 | furnished to do so, subject to the following conditions: 113 | 114 | The above copyright notice and this permission notice shall be included in all 115 | copies or substantial portions of the Software. 116 | 117 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 118 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 119 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 120 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 121 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 122 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 123 | SOFTWARE. 124 | ``` 125 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | --------------------------------------------------------------------------------