Search code examples
javaandroidanimationandroid-recyclerviewexpand

Using TransitionManager with ChangeBounds on expanding item in RecyclerView is making weird visuals when combined with layout_height: match_constraint


I am using Custom Adapter for RecyclerView and I needed to expand items when clicking on them. I followed this to achieve it using the method recommended by Google. The expanding is working fine, but the animation using TransitionManager.beginDelayedTransition(recyclerView); wasn't exactly what I was looking for. Also really weird stuff happened when I tried to scroll the RecyclerView during the animation which would mess things up.

So I went online to look for different way to animate the expanding. I found this:

            final ChangeBounds transition = new ChangeBounds();
            transition.setDuration(100L);
            TransitionManager.beginDelayedTransition(mRecyclerView,transition);

This was working really well since it would just animate the expanding item's row and then show the View.GONE views. No alpha transition etc involved. But then I had to change the RecyclerView's layout_height from match_parent to match_constraint because soft keyboard would often hide the expanded row (User can edit the item and keyboard pops ups).

What happens now: When I click to expand the RecyclerView's item, it Expands itself for really short time, then goes back to normal state and THEN starts the animation. It makes really weird blinking animation which is bad and ruins the smoothness. It wasn't doing this before the with the layout_height of RecyclerView set to match_parent, but now with match_constraint it is.

There is code I am using to expand it. Following the article linked above.

    final boolean isExpanded = position==mExpandedPosition;
    // Hidden Views
    holder.view1.setVisibility(isExpanded?View.VISIBLE:View.GONE);
    holder.view2.setVisibility(isExpanded?View.VISIBLE:View.GONE);
    holder.view3.setVisibility(isExpanded?View.VISIBLE:View.GONE);

    holder.itemView.setActivated(isExpanded);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mExpandedPosition = isExpanded? -1:holder.getAdapterPosition();
            // Animace
            final ChangeBounds transition = new ChangeBounds();
            transition.setDuration(100L);
          TransitionManager.beginDelayedTransition(mRecyclerView,transition);

            // Hiding keyboard if it's up
            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);


            notifyDataSetChanged();
        }
    });

Am I doing things right? It's possible I am missing something basic. I am pretty new to Android Development. Thank you!


Solution

  • I found a solution and maybe somebody will face the same problem as me. It was really dull mistake. I set layout_height to match_constraint , but not thelayout_width.

    So simply: also setting layout_width to match_constraint solved the problem.