Skip to content

Grid animation is not working with data binding #1

@dtunctuncer

Description

@dtunctuncer

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions