Skip to content

Commit

Permalink
Merge pull request #107 from AlanCheen/feature/animation
Browse files Browse the repository at this point in the history
Feature/animation
  • Loading branch information
AlanCheen authored Aug 29, 2022
2 parents 68f84b1 + b53b1c6 commit 7b29afb
Show file tree
Hide file tree
Showing 20 changed files with 375 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ dependencies {
// implementation 'me.yifeiyuan.flap:flap-annotations:2.0.0'
// implementation 'me.yifeiyuan.flap:flap:2.0.0'
// kapt 'me.yifeiyuan.flap:flap-compiler:2.0.0'
//http://facebook.github.io/shimmer-android/
// implementation 'com.facebook.shimmer:shimmer:0.5.0'

implementation 'jp.wasabeef:recyclerview-animators:4.0.2'
}
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
xmlns:tools="http://schemas.android.com/tools"
package="me.yifeiyuan.flapdev">


<uses-sdk tools:overrideLibrary="jp.wasabeef.recyclerview"/>

<application
android:name=".FlapApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
tools:ignore="GoogleAppIndexingWarning"
>

<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GitHubDemoFragment : BaseTestcaseFragment() {
dataList.add(SimpleTextModel("Java"))
dataList.add(SimpleTextModel("Kotlin"))

adapter.setData(dataList)
adapter.setDataAndNotify(dataList)
}

