- Stack Overflow: patrick.elmquist
- Medium: @patrick.elmquist
- LinkedIn: in/patrickelmquist
- Monkeytype: pagge
patrick-elmquist / demo-recyclerviewenteranimation Goto Github PK
View Code? Open in Web Editor NEWDemo project for a blog post
Demo project for a blog post
Hello,
I found your tutorial on : https://proandroiddev.com/enter-animation-using-recyclerview-and-layoutanimation-part-2-grids-688829b1d29b
I tried implementing in an application where I have a grid recyclerview. However, no animation is executed when the application is started. I tried debugging and saw that
protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params,
int index, int count)
--Update--
The issue I had was that I did not call - scheduleLayoutAnimation().
You can watch this video
Here is my code and animation resources are the exactly same as yours
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = DataBindingUtil.inflate<FragmentMovieListBinding>(inflater, R.layout.fragment_movie_list, container, false)
initRecyclerView(binding)
val model = ViewModelProviders.of(this).get(MovieListViewModel::class.java)
model.listType = listType
binding.viewModel = model
getMovieList(binding)
return binding.root
}
private fun getMovieList(binding: FragmentMovieListBinding) {
binding.viewModel.getMovieList()
.subscribe({ movieResponse ->
movieResponse.results.let {
adapter.setMovieList(it)
val animation = AnimationUtils.loadLayoutAnimation(activity, R.anim.grid_layout_animation_from_bottom)
binding.listRecyclerView.layoutAnimation = animation
binding.listRecyclerView.adapter.notifyDataSetChanged()
binding.listRecyclerView.scheduleLayoutAnimation()
}
}, { throwable ->
Timber.e(throwable)
})
}
private fun initRecyclerView(binding: FragmentMovieListBinding) {
val manager = GridLayoutManager(activity, 3)
binding.listRecyclerView.layoutManager = manager
binding.listRecyclerView.adapter = adapter
}
class AdvancedRecyclerView : RecyclerView {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)
override fun attachLayoutAnimationParameters(child: View, params: ViewGroup.LayoutParams,
index: Int, count: Int) {
val layoutManager = layoutManager
if (adapter != null && layoutManager is GridLayoutManager) {
var animationParams: GridLayoutAnimationController.AnimationParameters? = null
if (params.layoutAnimationParameters == null) {
// If there are no animation parameters, create new once and attach them to
// the LayoutParams.
animationParams = GridLayoutAnimationController.AnimationParameters()
params.layoutAnimationParameters = animationParams
}
// Next we are updating the parameters
// Set the number of items in the RecyclerView and the index of this item
animationParams?.count = count
animationParams?.index = index
// Calculate the number of columns and rows in the grid
val columns = layoutManager.spanCount
animationParams?.columnsCount = columns
animationParams?.rowsCount = count / columns
// Calculate the column/row position in the grid
val invertedIndex = count - 1 - index
animationParams?.column = columns - 1 - invertedIndex % columns
animationParams?.row = animationParams!!.rowsCount - 1 - invertedIndex / columns
} else {
// Proceed as normal if using another type of LayoutManager
super.attachLayoutAnimationParameters(child, params, index, count)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="com.dtunctuncer.todomovielist.ui.discover.movielist.MovieListViewModel" />
</data>
<com.dtunctuncer.todomovielist.core.widgets.AdvancedRecyclerView
android:id="@+id/listRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/grid_layout_animation_from_bottom" />
</layout>
RecyclerView gets data from API call and when data gets populated it gets Animation as expected, but when the user scrolls to end and pagination logic calls for next page data I call runLayoutAnimation() when next page data is available but Animation gets executed on older data set which is already in recyclerview.
I want the Animation to execute only on the new data set.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.