Talha Talha - 24 days ago 13
Android Question

Restore or Reset a Scene using Transition Framework

I was following a tutorial and want to do something of my own with it. The tutorial showed me how to animate. So I did that, now I want to restore the view(scene) that was before. First thing is to fade in the new views, which works fine, but restore/reset fails. It should behave like this:

enter image description here

I am setting

onClickListener
to menu strip (ImageView), and toggling
boolean
value to check if view is full or not. One issue I came across was that I had to reassign
onClickListener
when tansition ends, because new layout is inflating in transition. But when I click after three buttons are visible, it does not register my click (by debugging I checked).

here is code file:

public class HorizotalViewActivity extends AppCompatActivity implements Transition.TransitionListener, View.OnClickListener
{


boolean viewing = false;
ImageView goButton;
Scene scene, restoreScene;

@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_horizotal_view );

ViewGroup sceneView = (ViewGroup) findViewById( R.id.sceneView );


scene = Scene.getSceneForLayout( sceneView,
R.layout.transition_example2, this );

restoreScene = Scene.getSceneForLayout( sceneView,
R.layout.transition_example, this );


goButton = (ImageView) findViewById( R.id.goButton );


goButton.setOnClickListener( this );


}



@Override public void onTransitionStart( Transition transition )
{

}

@Override public void onTransitionEnd( Transition transition )
{
goButton = (ImageView) findViewById( R.id.goButton );


goButton.setOnClickListener( this );
}
//...

@Override public void onClick( View view )
{
if ( !viewing )
{
TransitionManager.go( scene );
viewing = true;
}
else
{


TransitionManager.go( restoreScene );

//goToScene( restoreScene );


viewing = false;
}
}
}


And here are xml files:

Activity layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sceneView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lgvalle.material_animations.HorizotalViewActivity">
<include layout="@layout/transition_example"/>

</LinearLayout>


Default layout (transition_example.xml):

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scene"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/goButton"
android:src="@drawable/menu"
android:layout_width="60dp"
android:layout_height="60dp" />

</LinearLayout>


Transition layout (transition_example2.xml):

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scene"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/goButton"
android:src="@drawable/menu"

android:layout_width="60dp"
android:layout_height="60dp" />

<ImageView
android:id="@+id/play"
android:layout_marginLeft="15dp"
android:src="@drawable/play"
android:layout_width="60dp"
android:layout_height="60dp" />
<ImageView
android:id="@+id/repeat"
android:layout_marginLeft="15dp"
android:src="@drawable/repeat"
android:layout_width="60dp"
android:layout_height="60dp" />
<ImageView
android:id="@+id/share"
android:layout_marginLeft="15dp"
android:src="@drawable/share"
android:layout_width="60dp"
android:layout_height="60dp" />


</LinearLayout>


I cannot see any issues with my logic, its self explaining,

1 - Check
boolean
if transition no.2 is already happened

1A - Do transition, update
boolean
.

2 - If transition happened

2A - Call previous scene, update
boolean
.

Now, I guess there is this problem with
onCickListener
, because I cant get to breakpoint in
onClickListener
once
TransitionManager.go()
is called, if it get register it might work as expected. There are no
logcat
errors/warnings.

Answer

I had the same with my project this is what i found out:

What happend when you use the Scene transition you have 2 View sets that will animate. lets say Set A (start layout) and Set B (End Layout). What you do in your code is that you set your clicklisteners on Set A. But after the Scene Transition set A is not on the screen anymore.

set your clicklisteners again after your transition will solve your problem. (when you use a CustomView that contents values you need to "copy" aswell.

To achieve this, use Scene.setEnterAction(Runnable) method,

scene.setEnterAction(new Runnable(){

@override
void run()
{

   goButton = (ImageView) scene.getSceneRoot.findViewById( R.id.goButton );


   goButton.setOnClickListener( this );

}

});