Skip to content

Commit 040a81a

Browse files
committed
kotlin version of recyclerview
kotlin version of recyclerview
1 parent 0b3e4de commit 040a81a

File tree

14 files changed

+449
-2
lines changed

14 files changed

+449
-2
lines changed

android-recyclerview/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
Android | Create a List with RecyclerView
22
=========================================
33

4+
> `app` written in Java
5+
> `app-kotlin` is the same as `app` but written in Kotlin
6+
7+
48
![android-recyclerview-app-layout](http://hmkcode.github.io/images/android/android-recyclerview-app_layout.png)
59

610

711
Creating a scrollable list of elements is a common pattern in mobile application. Using RecyclerView we can list a large data sets or frequently changing one. `RecyclerView` is an advanced and flexible version of ListView, addressing serveral issues with existing listing views. Here we will build a simple application with `RecyclerView`
812

913
## Overview
1014

11-
We will build a simple app that lists a hard-coded `Link` in a `RecyclerView`. To display items on `RecyclerView` you need to have the following components:
15+
We will build two versions of a simple app (one in Java and one in Kotlin) that displays a list of hard-coded instances of class `Link` in a `RecyclerView`. To display items on `RecyclerView` you need to the following:
1216

1317
- `RecyclerView` widget added to the activity layout.
1418
- A class extending `RecyclerView.Adapter`.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
android {
5+
compileSdkVersion 27
6+
7+
8+
9+
defaultConfig {
10+
applicationId "com.hmkcode"
11+
minSdkVersion 19
12+
targetSdkVersion 27
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
17+
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
27+
}
28+
29+
dependencies {
30+
implementation fileTree(dir: 'libs', include: ['*.jar'])
31+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
32+
implementation 'com.android.support:appcompat-v7:27.1.1'
33+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
34+
implementation 'com.android.support:recyclerview-v7:27.1.1'
35+
testImplementation 'junit:junit:4.12'
36+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
37+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.hmkcode">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".activities.MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.hmkcode.activities
2+
3+
import android.support.v7.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.support.v7.widget.LinearLayoutManager
6+
import android.support.v7.widget.RecyclerView
7+
import com.hmkcode.R
8+
import com.hmkcode.adapters.MyAdapter
9+
import com.hmkcode.model.Link
10+
import java.util.*
11+
12+
class MainActivity : AppCompatActivity() {
13+
14+
override fun onCreate(savedInstanceState: Bundle?) {
15+
super.onCreate(savedInstanceState)
16+
setContentView(R.layout.activity_main)
17+
18+
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
19+
20+
// layout manager
21+
val layoutManager:RecyclerView.LayoutManager = LinearLayoutManager(this)
22+
recyclerView.layoutManager = layoutManager;
23+
24+
// adapter
25+
val adapter:RecyclerView.Adapter<MyAdapter.MyViewHolder> = MyAdapter(getListData())
26+
recyclerView.adapter = adapter;
27+
}
28+
29+
//generate a list of Link
30+
private fun getListData(): List<Link> {
31+
val links = LinkedList<Link>()
32+
33+
var link = Link()
34+
link.icon = R.drawable.ic_link;
35+
link.title ="HMKCODE BLOG";
36+
link.url = "hmkcode.com";
37+
38+
links.add(link)
39+
40+
link = Link()
41+
link.icon = R.drawable.ic_twitter
42+
link.title = "@HMKCODE"
43+
link.url = "twitter.com/hmkcode"
44+
45+
links.add(link)
46+
47+
link = Link()
48+
link.icon = R.drawable.ic_github
49+
link.title = "HMKCODE"
50+
link.url = "github.com/hmkcode"
51+
52+
links.add(link)
53+
return links
54+
}
55+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.hmkcode.adapters
2+
3+
import android.support.v7.widget.RecyclerView
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.ImageView
8+
import android.widget.TextView
9+
import com.hmkcode.R
10+
import com.hmkcode.model.Link
11+
12+
class MyAdapter(private val links:List<Link>): RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
13+
14+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
15+
MyViewHolder {
16+
17+
val itemLayoutView:View = LayoutInflater.from(parent.context)
18+
.inflate(R.layout.item_layout, null)
19+
20+
val vh:MyViewHolder = MyViewHolder(itemLayoutView)
21+
return vh
22+
}
23+
24+
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
25+
holder.itemIcon.setImageResource(links[position].icon)
26+
holder.itemTitle.text = links[position].title
27+
holder.itemUrl.text = links[position].url
28+
}
29+
30+
override fun getItemCount(): Int {
31+
return links?.size ?: 0
32+
}
33+
34+
class MyViewHolder(itemLayoutView: View)
35+
: RecyclerView.ViewHolder(itemLayoutView) {
36+
37+
val itemTitle = itemLayoutView.findViewById<TextView>(R.id.item_title)
38+
val itemUrl = itemLayoutView.findViewById<TextView>(R.id.item_url)
39+
val itemIcon = itemLayoutView.findViewById<ImageView>(R.id.item_icon)
40+
41+
}
42+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hmkcode.model
2+
3+
class Link {
4+
5+
var icon: Int = 0
6+
var title: String? = null
7+
var url: String? = null
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<vector android:height="24dp" android:viewportHeight="1024"
2+
android:viewportWidth="1024" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
3+
<path android:fillColor="#1B1F23" android:fillType="evenOdd" android:pathData="M512,0C229.12,0 0,229.12 0,512C0,738.56 146.56,929.92 350.08,997.76C375.68,1002.24 385.28,986.88 385.28,973.44C385.28,961.28 384.64,920.96 384.64,878.08C256,901.76 222.72,846.72 212.48,817.92C206.72,803.2 181.76,757.76 160,745.6C142.08,736 116.48,712.32 159.36,711.68C199.68,711.04 228.48,748.8 238.08,764.16C284.16,841.6 357.76,819.84 387.2,806.4C391.68,773.12 405.12,750.72 419.84,737.92C305.92,725.12 186.88,680.96 186.88,485.12C186.88,429.44 206.72,383.36 239.36,347.52C234.24,334.72 216.32,282.24 244.48,211.84C244.48,211.84 287.36,198.4 385.28,264.32C426.24,252.8 469.76,247.04 513.28,247.04C556.8,247.04 600.32,252.8 641.28,264.32C739.2,197.76 782.08,211.84 782.08,211.84C810.24,282.24 792.32,334.72 787.2,347.52C819.84,383.36 839.68,428.8 839.68,485.12C839.68,681.6 720,725.12 606.08,737.92C624.64,753.92 640.64,784.64 640.64,832.64C640.64,901.12 640,956.16 640,973.44C640,986.88 649.6,1002.88 675.2,997.76C877.44,929.92 1024,737.92 1024,512C1024,229.12 794.88,0 512,0Z"/>
4+
</vector>
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillColor="#26A69A"
9+
android:pathData="M0,0h108v108h-108z" />
10+
<path
11+
android:fillColor="#00000000"
12+
android:pathData="M9,0L9,108"
13+
android:strokeColor="#33FFFFFF"
14+
android:strokeWidth="0.8" />
15+
<path
16+
android:fillColor="#00000000"
17+
android:pathData="M19,0L19,108"
18+
android:strokeColor="#33FFFFFF"
19+
android:strokeWidth="0.8" />
20+
<path
21+
android:fillColor="#00000000"
22+
android:pathData="M29,0L29,108"
23+
android:strokeColor="#33FFFFFF"
24+
android:strokeWidth="0.8" />
25+
<path
26+
android:fillColor="#00000000"
27+
android:pathData="M39,0L39,108"
28+
android:strokeColor="#33FFFFFF"
29+
android:strokeWidth="0.8" />
30+
<path
31+
android:fillColor="#00000000"
32+
android:pathData="M49,0L49,108"
33+
android:strokeColor="#33FFFFFF"
34+
android:strokeWidth="0.8" />
35+
<path
36+
android:fillColor="#00000000"
37+
android:pathData="M59,0L59,108"
38+
android:strokeColor="#33FFFFFF"
39+
android:strokeWidth="0.8" />
40+
<path
41+
android:fillColor="#00000000"
42+
android:pathData="M69,0L69,108"
43+
android:strokeColor="#33FFFFFF"
44+
android:strokeWidth="0.8" />
45+
<path
46+
android:fillColor="#00000000"
47+
android:pathData="M79,0L79,108"
48+
android:strokeColor="#33FFFFFF"
49+
android:strokeWidth="0.8" />
50+
<path
51+
android:fillColor="#00000000"
52+
android:pathData="M89,0L89,108"
53+
android:strokeColor="#33FFFFFF"
54+
android:strokeWidth="0.8" />
55+
<path
56+
android:fillColor="#00000000"
57+
android:pathData="M99,0L99,108"
58+
android:strokeColor="#33FFFFFF"
59+
android:strokeWidth="0.8" />
60+
<path
61+
android:fillColor="#00000000"
62+
android:pathData="M0,9L108,9"
63+
android:strokeColor="#33FFFFFF"
64+
android:strokeWidth="0.8" />
65+
<path
66+
android:fillColor="#00000000"
67+
android:pathData="M0,19L108,19"
68+
android:strokeColor="#33FFFFFF"
69+
android:strokeWidth="0.8" />
70+
<path
71+
android:fillColor="#00000000"
72+
android:pathData="M0,29L108,29"
73+
android:strokeColor="#33FFFFFF"
74+
android:strokeWidth="0.8" />
75+
<path
76+
android:fillColor="#00000000"
77+
android:pathData="M0,39L108,39"
78+
android:strokeColor="#33FFFFFF"
79+
android:strokeWidth="0.8" />
80+
<path
81+
android:fillColor="#00000000"
82+
android:pathData="M0,49L108,49"
83+
android:strokeColor="#33FFFFFF"
84+
android:strokeWidth="0.8" />
85+
<path
86+
android:fillColor="#00000000"
87+
android:pathData="M0,59L108,59"
88+
android:strokeColor="#33FFFFFF"
89+
android:strokeWidth="0.8" />
90+
<path
91+
android:fillColor="#00000000"
92+
android:pathData="M0,69L108,69"
93+
android:strokeColor="#33FFFFFF"
94+
android:strokeWidth="0.8" />
95+
<path
96+
android:fillColor="#00000000"
97+
android:pathData="M0,79L108,79"
98+
android:strokeColor="#33FFFFFF"
99+
android:strokeWidth="0.8" />
100+
<path
101+
android:fillColor="#00000000"
102+
android:pathData="M0,89L108,89"
103+
android:strokeColor="#33FFFFFF"
104+
android:strokeWidth="0.8" />
105+
<path
106+
android:fillColor="#00000000"
107+
android:pathData="M0,99L108,99"
108+
android:strokeColor="#33FFFFFF"
109+
android:strokeWidth="0.8" />
110+
<path
111+
android:fillColor="#00000000"
112+
android:pathData="M19,29L89,29"
113+
android:strokeColor="#33FFFFFF"
114+
android:strokeWidth="0.8" />
115+
<path
116+
android:fillColor="#00000000"
117+
android:pathData="M19,39L89,39"
118+
android:strokeColor="#33FFFFFF"
119+
android:strokeWidth="0.8" />
120+
<path
121+
android:fillColor="#00000000"
122+
android:pathData="M19,49L89,49"
123+
android:strokeColor="#33FFFFFF"
124+
android:strokeWidth="0.8" />
125+
<path
126+
android:fillColor="#00000000"
127+
android:pathData="M19,59L89,59"
128+
android:strokeColor="#33FFFFFF"
129+
android:strokeWidth="0.8" />
130+
<path
131+
android:fillColor="#00000000"
132+
android:pathData="M19,69L89,69"
133+
android:strokeColor="#33FFFFFF"
134+
android:strokeWidth="0.8" />
135+
<path
136+
android:fillColor="#00000000"
137+
android:pathData="M19,79L89,79"
138+
android:strokeColor="#33FFFFFF"
139+
android:strokeWidth="0.8" />
140+
<path
141+
android:fillColor="#00000000"
142+
android:pathData="M29,19L29,89"
143+
android:strokeColor="#33FFFFFF"
144+
android:strokeWidth="0.8" />
145+
<path
146+
android:fillColor="#00000000"
147+
android:pathData="M39,19L39,89"
148+
android:strokeColor="#33FFFFFF"
149+
android:strokeWidth="0.8" />
150+
<path
151+
android:fillColor="#00000000"
152+
android:pathData="M49,19L49,89"
153+
android:strokeColor="#33FFFFFF"
154+
android:strokeWidth="0.8" />
155+
<path
156+
android:fillColor="#00000000"
157+
android:pathData="M59,19L59,89"
158+
android:strokeColor="#33FFFFFF"
159+
android:strokeWidth="0.8" />
160+
<path
161+
android:fillColor="#00000000"
162+
android:pathData="M69,19L69,89"
163+
android:strokeColor="#33FFFFFF"
164+
android:strokeWidth="0.8" />
165+
<path
166+
android:fillColor="#00000000"
167+
android:pathData="M79,19L79,89"
168+
android:strokeColor="#33FFFFFF"
169+
android:strokeWidth="0.8" />
170+
</vector>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<vector android:height="24dp" android:viewportHeight="512"
2+
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
3+
<path android:fillColor="#010101" android:pathData="M459.654,233.373l-90.531,90.5c-49.969,50 -131.031,50 -181,0c-7.875,-7.844 -14.031,-16.688 -19.438,-25.813l42.063,-42.063c2,-2.016 4.469,-3.172 6.828,-4.531c2.906,9.938 7.984,19.344 15.797,27.156c24.953,24.969 65.563,24.938 90.5,0l90.5,-90.5c24.969,-24.969 24.969,-65.563 0,-90.516c-24.938,-24.953 -65.531,-24.953 -90.5,0l-32.188,32.219c-26.109,-10.172 -54.25,-12.906 -81.641,-8.891l68.578,-68.578c50,-49.984 131.031,-49.984 181.031,0C509.623,102.342 509.623,183.389 459.654,233.373zM220.326,382.186l-32.203,32.219c-24.953,24.938 -65.563,24.938 -90.516,0c-24.953,-24.969 -24.953,-65.563 0,-90.531l90.516,-90.5c24.969,-24.969 65.547,-24.969 90.5,0c7.797,7.797 12.875,17.203 15.813,27.125c2.375,-1.375 4.813,-2.5 6.813,-4.5l42.063,-42.047c-5.375,-9.156 -11.563,-17.969 -19.438,-25.828c-49.969,-49.984 -131.031,-49.984 -181.016,0l-90.5,90.5c-49.984,50 -49.984,131.031 0,181.031c49.984,49.969 131.031,49.969 181.016,0l68.594,-68.594C274.561,395.092 246.42,392.342 220.326,382.186z"/>
4+
</vector>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="600dp"
3+
android:height="500dp"
4+
android:viewportWidth="600"
5+
android:viewportHeight="500">
6+
<path
7+
android:pathData="m549.992,91.054c-18.395,8.366 -38.167,14.018 -58.914,16.56 21.175,-13.016 37.442,-33.626 45.102,-58.186 -19.822,12.054 -41.774,20.805 -65.142,25.521 -18.71,-20.441 -45.369,-33.21 -74.874,-33.21 -56.649,-0 -102.58,47.087 -102.58,105.168 0,8.243 0.907,16.271 2.656,23.968 -85.253,-4.386 -160.84,-46.257 -211.432,-109.886 -8.831,15.533 -13.889,33.6 -13.889,52.875 0,36.489 18.109,68.679 45.635,87.538 -16.815,-0.546 -32.633,-5.277 -46.462,-13.154 -0.01,0.438 -0.01,0.877 -0.01,1.323 0,50.956 35.36,93.461 82.285,103.125 -8.606,2.403 -17.669,3.69 -27.024,3.69 -6.61,-0 -13.036,-0.661 -19.3,-1.887C119.096,336.279 156.979,366.688 201.867,367.533 166.76,395.744 122.53,412.557 74.47,412.557 66.189,412.557 58.024,412.058 49.999,411.086 95.395,440.926 149.315,458.34 207.244,458.34 395.924,458.34 499.105,298.084 499.105,159.103c0,-4.56 -0.102,-9.094 -0.3,-13.605 20.045,-14.83 37.434,-33.351 51.187,-54.443"
8+
android:fillColor="#ff000000"
9+
android:strokeColor="#00000000"
10+
android:fillType="nonZero"
11+
android:fillAlpha="1"/>
12+
</vector>

0 commit comments

Comments
 (0)