Skip to content

Commit

Permalink
add data diff in refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
werbhelius committed Dec 19, 2017
1 parent 3ea14cb commit b7057fd
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 145 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

<activity android:name=".me.AboutMeActivity"/>

<activity android:name=".diff.DiffDataActivity"
android:theme="@style/AnimTheme"/>

<provider
android:name="com.werb.pickphotoview.provider.PickProvider"
android:authorities="${applicationId}.provider"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/werb/moretype/anim/AnimType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.werb.moretype.anim
/**
* Created by wanbo on 2017/7/15.
*/
data class AnimType(val title: String,
class AnimType(val title: String,
val type: String,
val typeIcon: String,
val thumb: String)
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ItemClickActivity : AppCompatActivity() {
toolbar.setNavigationOnClickListener { finish() }

item_click_list.layoutManager = LinearLayoutManager(this)
item_click_list.adapter = adapter
adapter.apply {
register(RegisterItem(R.layout.item_view_title, TitleViewHolder::class.java))
multiRegister(object : MultiLink<ItemClick>() {
Expand All @@ -45,7 +46,6 @@ class ItemClickActivity : AppCompatActivity() {
}
}
})
attachTo(item_click_list)
}

adapter.loadData(DataServer.getItemClickData())
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/com/werb/moretype/complete/CompleteActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import com.werb.moretype.anim.AnimViewHolder
import com.werb.moretype.data.DataServer
import com.werb.moretype.main.MainCard
import kotlinx.android.synthetic.main.activity_complete.*
import android.support.v7.util.DiffUtil
import android.support.v7.widget.DefaultItemAnimator





/**
* Created by wanbo on 2017/7/15.
Expand Down Expand Up @@ -47,8 +53,8 @@ class CompleteActivity : AppCompatActivity() {

//refresh
refresh.setOnRefreshListener {
adapter.refresh(1, AnimType("看图:这里是过去24小时发生的重要的事", "城市", "http://img.qdaily.com/category/icon_yellow_app/20160606004533uFozK3xI4CHZjfSM.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170715103111KHrZm3894Ys7uGIB.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"), true)
adapter.removeDataFromIndex(1)
adapter.loadData(DataServer.getCompleteData())
refresh.isRefreshing = false
}

Expand All @@ -67,6 +73,7 @@ class CompleteActivity : AppCompatActivity() {
if (any is Footer) {
adapter.removeData(any)
isShowFooter = false
adapter.loadData(DataServer.getCompleteData())
}
}, 500)
}
Expand Down
74 changes: 74 additions & 0 deletions app/src/main/java/com/werb/moretype/data/DataServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object DataServer {
mainCardList.add(MainCard("Animation", "Provides five types of Animation"))
mainCardList.add(MainCard("ItemClick", "Support onItemClick and onItemLongClick"))
mainCardList.add(MainCard("Complete Example", "A combination of elegant implementation layouts with refresh and loadMore"))
mainCardList.add(MainCard("Diff Data", "Diff data when refresh and loadMore"))
mainCardList.add(MainCard("About Me", "Developer & Designer"))
return mainCardList
}
Expand Down Expand Up @@ -361,4 +362,77 @@ object DataServer {
aboutMeDataList.add(MeMessage(MyApp.myApp.getString(R.string.like_words_two)))
return aboutMeDataList
}

fun getDiffData1(): List<Any> {
val diffList = arrayListOf<Any>()
diffList.add(Complete(
"wanbo",
"Developer & Designer",
"http://wx3.sinaimg.cn/mw690/b447c185ly8fdocwsavc6j20ku0kuaba.jpg",
"1500106878",
"new method to build data in RecyclerView with Kotlin!",
"https://boygeniusreport.files.wordpress.com/2014/12/ultimate-material-lollipop-collection-268.png?w=782",
"467",
"292"
))
diffList.add(Complete(
"wanbo",
"Curiosity",
"http://wx3.sinaimg.cn/mw690/b447c185ly8fdocwsavc6j20ku0kuaba.jpg",
"1500107414",
"The motivation that drives me to keep trying is my curiosity",
"https://img.scoop.it/f9ZSLthhGIc6nSw9gF7Z1Tl72eJkfbmt4t8yenImKBVvK0kTmF0xjctABnaLJIm9",
"467",
"292"
))
diffList.add(Complete(
"wanbo",
"I'm actually a musician",
"http://wx3.sinaimg.cn/mw690/b447c185ly8fdocwsavc6j20ku0kuaba.jpg",
"1500108765",
"Can you read this paragraph? My dream is to make music. My idol is jay Chou. I want to stand on the stage like him and sing for my favorite people",
"http://h.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=e778191ebd3eb1354492bfbf932e84e5/9213b07eca8065386ba591e596dda144ac3482fc.jpg",
"436",
"600"
))
diffList.add(AnimType("看图:这里是过去24小时发生的重要的事", "城市", "http://img.qdaily.com/category/icon_yellow_app/20160606004533uFozK3xI4CHZjfSM.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170715103111KHrZm3894Ys7uGIB.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"))
diffList.add(AnimType("盗版碟商贩,本周的时代预言家 | 好奇心辞典", "城市", "http://img.qdaily.com/category/icon_yellow_app/20160606004533uFozK3xI4CHZjfSM.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170714180204gKehPAICiFdx1Uwm.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"))
diffList.add(AnimType("这部纪录片美极了,而且居然没有用到特效", "娱乐", "http://img.qdaily.com/category/icon_yellow_app/20160606004531TgJK5XPy61qfS7rN.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170712164259UN4sqz2rogl0bWKY.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"))
diffList.add(AnimType("诺兰表示,《敦刻尔克》的叙事结构可能是他所有作品中最复杂的。?", "娱乐", "http://img.qdaily.com/category/icon_yellow_app/20160606004531TgJK5XPy61qfS7rN.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170714133142RYHh7qjSKcUPa9wp.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"))
return diffList
}

fun getDiffData2(): List<Any> {
val diffList = arrayListOf<Any>()
diffList.add(MainCard("Single Register", "Register one2one ViewType"))
diffList.add(MainCard("Multi Register", "Register one2more ViewType"))
diffList.add(MainCard("Animation", "Provides five types of Animation"))
diffList.add(MainCard("ItemClick", "Support onItemClick and onItemLongClick"))
diffList.add(MainCard("Complete Example", "A combination of elegant implementation layouts with refresh and loadMore"))
diffList.add(MainCard("About Me", "Developer & Designer"))
return diffList
}

fun getDiffData3(): List<Any> {
val diffList = arrayListOf<Any>()
diffList.add(AnimType("看图:这里是过去24小时发生的重要的事", "城市", "http://img.qdaily.com/category/icon_yellow_app/20160606004533uFozK3xI4CHZjfSM.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170715103111KHrZm3894Ys7uGIB.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"))
diffList.add(AnimType("盗版碟商贩,本周的时代预言家 | 好奇心辞典", "城市", "http://img.qdaily.com/category/icon_yellow_app/20160606004533uFozK3xI4CHZjfSM.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170714180204gKehPAICiFdx1Uwm.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"))
diffList.add(AnimType("这部纪录片美极了,而且居然没有用到特效", "娱乐", "http://img.qdaily.com/category/icon_yellow_app/20160606004531TgJK5XPy61qfS7rN.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170712164259UN4sqz2rogl0bWKY.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"))
diffList.add(AnimType("诺兰表示,《敦刻尔克》的叙事结构可能是他所有作品中最复杂的。?", "娱乐", "http://img.qdaily.com/category/icon_yellow_app/20160606004531TgJK5XPy61qfS7rN.png?imageMogr2/auto-orient/thumbnail/!160x160r/gravity/Center/crop/160x160/quality/85/ignore-error/1",
"http://img.qdaily.com/article/article_show/20170714133142RYHh7qjSKcUPa9wp.jpg?imageMogr2/auto-orient/thumbnail/!640x380r/gravity/Center/crop/640x380/quality/85/format/jpg/ignore-error/1"))
diffList.add(MainCard("Single Register", "Register one2one ViewType"))
diffList.add(MainCard("Multi Register", "Register one2more ViewType"))
diffList.add(MainCard("Animation", "Provides five types of Animation"))
diffList.add(MainCard("ItemClick", "Support onItemClick and onItemLongClick"))
diffList.add(MainCard("Complete Example", "A combination of elegant implementation layouts with refresh and loadMore"))
diffList.add(MainCard("About Me", "Developer & Designer"))
return diffList
}
}
93 changes: 93 additions & 0 deletions app/src/main/java/com/werb/moretype/diff/DiffDataActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.werb.moretype.diff

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.AdapterView
import com.werb.library.MoreAdapter
import com.werb.library.link.RegisterItem
import com.werb.moretype.R
import com.werb.moretype.TitleViewHolder
import com.werb.moretype.anim.AnimViewHolder
import com.werb.moretype.complete.CompleteViewHolder
import com.werb.moretype.data.DataServer
import com.werb.moretype.main.MainCard
import kotlinx.android.synthetic.main.activity_diff.*
import android.support.v7.widget.DefaultItemAnimator



/**
* Created by wanbo on 2017/12/19.
*/
class DiffDataActivity : AppCompatActivity() {

private val adapter = MoreAdapter()
private val newList = mutableListOf<Any>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_diff)

toolbar.setNavigationIcon(R.mipmap.ic_close_white_24dp)
toolbar.setNavigationOnClickListener { finish() }

diff_list.layoutManager = LinearLayoutManager(this)
adapter.apply {
userSoleRegister()
register(RegisterItem(R.layout.item_view_title, TitleViewHolder::class.java))
register(RegisterItem(R.layout.item_view_anim, AnimViewHolder::class.java))
register(RegisterItem(R.layout.item_view_complete, CompleteViewHolder::class.java))
attachTo(diff_list)
}

adapter.loadData(MainCard("Diff Data", "Diff data when refresh and loadMore"))
adapter.loadData(DataServer.getDiffData1())
(diff_list.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false

//refresh
refresh.setOnRefreshListener {
adapter.refresh(1, newList, DiffDataCallback::class.java)
refresh.isRefreshing = false
}

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

override fun onNothingSelected(p0: AdapterView<*>?) {

}

override fun onItemSelected(p0: AdapterView<*>?, view: View?, position: Int, p3: Long) {
when (position) {
0 -> {
newList.clear()
newList.addAll(DataServer.getDiffData1())
}
1 -> {
newList.clear()
newList.addAll(DataServer.getDiffData2())
}
2 -> {
newList.clear()
newList.addAll(DataServer.getDiffData3())
}
3 -> {
newList.clear()
newList.addAll(adapter.list.subList(1, adapter.itemCount))
}
}
}

}
}

