├── .github └── workflows │ └── android.yml ├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── cooltechworks │ │ └── sample │ │ ├── DemoActivity.kt │ │ ├── MainActivity.kt │ │ ├── adapters │ │ └── CardAdapter.kt │ │ ├── models │ │ └── ItemCard.kt │ │ ├── utils │ │ ├── BaseUtils.kt │ │ ├── DemoConfiguration.kt │ │ └── view │ │ │ └── CardPaddingItemDecoration.kt │ │ └── viewholders │ │ └── ItemHolder.kt │ └── res │ ├── drawable │ ├── bg_card.xml │ ├── bg_sharp_card.xml │ └── gradient_background.xml │ ├── layout │ ├── activity_grid.xml │ ├── activity_list.xml │ ├── activity_main.xml │ ├── activity_second_grid.xml │ ├── activity_second_list.xml │ ├── layout_demo.xml │ ├── layout_demo_grid.xml │ ├── layout_ecom_item.xml │ ├── layout_news_card.xml │ ├── layout_second_demo.xml │ ├── layout_second_demo_grid.xml │ └── layout_second_news_card.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshots ├── grid_demo.gif ├── list_demo.gif ├── second_grid_demo.gif └── second_list_demo.gif ├── settings.gradle └── shimmer ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── java └── com │ └── cooltechworks │ └── views │ └── shimmer │ ├── ShimmerAdapter.kt │ ├── ShimmerRecyclerView.kt │ └── ShimmerViewHolder.kt └── res ├── layout ├── layout_sample_view.xml └── viewholder_shimmer.xml └── values ├── attrs.xml └── colors.xml /.github/workflows/android.yml: -------------------------------------------------------------------------------- 1 | name: Android CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: set up JDK 1.8 17 | uses: actions/setup-java@v1 18 | with: 19 | java-version: 1.8 20 | - name: Build with Gradle 21 | run: ./gradlew build 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | .idea 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | 3 | sudo: false 4 | 5 | android: 6 | components: 7 | # Note that the tools section appears twice on purpose as it’s required to get the newest Android SDK tools. 8 | - tools 9 | - platform-tools 10 | - tools 11 | - build-tools-27.0.3 12 | - android-27 13 | - extra-android-m2repository 14 | 15 | before_install: 16 | - yes | sdkmanager "platforms;android-27" # https://github.com/travis-ci/travis-ci/issues/8651 17 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2017 Harish Sridharan 2 | 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-ShimmerRecyclerView-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/5058) 2 | [![Build Status](https://travis-ci.org/sharish/ShimmerRecyclerView.svg?branch=master)](https://travis-ci.org/sharish/ShimmerRecyclerView) 3 | 4 | # ShimmerRecyclerView 5 | 6 | 7 | Intro 8 | ------ 9 | 10 | A custom recycler view with shimmer views to indicate that views are loading. The recycler view has a built-in adapter to control the shimmer appearance and provide two methods - 11 | 12 | * showShimmerAdapter() - set up a demo adapter a predefined number of child demo views. 13 | * hideShimmerAdapter() - restores your adapter to show the actual child elements. 14 | 15 | 16 | Demo Screen 17 | ------ 18 | 19 | There are two kinds of shimmer animation which you can see here: 20 | 21 | 1. This type of shimmer effect uses the whole ViewHolder item to animate on. 22 | 23 | | List Demo | Grid Demo | 24 | | ---------------------------- | ----------------------------- | 25 | | | | 26 | 27 | 2. Here the shimmer effect only applied on for those views which background color is nontransparent. 28 | 29 | | List Demo | Grid Demo | 30 | | ---------------------------- | ----------------------------- | 31 | | | | 32 | 33 | 34 | ### Shimmer effect types 35 | 36 | 1. As you can see the first demo examples show that the whole ViewHolder item is animated. To achieve the desired effect, the children of the ShimmerLayout should have a nontransparent background. 37 | 2. You can achieve the second kind of shimmer effect by adding only one ViewGroup child to the ShimmerLayout with a transparent background. This ViewGroup will have the other views with nontransparent backgrounds on which the effect will be seen. 38 | 39 | You may wonder how can you add background to the root view of the ViewHolder, if you do not have direct access to the ShimmerLayout and the only child has a nontransparent background. The solution for this is to use the `shimmer_demo_view_holder_item_background` attribute. 40 | 41 | ### Attributes and Methods 42 | 43 | Following are the attributes and methods to initialise the demo views. 44 | 45 | | XML Attributes | Java Methods | Explanation | 46 | | ------------- | ------------ | ----------- | 47 | |```app:shimmer_demo_child_count``` | ```setDemoChildCount(int)``` | Integer value that sets the number of demo views should be present in shimmer adapter. | 48 | |```app:shimmer_demo_layout``` | ```setDemoLayoutReference(int)``` | Layout reference to your demo view. Define your my_demo_view.xml and refer the layout reference here. | 49 | |```app:shimmer_demo_layout_manager_type``` | ```setDemoLayoutManager(LayoutManagerType)``` | Layout manager of demo view. Can be one among linear_vertical or linear_horizontal or grid. | 50 | |```app:shimmer_demo_shimmer_color``` | ``` - ``` | Color reference or value. It can be used to change the color of the shimmer line. | 51 | |```app:shimmer_demo_angle``` | ``` - ``` | Integer value between 0 and 30 which can modify the angle of the shimmer line. The default value is zero. | 52 | |```app:shimmer_demo_mask_width``` | ``` setDemoShimmerMaskWidth(float) ``` | Float value between 0 and 1 which can modify the width of the shimmer line. The default value is 0.5. | 53 | |```app:shimmer_demo_view_holder_item_background``` | ``` - ``` | Color or an xml drawable for the ViewHolder background if you want to achieve the second type of shimmer effect. | 54 | |```app:shimmer_demo_reverse_animation``` | ``` - ``` | Defines whether the animation should be reversed. If it is true, then the animation starts from the right side of the View. Default value is false. | 55 | 56 | 57 | 58 | Usage 59 | -------- 60 | 61 | Define your xml as: 62 | 63 | ```xml 64 | 75 | 76 | ``` 77 | where ```@layout/layout_demo_grid``` refers to your sample layout that should be shown during loading spinner. Now on your activity onCreate, initialize the shimmer as below: 78 | 79 | ```java 80 | ShimmerRecyclerView shimmerRecycler = (ShimmerRecyclerView) findViewById(R.id.shimmer_recycler_view); 81 | shimmerRecycler.showShimmerAdapter(); 82 | ``` 83 | 84 | Adding to your project 85 | ------------------------ 86 | 87 | - Add the following configuration in your build.gradle file. 88 | 89 | ```gradle 90 | repositories { 91 | jcenter() 92 | maven { url "https://jitpack.io" } 93 | } 94 | 95 | dependencies { 96 | implementation 'com.github.sharish:ShimmerRecyclerView:v1.3' 97 | } 98 | ``` 99 | 100 | Developed By 101 | ------------ 102 | 103 | * Harish Sridharan - 104 | 105 | 106 | Used libraries 107 | ---------------- 108 | 109 | * ShimmerLayout: the library which achieves the shimmer effect in a memory efficient way. 110 | 111 | License 112 | -------- 113 | The repo is released under following licenses 114 | 115 | Apache License for ShimmerRecycler
116 | Apache License for ShimmerLayout 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android-extensions' 3 | apply plugin: 'kotlin-android' 4 | 5 | android { 6 | compileSdkVersion 27 7 | buildToolsVersion "27.0.3" 8 | defaultConfig { 9 | applicationId "com.cooltechworks.sample" 10 | minSdkVersion 14 11 | targetSdkVersion 27 12 | versionCode 2 13 | versionName "1.3" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | implementation 'com.android.support:appcompat-v7:27.1.1' 25 | implementation 'com.android.support:recyclerview-v7:27.1.1' 26 | implementation 'com.github.bumptech.glide:glide:4.5.0' 27 | implementation project(':shimmer') 28 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 29 | } 30 | repositories { 31 | mavenCentral() 32 | } 33 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/sharish/SDK_HOME/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 29 | 30 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/cooltechworks/sample/DemoActivity.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Harish Sridharan 3 | * 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | package com.cooltechworks.sample 21 | 22 | import android.os.Bundle 23 | import android.support.v7.app.AppCompatActivity 24 | import android.support.v7.widget.RecyclerView 25 | import com.cooltechworks.sample.adapters.CardAdapter 26 | import com.cooltechworks.sample.utils.BaseUtils 27 | import kotlinx.android.synthetic.main.activity_grid.* 28 | 29 | 30 | class DemoActivity : AppCompatActivity() { 31 | 32 | private lateinit var mAdapter: CardAdapter 33 | 34 | private val type: Int 35 | get() = intent.getIntExtra(EXTRA_TYPE, BaseUtils.TYPE_LIST) 36 | 37 | override fun onCreate(savedInstanceState: Bundle?) { 38 | super.onCreate(savedInstanceState) 39 | 40 | val type = type 41 | 42 | val layoutManager: RecyclerView.LayoutManager 43 | 44 | val demoConfiguration = BaseUtils.getDemoConfiguration(type, this) 45 | setTheme(demoConfiguration!!.styleResource) 46 | setContentView(demoConfiguration.layoutResource) 47 | layoutManager = demoConfiguration.layoutManager!! 48 | setTitle(demoConfiguration.titleResource) 49 | 50 | if (demoConfiguration.itemDecoration != null) { 51 | shimmer_recycler_view.addItemDecoration(demoConfiguration.itemDecoration) 52 | } 53 | 54 | mAdapter = CardAdapter() 55 | mAdapter.setType(type) 56 | 57 | shimmer_recycler_view.layoutManager = layoutManager 58 | shimmer_recycler_view.adapter = mAdapter 59 | shimmer_recycler_view.showShimmerAdapter() 60 | 61 | shimmer_recycler_view.postDelayed({ loadCards() }, 3000) 62 | } 63 | 64 | private fun loadCards() { 65 | val type = type 66 | 67 | mAdapter.setCards(BaseUtils.getCards(resources, type)) 68 | shimmer_recycler_view.hideShimmerAdapter() 69 | } 70 | 71 | companion object { 72 | const val EXTRA_TYPE = "type" 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/com/cooltechworks/sample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Harish Sridharan 3 | * 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | package com.cooltechworks.sample 21 | 22 | import android.content.Intent 23 | import android.os.Bundle 24 | import android.support.v7.app.AppCompatActivity 25 | import android.widget.Button 26 | import com.cooltechworks.sample.utils.BaseUtils 27 | import kotlinx.android.synthetic.main.activity_main.* 28 | 29 | class MainActivity : AppCompatActivity() { 30 | 31 | override fun onCreate(savedInstanceState: Bundle?) { 32 | super.onCreate(savedInstanceState) 33 | setContentView(R.layout.activity_main) 34 | 35 | createClickListener(list_demo_button, BaseUtils.TYPE_LIST) 36 | createClickListener(grid_demo_button, BaseUtils.TYPE_GRID) 37 | createClickListener(list_second_demo_button, BaseUtils.TYPE_SECOND_LIST) 38 | createClickListener(grid_second_demo_button, BaseUtils.TYPE_SECOND_GRID) 39 | } 40 | 41 | private fun createClickListener(button: Button, demoType: Int) { 42 | button.setOnClickListener { startDemo(demoType) } 43 | } 44 | 45 | private fun startDemo(demoType: Int) { 46 | val intent = Intent(this, DemoActivity::class.java) 47 | intent.putExtra(DemoActivity.EXTRA_TYPE, demoType) 48 | startActivity(intent) 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/cooltechworks/sample/adapters/CardAdapter.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Harish Sridharan 3 | * 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | package com.cooltechworks.sample.adapters 20 | 21 | import android.support.v7.widget.RecyclerView 22 | import android.view.ViewGroup 23 | import com.cooltechworks.sample.models.ItemCard 24 | import com.cooltechworks.sample.utils.BaseUtils 25 | import com.cooltechworks.sample.viewholders.ItemHolder 26 | import java.util.* 27 | 28 | class CardAdapter : RecyclerView.Adapter() { 29 | 30 | private var mCards: List = ArrayList() 31 | private var mType = BaseUtils.TYPE_LIST 32 | 33 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder { 34 | return ItemHolder.newInstance(parent, mType) 35 | } 36 | 37 | override fun onBindViewHolder(holder: ItemHolder, position: Int) { 38 | holder.bind(mCards[position]) 39 | } 40 | 41 | override fun getItemCount() = mCards.size 42 | 43 | 44 | fun setCards(cards: List?) { 45 | if (cards == null) { 46 | return 47 | } 48 | 49 | mCards = cards 50 | } 51 | 52 | fun setType(type: Int) { 53 | this.mType = type 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/cooltechworks/sample/models/ItemCard.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Harish Sridharan 3 | * 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | package com.cooltechworks.sample.models 20 | 21 | class ItemCard { 22 | 23 | var title: String? = null 24 | var description: String? = null 25 | var thumbnailUrl: String? = null 26 | var summaryText: String? = null 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/cooltechworks/sample/utils/BaseUtils.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Harish Sridharan 3 | * 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | package com.cooltechworks.sample.utils 20 | 21 | import android.content.Context 22 | import android.content.res.Resources 23 | import android.support.annotation.StringRes 24 | import android.support.v7.widget.GridLayoutManager 25 | import android.support.v7.widget.LinearLayoutManager 26 | 27 | import com.cooltechworks.sample.R 28 | import com.cooltechworks.sample.models.ItemCard 29 | import com.cooltechworks.sample.utils.view.CardPaddingItemDecoration 30 | 31 | import java.util.Arrays 32 | 33 | object BaseUtils { 34 | 35 | const val TYPE_LIST = 0 36 | const val TYPE_GRID = 1 37 | const val TYPE_SECOND_LIST = 2 38 | const val TYPE_SECOND_GRID = 3 39 | 40 | private fun getListCards(resources: Resources): List { 41 | val ndtvCard = createItemCard(resources, R.string.ndtv_titletext, R.string.ndtv_image_url, 42 | R.string.ndtv_subtext, R.string.ndtv_summarytext) 43 | 44 | val opCard = createItemCard(resources, R.string.op_titletext, R.string.op_image_url, 45 | R.string.op_subtext, R.string.op_summarytext) 46 | 47 | val gotCard = createItemCard(resources, R.string.got_titletext, R.string.got_image_url, 48 | R.string.got_subtext, R.string.got_summarytext) 49 | 50 | val jetCard = createItemCard(resources, R.string.jet_titletext, R.string.jet_image_url, 51 | R.string.jet_subtext, R.string.jet_summarytext) 52 | 53 | return Arrays.asList(ndtvCard, opCard, gotCard, jetCard) 54 | } 55 | 56 | private fun getGridCards(resources: Resources): List { 57 | val on7 = createItemCard(resources, R.string.on7_titletext, R.string.on7_image_url, 58 | R.string.on7_subtext, R.string.on7_summarytext) 59 | 60 | val note5 = createItemCard(resources, R.string.note5_titletext, R.string.note5_image_url, 61 | R.string.note5_subtext, R.string.note5_summarytext) 62 | 63 | val pixel = createItemCard(resources, R.string.pix_titletext, R.string.pix_image_url, 64 | R.string.pix_subtext, R.string.pix_summarytext) 65 | 66 | val iphone6 = createItemCard(resources, R.string.i6_titletext, R.string.i6_image_url, 67 | R.string.i6_subtext, R.string.i6_summarytext) 68 | 69 | val moto = createItemCard(resources, R.string.moto_titletext, R.string.moto_image_url, 70 | R.string.moto_subtext, R.string.moto_summarytext) 71 | 72 | val s7 = createItemCard(resources, R.string.s7_titletext, R.string.s7_image_url, 73 | R.string.s7_subtext, R.string.s7_summarytext) 74 | 75 | return Arrays.asList(on7, note5, pixel, iphone6, s7, moto) 76 | } 77 | 78 | fun getCards(resources: Resources, type: Int): List? { 79 | val itemCards: List? 80 | 81 | when (type) { 82 | TYPE_LIST, TYPE_SECOND_LIST -> itemCards = getListCards(resources) 83 | TYPE_GRID, TYPE_SECOND_GRID -> itemCards = getGridCards(resources) 84 | else -> itemCards = null 85 | } 86 | 87 | return itemCards 88 | } 89 | 90 | fun getDemoConfiguration(configurationType: Int, context: Context): DemoConfiguration? { 91 | val demoConfiguration: DemoConfiguration? 92 | 93 | when (configurationType) { 94 | TYPE_LIST -> { 95 | demoConfiguration = DemoConfiguration() 96 | demoConfiguration.styleResource = R.style.AppTheme 97 | demoConfiguration.layoutResource = R.layout.activity_list 98 | demoConfiguration.layoutManager = LinearLayoutManager(context) 99 | demoConfiguration.titleResource = R.string.ab_list_title 100 | } 101 | TYPE_GRID -> { 102 | demoConfiguration = DemoConfiguration() 103 | demoConfiguration.styleResource = R.style.AppThemeGrid 104 | demoConfiguration.layoutResource = R.layout.activity_grid 105 | demoConfiguration.layoutManager = GridLayoutManager(context, 2) 106 | demoConfiguration.titleResource = R.string.ab_grid_title 107 | } 108 | TYPE_SECOND_LIST -> { 109 | demoConfiguration = DemoConfiguration() 110 | demoConfiguration.styleResource = R.style.AppTheme 111 | demoConfiguration.layoutResource = R.layout.activity_second_list 112 | demoConfiguration.layoutManager = LinearLayoutManager(context) 113 | demoConfiguration.titleResource = R.string.ab_list_title 114 | demoConfiguration.itemDecoration = CardPaddingItemDecoration(context) 115 | } 116 | TYPE_SECOND_GRID -> { 117 | demoConfiguration = DemoConfiguration() 118 | demoConfiguration.styleResource = R.style.AppThemeGrid 119 | demoConfiguration.layoutResource = R.layout.activity_second_grid 120 | demoConfiguration.layoutManager = GridLayoutManager(context, 2) 121 | demoConfiguration.titleResource = R.string.ab_grid_title 122 | } 123 | else -> demoConfiguration = null 124 | } 125 | 126 | return demoConfiguration 127 | } 128 | 129 | private fun createItemCard(resources: Resources, @StringRes title: Int, @StringRes imageUrl: Int, 130 | @StringRes description: Int, @StringRes summary: Int): ItemCard { 131 | val itemCard = ItemCard() 132 | 133 | itemCard.title = resources.getString(title) 134 | itemCard.thumbnailUrl = resources.getString(imageUrl) 135 | itemCard.description = resources.getString(description) 136 | itemCard.summaryText = resources.getString(summary) 137 | 138 | return itemCard 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /app/src/main/java/com/cooltechworks/sample/utils/DemoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package com.cooltechworks.sample.utils 2 | 3 | 4 | import android.support.annotation.LayoutRes 5 | import android.support.annotation.StringRes 6 | import android.support.annotation.StyleRes 7 | import android.support.v7.widget.RecyclerView 8 | 9 | class DemoConfiguration { 10 | @StyleRes 11 | var styleResource: Int = 0 12 | 13 | @LayoutRes 14 | var layoutResource: Int = 0 15 | 16 | @StringRes 17 | var titleResource: Int = 0 18 | 19 | var layoutManager: RecyclerView.LayoutManager? = null 20 | 21 | var itemDecoration: RecyclerView.ItemDecoration? = null 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/cooltechworks/sample/utils/view/CardPaddingItemDecoration.kt: -------------------------------------------------------------------------------- 1 | package com.cooltechworks.sample.utils.view 2 | 3 | 4 | import android.content.Context 5 | import android.graphics.Rect 6 | import android.support.v7.widget.RecyclerView 7 | import android.util.TypedValue 8 | import android.view.View 9 | 10 | class CardPaddingItemDecoration(context: Context) : RecyclerView.ItemDecoration() { 11 | 12 | private val paddingBetweenItems = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8f, context.resources.displayMetrics).toInt() 13 | 14 | override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) { 15 | outRect.set(0, 0, 0, paddingBetweenItems) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/cooltechworks/sample/viewholders/ItemHolder.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Harish Sridharan 3 | * 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | package com.cooltechworks.sample.viewholders 20 | 21 | import android.support.v7.widget.RecyclerView 22 | import android.view.LayoutInflater 23 | import android.view.View 24 | import android.view.ViewGroup 25 | import android.widget.ImageView 26 | import android.widget.TextView 27 | 28 | import com.bumptech.glide.Glide 29 | import com.cooltechworks.sample.R 30 | import com.cooltechworks.sample.models.ItemCard 31 | 32 | import com.cooltechworks.sample.utils.BaseUtils.TYPE_GRID 33 | import com.cooltechworks.sample.utils.BaseUtils.TYPE_LIST 34 | import com.cooltechworks.sample.utils.BaseUtils.TYPE_SECOND_GRID 35 | import com.cooltechworks.sample.utils.BaseUtils.TYPE_SECOND_LIST 36 | 37 | class ItemHolder private constructor(itemView: View) : RecyclerView.ViewHolder(itemView) { 38 | 39 | private val mTitleView: TextView = itemView.findViewById(R.id.card_title) 40 | private val mDescView: TextView = itemView.findViewById(R.id.card_subtitle) 41 | private val mThumbnailView: ImageView = itemView.findViewById(R.id.card_image) 42 | private val mSummaryView: TextView = itemView.findViewById(R.id.card_summary) 43 | 44 | fun bind(card: ItemCard) { 45 | mTitleView.text = card.title 46 | mDescView.text = card.description 47 | mSummaryView.text = card.summaryText 48 | 49 | Glide.with(itemView.context).load(card.thumbnailUrl).into(mThumbnailView) 50 | } 51 | 52 | companion object { 53 | 54 | fun newInstance(container: ViewGroup, type: Int): ItemHolder { 55 | val root = LayoutInflater.from(container.context).inflate(getLayoutResourceId(type), 56 | container, false) 57 | 58 | return ItemHolder(root) 59 | } 60 | 61 | private fun getLayoutResourceId(type: Int): Int { 62 | return when (type) { 63 | TYPE_LIST -> R.layout.layout_news_card 64 | TYPE_SECOND_LIST -> R.layout.layout_second_news_card 65 | TYPE_GRID, TYPE_SECOND_GRID -> R.layout.layout_ecom_item 66 | else -> 0 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_card.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_sharp_card.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/gradient_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 22 | 23 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_grid.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 27 | 28 | 32 | 33 |