onClick does not work after transition
Asked Answered
B

2

9

I am trying to learn simple TransitionManager work, but I'm stuck little bit...

My goal is make changing between two scenes by image clicking. But after first image click and first scene change, i can not catch image click...

here is my fragment code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ViewGroup sceneRoot = (ViewGroup)rootView.findViewById(R.id.relative);

        final Transition transition = new ChangeBounds();
        transition.setDuration(3000);

        final Scene scene1 = Scene.getSceneForLayout(sceneRoot, R.layout.scene1, getActivity());
        final Scene scene2 = Scene.getSceneForLayout(sceneRoot, R.layout.scene2, getActivity());

        ImageView image = (ImageView)rootView.findViewById(R.id.image);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!testBoo){
                    TransitionManager.go(scene2, transition);
                    testBoo = true;
                } else {
                    testBoo = false;
                    TransitionManager.go(scene1, transition);
                }

            }
        });
        return rootView;
    }

fragment xml:

<RelativeLayout
    android:id="@+id/relative"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:showIn="@layout/activity_main"
    tools:context=".MainActivityFragment">

    <include
        layout="@layout/scene1"
        />

</RelativeLayout>

scene1 xml:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/text"
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/image"
        android:layout_margin="5dp"
        />
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentTop="true"
        />

</RelativeLayout>

scene2 xml:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/text"
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@id/image"
        />
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"

        />

</RelativeLayout>

Have you any idea where I am going wrong?

Blate answered 16/10, 2015 at 7:53 Comment(2)
found any answer yet ?Musculature
what about now? I have the same issue(Ecto
C
2

You need to set onClickListeners again to make it work.

In your onCreateView you can call setListeners method directly.

private void setListeners() {
    findViewById(R.id.thirdText).setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onClick(final View v) {
            final Scene scene3 = Scene.getSceneForLayout((ViewGroup) findViewById(R.id.mainlayout),
            R.layout.scene_3, MainActivity.this);
            Transition mFadeTransition = new ChangeBounds();
            TransitionManager.go(scene3, mFadeTransition);
            setListeners();
        }
    });

    findViewById(R.id.firsttext).setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onClick(final View v) {
            final Scene scene1 = Scene.getSceneForLayout((ViewGroup) findViewById(R.id.mainlayout),
            R.layout.activity_main, MainActivity.this);
            Transition mFadeTransition = new ChangeBounds();
            TransitionManager.go(scene1,mFadeTransition);
            setListeners();
        }
    });

    findViewById(R.id.secondText).setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onClick(final View v) {
            final Scene scene2 = Scene.getSceneForLayout((ViewGroup) findViewById(R.id.mainlayout),
            R.layout.scene_2, MainActivity.this);
            Transition mFadeTransition = new ChangeBounds();
            TransitionManager.go(scene2, mFadeTransition);
            setListeners();
        }
    });
}
Clabber answered 30/1, 2018 at 10:54 Comment(0)
N
-2

Interestingly enough (though it's the same for almost all tutorials), when I changed container IDs, it started to work. You can try the same method as well to see if it's working.

Nubbin answered 24/10, 2016 at 17:3 Comment(1)
Which containers (containers of what)?Rembert

© 2022 - 2024 — McMap. All rights reserved.