companion object {
fun startActivity(activity: Activity) {
activity.startActivity(Intent(activity, DiffDataActivity::class.java))
}
}

}
44 changes: 44 additions & 0 deletions app/src/main/java/com/werb/moretype/diff/DiffDataCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.werb.moretype.diff

import com.werb.library.link.XDiffCallback
import com.werb.moretype.anim.AnimType
import com.werb.moretype.complete.Complete
import com.werb.moretype.main.MainCard

/**
* Created by wanbo on 2017/12/19.
*/
class DiffDataCallback(oldItem: List<Any>, newItem: List<Any>) : XDiffCallback(oldItem, newItem) {

override fun getOldListSize(): Int = oldItem.size

override fun getNewListSize(): Int = newItem.size

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val old = oldItem[oldItemPosition]
val new = newItem[newItemPosition]
if (old is Complete && new is Complete) {
return old.name == new.name
} else if (old is AnimType && new is AnimType) {
return old.title == new.title
} else if (old is MainCard && new is MainCard) {
return old.cardTitle == new.cardTitle
}

return false
}

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val old = oldItem[oldItemPosition]
val new = newItem[newItemPosition]
if (old is Complete && new is Complete) {
return old.desc == new.desc
} else if (old is AnimType && new is AnimType) {
return old.thumb == new.thumb
} else if (old is MainCard && new is MainCard) {
return old.cardDesc == new.cardDesc
}

