Skip to content

Commit 72ab3a9

Browse files
committed
Add list view
1 parent 49d6253 commit 72ab3a9

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

list/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

list/build.gradle

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'maven-publish'
3+
apply plugin: 'kotlin-android'
4+
5+
group = "$groupId"
6+
version = "$versionName"
7+
8+
android {
9+
compileSdkVersion 30
10+
11+
defaultConfig {
12+
minSdkVersion 21
13+
targetSdkVersion 30
14+
15+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16+
}
17+
buildFeatures {
18+
viewBinding true
19+
}
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
compileOptions {
27+
sourceCompatibility JavaVersion.VERSION_1_8
28+
targetCompatibility JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = JavaVersion.VERSION_1_8.toString()
32+
useIR = true
33+
}
34+
afterEvaluate {
35+
publishing {
36+
publications {
37+
release(MavenPublication) {
38+
from components.release
39+
groupId = group
40+
artifactId = 'list'
41+
version = version
42+
}
43+
}
44+
}
45+
}
46+
}
47+
48+
dependencies {
49+
implementation project(':core')
50+
implementation 'androidx.core:core-ktx:1.6.0'
51+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
52+
testImplementation 'junit:junit:4.13.2'
53+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
54+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
55+
}

list/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest package="com.kylecorry.andromeda.list">
3+
4+
</manifest>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.kylecorry.andromeda.list
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import androidx.annotation.LayoutRes
7+
import androidx.recyclerview.widget.DividerItemDecoration
8+
import androidx.recyclerview.widget.LinearLayoutManager
9+
import androidx.recyclerview.widget.RecyclerView
10+
11+
class ListView<T>(
12+
private val view: RecyclerView,
13+
@LayoutRes private val itemLayoutId: Int,
14+
private val onViewBind: (View, T) -> Unit
15+
) {
16+
17+
private val adapter: Adapter
18+
private val layoutInflater: LayoutInflater
19+
private val layoutManager: LinearLayoutManager
20+
21+
init {
22+
layoutManager = LinearLayoutManager(view.context)
23+
view.layoutManager = layoutManager
24+
25+
layoutInflater = LayoutInflater.from(view.context)
26+
27+
adapter = Adapter(listOf())
28+
view.adapter = adapter
29+
}
30+
31+
fun setData(data: List<T>) {
32+
adapter.data = data
33+
}
34+
35+
fun addLineSeparator() {
36+
val dividerItemDecoration = DividerItemDecoration(
37+
view.context,
38+
layoutManager.orientation
39+
)
40+
view.addItemDecoration(dividerItemDecoration)
41+
}
42+
43+
fun scrollToPosition(position: Int, smooth: Boolean = true){
44+
if (smooth) {
45+
view.smoothScrollToPosition(position)
46+
} else {
47+
view.scrollToPosition(position)
48+
}
49+
}
50+
51+
inner class Adapter(mData: List<T>) : RecyclerView.Adapter<Holder>() {
52+
53+
var data: List<T> = mData
54+
set(value) {
55+
field = value
56+
notifyDataSetChanged()
57+
}
58+
59+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
60+
val view = layoutInflater.inflate(itemLayoutId, parent, false)
61+
return Holder(view)
62+
}
63+
64+
override fun getItemCount(): Int {
65+
return data.size
66+
}
67+
68+
override fun onBindViewHolder(holder: Holder, position: Int) {
69+
holder.bind(data[position])
70+
}
71+
72+
}
73+
74+
inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
75+
76+
fun bind(detail: T) {
77+
onViewBind(itemView, detail)
78+
}
79+
}
80+
81+
82+
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ include ':markdown'
2727
include ':signal'
2828
include ':fragments'
2929
include ':microphone'
30+
include ':list'

0 commit comments

Comments
 (0)