override fun createRefreshData(size: Int): MutableList<Any> {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/me/yifeiyuan/flapdev/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class MainActivity : AppCompatActivity() {
subtitle = "滑动删除&拖放"
replace(SwipeAndDragTestcase::class.java)
}
R.id.nav_animation -> {
subtitle = "动画"
replace(AnimationTestcase::class.java)
}
R.id.nav_github_demo -> {
subtitle = "GitHub Demo"
replace(GitHubDemoFragment::class.java)
Expand Down
107 changes: 107 additions & 0 deletions app/src/main/java/me/yifeiyuan/flapdev/testcases/AnimationTestcase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package me.yifeiyuan.flapdev.testcases

import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import jp.wasabeef.recyclerview.animators.ScaleInAnimator
import me.yifeiyuan.flap.animation.*
import me.yifeiyuan.flapdev.R
import me.yifeiyuan.flapdev.components.SimpleTextModel

/**
* 测试动画功能
*
* 更推荐使用 https://github.com/wasabeef/recyclerview-animators
* 除了 minSdk 指定 21 有点高外。
*
* Created by 程序亦非猿 on 2022/8/29.
*/
class AnimationTestcase : BaseTestcaseFragment() {

private val alphaInAdapterAnimation = AlphaInAdapterAnimation()
private val scaleInAdapterAnimation = ScaleInAdapterAnimation()
private val slideInRightAdapterAnimation = SlideInRightAdapterAnimation()
private val slideInLeftAdapterAnimation = SlideInLeftAdapterAnimation()
private val slideInBottomAdapterAnimation = SlideInBottomAdapterAnimation()

lateinit var currentAdapterAnimation: BaseAdapterAnimation

override fun onInit(view: View) {
super.onInit(view)

setHasOptionsMenu(true)

adapter.doOnPreload {
loadMoreData(20)
}

currentAdapterAnimation = slideInBottomAdapterAnimation
currentAdapterAnimation.attachToAdapter(adapter)

recyclerView.addItemDecoration(spaceItemDecoration)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.animation, menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.resetData -> {
resetData()
}
R.id.slideInLeft -> {
resetAdapterAnimation()
currentAdapterAnimation = slideInLeftAdapterAnimation
currentAdapterAnimation.attachToAdapter(adapter)
}
R.id.slideInRight -> {
resetAdapterAnimation()
currentAdapterAnimation = slideInRightAdapterAnimation
currentAdapterAnimation.attachToAdapter(adapter)
}
R.id.scaleIn -> {
resetAdapterAnimation()
currentAdapterAnimation = scaleInAdapterAnimation
currentAdapterAnimation.attachToAdapter(adapter)
}
R.id.alphaIn -> {
resetAdapterAnimation()
currentAdapterAnimation = alphaInAdapterAnimation
currentAdapterAnimation.attachToAdapter(adapter)
}
R.id.slideInBottom -> {
resetAdapterAnimation()
currentAdapterAnimation = slideInBottomAdapterAnimation
currentAdapterAnimation.attachToAdapter(adapter)
}

R.id.animators -> {
resetAdapterAnimation()
recyclerView.itemAnimator = ScaleInAnimator()
}
R.id.addOne -> {
val newOne = SimpleTextModel("New one")
adapter.insertDataAt(1, newOne)
}
R.id.removeOne -> {
adapter.removeDataAt(1)
}
}
return super.onOptionsItemSelected(item)
}

private fun resetData() {
adapter.setDataAndNotify(createRefreshData())
currentAdapterAnimation.reset()
}

private fun resetAdapterAnimation() {
currentAdapterAnimation.let {
it.reset()
adapter.unregisterAdapterHook(it)
}
recyclerView.itemAnimator = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ open class BaseTestcaseFragment : Fragment(), Scrollable {
lateinit var staggeredGridLayoutManager: FlapStaggeredGridLayoutManager
lateinit var currentLayoutManager: RecyclerView.LayoutManager

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(getLayoutId(), container, false)
}

override fun onAttach(context: Context) {
super.onAttach(context)
toast = Toast.makeText(context.applicationContext, "", Toast.LENGTH_SHORT)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(getLayoutId(), container, false)
}

open fun getLayoutId(): Int = R.layout.fragment_base_case

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand Down Expand Up @@ -153,7 +155,7 @@ open class BaseTestcaseFragment : Fragment(), Scrollable {
// FlapItemTouchHelper(adapter).attachToRecyclerView(recyclerView)

Handler().postDelayed({
adapter.setData(createRefreshData())
adapter.setDataAndNotify(createRefreshData())
swipeRefreshLayout.isRefreshing = false
}, getRefreshDelayedTime())
}
Expand Down Expand Up @@ -199,8 +201,6 @@ open class BaseTestcaseFragment : Fragment(), Scrollable {

open fun createAdapter() = FlapAdapter()

open fun getLayoutId(): Int = R.layout.fragment_base_case

open fun onRefresh() {
swipeRefreshLayout.postDelayed({
refreshData(20)
Expand All @@ -211,10 +211,10 @@ open class BaseTestcaseFragment : Fragment(), Scrollable {
open fun getRefreshDelayedTime() = 1000L

open fun refreshData(size: Int = 20) {
adapter.setData(createRefreshData(size))
adapter.setDataAndNotify(createRefreshData(size))
}

open fun createRefreshData(size: Int = 40): MutableList<Any> {
open fun createRefreshData(size: Int = 20): MutableList<Any> {
val list = ArrayList<Any>()
repeat(size) {
list.add(SimpleTextModel("初始数据 $it of $size"))
Expand All @@ -228,7 +228,7 @@ open class BaseTestcaseFragment : Fragment(), Scrollable {
repeat(size) {
list.add(SimpleTextModel("加载更多数据 $it of $size"))
}
adapter.appendData(list)
adapter.appendDataAndNotify(list)
}, 500)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class FlapDifferAdapterTestcase : BaseTestcaseFragment() {
repeat(size) {
list.add(TestDiffModel("content,$it of 20,more data", it, "this is desc :${it % 3}"))
}
adapter.appendData(list)
adapter.appendDataAndNotify(list)
}, 500)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class FlapRecyclerViewTestcase : BaseTestcaseFragment() {
true
}

setData(createRefreshData(30))
setDataAndNotify(createRefreshData(30))
}

linearSpaceItemDecoration = LinearSpaceItemDecoration(requireActivity().toPixel(6))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ class MultiTypeTestcase : BaseTestcaseFragment() {
when (item.itemId) {
R.id.emptyData -> {
skeletonHelper.hide()
adapter.setData(mutableListOf())
adapter.setDataAndNotify(mutableListOf())
}
R.id.resetData -> {
adapter.setData(mockMultiTypeModels())
adapter.setDataAndNotify(mockMultiTypeModels())
}
R.id.linear -> {
recyclerView.layoutManager = FlapLinearLayoutManager(requireActivity(), RecyclerView.VERTICAL, false)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/activity_main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
android:id="@+id/nav_dismiss"
android:icon="@drawable/ic_menu_camera"
android:title="Swipe And Drag" />
<item
android:id="@+id/nav_animation"
android:icon="@drawable/ic_menu_camera"
android:title="动画" />
<item
android:id="@+id/nav_item_decorations"
android:icon="@drawable/ic_menu_camera"
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/res/menu/animation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/resetData"
android:title="重置数据" />
<item
android:id="@+id/slideInRight"
android:title="SlideInRight" />
<item
android:id="@+id/slideInLeft"
android:title="SlideInLeft" />
<item
android:id="@+id/slideInBottom"
android:title="SlideInBottom" />
<item
android:id="@+id/alphaIn"
android:title="AlphaIn" />

<item
android:id="@+id/scaleIn"
android:title="ScaleIn" />
<item
android:id="@+id/animators"
android:title="使用 recyclerview-animators" />

<item
android:id="@+id/addOne"
android:title="增加一个" app:showAsAction="always"/>

<item
android:id="@+id/removeOne"
android:title="删除一个" app:showAsAction="always"/>
</menu>
28 changes: 26 additions & 2 deletions flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,29 @@ open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry, IAdapt
open fun setData(newDataList: MutableList<Any>) {
data.clear()
data.addAll(newDataList)
notifyDataSetChanged()
}

open fun setDataAndNotify(newDataList: MutableList<Any>, notifyAll: Boolean = false) {
data.clear()
data.addAll(newDataList)
if (notifyAll) {
notifyDataSetChanged()
} else {
notifyItemRangeChanged(0, newDataList.size)
}
}

open fun appendData(appendDataList: MutableList<Any>) {
data.addAll(appendDataList)
notifyDataSetChanged()
}

open fun appendDataAndNotify(appendDataList: MutableList<Any>, notifyAll: Boolean = false) {
data.addAll(appendDataList)
if (notifyAll) {
notifyDataSetChanged()
} else {
notifyItemRangeInserted(itemCount - appendDataList.size, appendDataList.size)
}
}

override fun getItemCount(): Int {
Expand Down Expand Up @@ -542,6 +559,13 @@ open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry, IAdapt
return serviceManager.getAdapterService(serviceName)
}

fun insertDataAt(position: Int, element: Any, notify: Boolean = true) {
this.data.add(position, element)
if (notify) {
notifyItemInserted(position)
}
}

fun removeDataAt(position: Int, notify: Boolean = true) {
data.removeAt(position)
if (notify) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.yifeiyuan.flap.animation

import android.animation.Animator
import android.animation.ObjectAnimator
import android.view.View
import me.yifeiyuan.flap.Component

/**
* Created by 程序亦非猿 on 2022/8/29.
*
* @since 3.0.7
*/
class AlphaInAdapterAnimation(var startAlpha: Float = 0f) : BaseAdapterAnimation() {

override fun createAnimator(view: View, component: Component<*>, data: Any, position: Int): Animator {
return ObjectAnimator.ofFloat(view, "alpha", startAlpha, 1f)
}

}
Loading

0 comments on commit 7b29afb

Please sign in to comment.