Skip to content

Commit 7648915

Browse files
committed
demo
1 parent be3fa13 commit 7648915

18 files changed

+1044
-11
lines changed

app/build.gradle

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'kotlin-kapt'
25

36
android {
4-
compileSdkVersion 26
7+
compileSdkVersion parent.ext.compileSdkVersion
8+
buildToolsVersion parent.ext.buildToolsVersion
59
defaultConfig {
610
applicationId "org.cgsdream.demo"
7-
minSdkVersion 19
8-
targetSdkVersion 26
11+
minSdkVersion parent.ext.minSdkVersion
12+
targetSdkVersion parent.ext.targetSdkVersion
913
versionCode 1
1014
versionName "1.0"
1115
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -18,9 +22,26 @@ android {
1822
}
1923
}
2024

25+
configurations.all {
26+
resolutionStrategy {
27+
force "com.android.support:support-annotations:${supportVersion}"
28+
force "com.android.support:recyclerview-v7:$supportVersion"
29+
force "com.android.support:appcompat-v7:$supportVersion"
30+
force "com.android.support:design:$supportVersion"
31+
force "com.android.support:support-vector-drawable:$supportVersion"
32+
}
33+
}
34+
2135
dependencies {
2236
implementation fileTree(dir: 'libs', include: ['*.jar'])
23-
implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
37+
implementation "com.android.support:appcompat-v7:${supportVersion}"
38+
implementation "com.qmuiteam:qmui:${qmuiVersion}"
39+
40+
implementation "com.jakewharton:butterknife:${butterknifeVersion}"
41+
kapt "com.jakewharton:butterknife-compiler:${butterknifeVersion}"
42+
43+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
44+
2445
testImplementation 'junit:junit:4.12'
2546
androidTestImplementation 'com.android.support.test:runner:1.0.1'
2647
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@
77
android:label="@string/app_name"
88
android:roundIcon="@mipmap/ic_launcher_round"
99
android:supportsRtl="true"
10-
android:theme="@style/AppTheme"/>
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN"/>
14+
<category android:name="android.intent.category.LAUNCHER"/>
15+
</intent-filter>
16+
</activity>
17+
</application>
1118
</manifest>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.cgsdream.demo
2+
3+
/**
4+
* Created by cgspine on 2018/1/28.
5+
*/
6+
7+
interface Cloneable<T>{
8+
fun clone(): T
9+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.cgsdream.demo
2+
3+
import android.support.v7.util.DiffUtil
4+
import android.util.SparseArray
5+
6+
/**
7+
* Created by cgspine on 2018/1/26.
8+
*/
9+
10+
class DiffCallback<H: Cloneable<H>, T: Cloneable<T>>(private val oldList: List<Section<H, T>>, private val newList: List<Section<H, T>>) : DiffUtil.Callback() {
11+
12+
private val mOldSectionIndex: SparseArray<Int> = SparseArray()
13+
private val mOldItemIndex: SparseArray<Int> = SparseArray()
14+
15+
private val mNewSectionIndex: SparseArray<Int> = SparseArray()
16+
private val mNewItemIndex: SparseArray<Int> = SparseArray()
17+
18+
init {
19+
generateIndex(oldList, mOldSectionIndex, mOldItemIndex)
20+
generateIndex(newList, mNewSectionIndex, mNewItemIndex)
21+
22+
}
23+
24+
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
25+
26+
val oldSectionIndex = mOldSectionIndex[oldItemPosition]
27+
val oldItemIndex = mOldItemIndex[oldItemPosition]
28+
val oldModel = oldList[oldSectionIndex]
29+
30+
val newSectionIndex = mNewSectionIndex[newItemPosition]
31+
val newItemIndex = mNewItemIndex[newItemPosition]
32+
val newModel = newList[newSectionIndex]
33+
34+
if (oldModel.header != newModel.header) {
35+
return false
36+
}
37+
38+
if (oldItemIndex < 0 && oldItemIndex == newItemIndex) {
39+
return true
40+
}
41+
42+
if (oldItemIndex < 0 || newItemIndex < 0) {
43+
return false
44+
}
45+
return oldModel.list[oldItemIndex] == newModel.list[newItemIndex]
46+
}
47+
48+
override fun getOldListSize() = mOldSectionIndex.size()
49+
50+
override fun getNewListSize() = mNewSectionIndex.size()
51+
52+
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
53+
54+
val oldSectionIndex = mOldSectionIndex[oldItemPosition]
55+
val oldItemIndex = mOldItemIndex[oldItemPosition]
56+
val oldModel = oldList[oldSectionIndex]
57+
58+
val newSectionIndex = mNewSectionIndex[newItemPosition]
59+
val newModel = newList[newSectionIndex]
60+
61+
if (oldItemIndex == ITEM_INDEX_SECTION_HEADER) {
62+
return oldModel.isFold == newModel.isFold
63+
}
64+
65+
if (oldItemIndex == ITEM_INDEX_LOAD_BEFORE || oldItemIndex == ITEM_INDEX_LOAD_AFTER) {
66+
// load more 强制返回 false,这样可以通过 FolderAdapter.onViewAttachedToWindow 触发 load more
67+
return false
68+
}
69+
70+
return true
71+
}
72+
73+
companion object {
74+
val ITEM_INDEX_SECTION_HEADER = -1
75+
val ITEM_INDEX_LOAD_BEFORE = -2
76+
val ITEM_INDEX_LOAD_AFTER = -3
77+
78+
fun <H: Cloneable<H>, T: Cloneable<T>> generateIndex(list: List<Section<H, T>>,
79+
sectionIndex: SparseArray<Int>,
80+
itemIndex: SparseArray<Int>){
81+
sectionIndex.clear()
82+
itemIndex.clear()
83+
var i = 0
84+
list.forEachIndexed { index, it ->
85+
if (!it.isLocked) {
86+
sectionIndex.append(i, index)
87+
itemIndex.append(i, ITEM_INDEX_SECTION_HEADER)
88+
i++
89+
if (!it.isFold && it.count() > 0) {
90+
if (it.hasBefore) {
91+
sectionIndex.append(i, index)
92+
itemIndex.append(i, ITEM_INDEX_LOAD_BEFORE)
93+
i++
94+
}
95+
96+
for (j in 0 until it.count()) {
97+
sectionIndex.append(i, index)
98+
itemIndex.append(i, j)
99+
i++
100+
}
101+
102+
if (it.hasAfter) {
103+
sectionIndex.append(i, index)
104+
itemIndex.append(i, ITEM_INDEX_LOAD_AFTER)
105+
i++
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)