2

I have a MainActivity and a Fragment called Frags that itself has a fragment called CardFrontFragment.

I register an OnClickListener on this fragment (the child fragment) and in this OnClickListener's OnClick method I replace this fragment(CardFrontFragment) by CardbackFragment fragment. Also I register an OnClickListener on this fragment(CardbackFragment) too, in order to switch again between this to child fragment.

OnClickListener in first fragment (CardfrontFragment) works and the second fragment is shown but now by clicking on the second fragment nothing happens, OnClickListener does not work with the second fragment, this is killing me, please help me!

Code:

MainActivity.java

public class MainActivity extends ActionBarActivity {
    public static Frags frag=new Frags();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, frag)
                    .commit();
        }
    }
}

Frags.java

public class Frags extends Fragment{

    CardBackFragment back=new CardBackFragment();
    CardFrontFragment front=new CardFrontFragment(); 

     View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view;
         view=inflater.inflate(R.layout.frag_layout, container, false);





                  getChildFragmentManager().beginTransaction()
                    .add(R.id.frag_container, back)
                    .commit();

                  return view;
    }


    public void Onflip(boolean iSback)
    {
        if(iSback)
        {
        getChildFragmentManager().beginTransaction()
        .setCustomAnimations(
              R.animator.card_flip_right_in, R.animator.card_flip_right_out,
               R.animator.card_flip_right_in, R.animator.card_flip_right_out)
               .replace(R.id.frag_container, front)
        .commit();
        }
        else
        {
            getChildFragmentManager().beginTransaction()
            .setCustomAnimations(
                  R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                   R.animator.card_flip_right_in, R.animator.card_flip_right_out)
                   .replace(R.id.frag_container, back)
            .commit();

        }

    }


}

CardFrontFragment

public class CardFrontFragment extends Fragment {
    public CardFrontFragment() {
    }

    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         view=inflater.inflate(R.layout.fragment_card_front, container, false);

         view.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {


                MainActivity.frag.Onflip(false);

                Log.i("front","onclick");
            }




         });


         return view;
    }

}

CardBackFragment

public class CardBackFragment extends Fragment {
    public CardBackFragment() {
    }

    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         view=inflater.inflate(R.layout.fragment_card_back, container, false);

         view.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {


                MainActivity.frag.Onflip(false);

                Log.i("back","onclick");
            }




         });


         return view;
    }

}

xml:

fragment_card_front.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#0000ff" >

      <Button
          android:id="@+id/front_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" />

</LinearLayout>

fragment_card_back.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#000000" >

      <Button
          android:id="@+id/back_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" />

</LinearLayout>

frag_layout.xml

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


</LinearLayout>

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.demokhar.MainActivity"
    tools:ignore="MergeRootFrame" />
masmic
  • 3,526
  • 11
  • 52
  • 105
arsenal
  • 199
  • 1
  • 1
  • 13
  • this is very strang,because the two child fragment are identical. – arsenal Jul 10 '14 at 01:11
  • 1
    Wouldn't it make more sense to have a single card `Fragment` and just switch the image? For that matter, do you really need a `Fragment` for the card? Perhaps a simple `View` subclass would be better. – Mike M. Jul 10 '14 at 01:46
  • 1
    i cant use image because i want button on each side, and separate onclicklisteners, i am new in android, and this was first thing come to my mind, using View sounds good.thanks – arsenal Jul 11 '14 at 01:35

0 Answers0