return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.werb.library.MoreViewHolder
import com.werb.moretype.anim.AnimActivity
import com.werb.moretype.click.ItemClickActivity
import com.werb.moretype.complete.CompleteActivity
import com.werb.moretype.diff.DiffDataActivity
import com.werb.moretype.me.AboutMeActivity
import com.werb.moretype.multi.MultiRegisterActivity
import com.werb.moretype.single.SingleRegisterActivity
Expand Down Expand Up @@ -61,6 +62,9 @@ class MainCardViewHolder(containerView: View) : MoreViewHolder<MainCard>(contain
CompleteActivity.startActivity(containerView.context as Activity)
}
5 -> {
DiffDataActivity.startActivity(containerView.context as Activity)
}
6 -> {
AboutMeActivity.startActivity(containerView.context as Activity)
}
else -> {
Expand Down
50 changes: 50 additions & 0 deletions app/src/main/res/layout/activity_diff.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:elevation="0dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"/>

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:theme="@style/SpinnerStyle"
android:entries="@array/DiffData"/>

</RelativeLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/diff_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorTextLight" />
</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/layout/item_view_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginLeft="12dp"
android:layout_toLeftOf="@id/card_playLoad"
android:layout_alignParentLeft="true"
android:layout_marginRight="20dp"
android:layout_below="@+id/card_title"
android:textSize="12sp"
android:textStyle="bold"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
<item>Right</item>
<item>Bottom</item>
</string-array>

<string-array name="DiffData">
<item>Data1</item>
<item>Data2</item>
<item>Data3</item>
<item>Same</item>
</string-array>
</resources>

0 comments on commit b7057fd

Please sign in to comment.