Skip to content

Commit 69c11fe

Browse files
committed
RecyclerView-Support: Create
Signed-off-by: Fung <fython@163.com>
1 parent 6d4b190 commit 69c11fe

File tree

12 files changed

+236
-5
lines changed

12 files changed

+236
-5
lines changed

demo/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ dependencies {
3333

3434
compile project(':kotlinyan-common')
3535
compile project(':kotlinyan-picasso-support')
36+
compile project(':kotlinyan-recyclerview-support')
3637
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
3738
compile "org.jetbrains.anko:anko:0.10.1"
3839
}
3940
repositories {
4041
mavenCentral()
4142
}
43+
kotlin {
44+
experimental {
45+
coroutines "enable"
46+
}
47+
}

demo/src/main/kotlin/moe/feng/kotlinyan/MainActivity.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ class MainActivity : AppCompatActivity(), AndroidExtensions, ColorExtensions {
3030
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
3131
menuInflater.inflate(R.menu.menu_main, menu)
3232

33-
/**
34-
* Tint menu items easily
35-
*/
33+
// Tint menu items easily
3634
menu?.tintItemsColor(resources.color[R.color.grey_material_control])
3735

3836
return super.onCreateOptionsMenu(menu)
@@ -43,7 +41,8 @@ class MainActivity : AppCompatActivity(), AndroidExtensions, ColorExtensions {
4341
val items = arrayOf<Pair<String, Fragment>>(
4442
"ViewExtension" to ViewExtDemoFragment(),
4543
"NetworkExtension" to NetworkExtDemoFragment(),
46-
"PicassoExtension" to PicassoExtDemoFragment()
44+
"PicassoExtension" to PicassoExtDemoFragment(),
45+
"RecyclerView Extension" to RecyclerViewExtDemoFragment()
4746
)
4847

4948
override fun getPageTitle(position: Int): CharSequence = items[position].first
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package moe.feng.kotlinyan
2+
3+
import android.app.Fragment
4+
import android.os.Bundle
5+
import android.support.v4.widget.SwipeRefreshLayout
6+
import android.support.v7.widget.RecyclerView
7+
import android.view.LayoutInflater
8+
import android.view.View
9+
import android.view.ViewGroup
10+
import android.widget.TextView
11+
import moe.feng.kotlinyan.common.AndroidExtensions
12+
import moe.feng.kotlinyan.common.RecyclerViewExtensions
13+
import moe.feng.kotlinyan.common.ViewExtensions
14+
import org.jetbrains.anko.doAsync
15+
import org.jetbrains.anko.fragmentUiThread
16+
import org.jetbrains.anko.support.v4.onRefresh
17+
18+
class RecyclerViewExtDemoFragment : Fragment(), RecyclerViewExtensions, AndroidExtensions {
19+
20+
lateinit var refreshLayout : SwipeRefreshLayout
21+
lateinit var recyclerView : RecyclerView
22+
23+
private val adapter = SimpleAdapter(20)
24+
25+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View
26+
= inflater.inflate(R.layout.fragment_recyclerview_ext, container, false)
27+
28+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
29+
refreshLayout = view[R.id.refresh_layout] as SwipeRefreshLayout
30+
recyclerView = view[R.id.recycler_view] as RecyclerView
31+
32+
refreshLayout.setColorSchemeColors(resources.color[R.color.colorAccent])
33+
refreshLayout.onRefresh {
34+
doAsync {
35+
Thread.sleep(1000)
36+
adapter.count = 15
37+
fragmentUiThread {
38+
adapter.notifyDataSetChanged()
39+
refreshLayout.isRefreshing = false
40+
}
41+
}
42+
}
43+
recyclerView.adapter = adapter
44+
45+
// Load more callback
46+
recyclerView.onLoadMore {
47+
if (refreshLayout.isRefreshing) return@onLoadMore
48+
refreshLayout.isRefreshing = true
49+
doAsync {
50+
Thread.sleep(1000)
51+
adapter.count += 20
52+
fragmentUiThread {
53+
adapter.notifyDataSetChanged()
54+
refreshLayout.isRefreshing = false
55+
}
56+
}
57+
}
58+
}
59+
60+
private class SimpleAdapter(var count : Int) : RecyclerView.Adapter<SimpleAdapter.ViewHolder>(), AndroidExtensions {
61+
62+
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
63+
holder?.textView?.text = "Item %d".format(position)
64+
}
65+
66+
override fun getItemCount() = count
67+
68+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
69+
= ViewHolder(parent.inflateView(R.layout.simple_list_item_1))
70+
71+
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), ViewExtensions {
72+
73+
val textView by lazy { itemView[R.id.text] as TextView }
74+
75+
}
76+
77+
}
78+
79+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:id="@+id/refresh_layout"
8+
tools:context=".RecyclerViewExtDemoFragment">
9+
10+
<android.support.v7.widget.RecyclerView
11+
android:id="@+id/recycler_view"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
15+
16+
</android.support.v4.widget.SwipeRefreshLayout>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:clickable="true"
6+
android:foreground="?attr/selectableItemBackground">
7+
8+
<TextView
9+
android:id="@+id/text"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:layout_marginStart="16dp"
13+
android:layout_marginEnd="16dp"
14+
android:layout_marginTop="16dp"
15+
android:layout_marginBottom="16dp"/>
16+
17+
</FrameLayout>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
4+
android {
5+
compileSdkVersion TARGET_SDK_VERSION.toInteger()
6+
buildToolsVersion BUILD_TOOLS_VERSION
7+
8+
defaultConfig {
9+
minSdkVersion MIN_SDK_VERSION.toInteger()
10+
targetSdkVersion TARGET_SDK_VERSION.toInteger()
11+
versionCode VERSION_CODE.toInteger()
12+
versionName VERSION_NAME
13+
}
14+
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
22+
sourceSets {
23+
main.java.srcDirs += 'src/main/kotlin'
24+
}
25+
}
26+
27+
dependencies {
28+
compile fileTree(dir: 'libs', include: ['*.jar'])
29+
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
30+
compile 'com.android.support:recyclerview-v7:25.3.1'
31+
}
32+
repositories {
33+
mavenCentral()
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in G:\adt\sdk/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+
#}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<manifest package="moe.feng.kotlinyan.common.recyclerview">
2+
3+
</manifest>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package moe.feng.kotlinyan.common
2+
3+
import android.support.v7.widget.GridLayoutManager
4+
import android.support.v7.widget.LinearLayoutManager
5+
import android.support.v7.widget.RecyclerView
6+
import android.support.v7.widget.StaggeredGridLayoutManager
7+
8+
class OnLoadMoreListener(private val callback: OnLoadMoreListener.Callback) : RecyclerView.OnScrollListener() {
9+
10+
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
11+
if (dy > 0) {
12+
var visibleItemCount = 0
13+
var totalItemCount = 0
14+
var pastVisibleItems = 0
15+
16+
val lm = recyclerView!!.layoutManager
17+
if (lm is LinearLayoutManager) {
18+
visibleItemCount = lm.childCount
19+
totalItemCount = lm.itemCount
20+
pastVisibleItems = lm.findFirstVisibleItemPosition()
21+
} else if (lm is GridLayoutManager) {
22+
visibleItemCount = lm.childCount
23+
totalItemCount = lm.itemCount
24+
pastVisibleItems = lm.findFirstVisibleItemPosition()
25+
} else if (lm is StaggeredGridLayoutManager) {
26+
visibleItemCount = lm.childCount
27+
totalItemCount = lm.itemCount
28+
pastVisibleItems = lm.findFirstVisibleItemPositions(IntArray(lm.spanCount))[0]
29+
} else {
30+
throw IllegalStateException("Unsupported layout manager. Please commit issue on github."
31+
+ " " + "https://github.com/fython/Kotlinyan/issues")
32+
}
33+
34+
if (visibleItemCount + pastVisibleItems >= totalItemCount) {
35+
callback.onLoadMore()
36+
}
37+
}
38+
}
39+
40+
interface Callback {
41+
fun onLoadMore()
42+
}
43+
44+
}

0 commit comments

Comments
 (0)