OnClickListener in Fragments not working

I have a MainActivity and Fragment called Frags, which has a fragment called CardFrontFragment.

I register OnClickListener on this fragment (child fragment) and in this OnClickListener OnClick method I replace this fragment (CardFrontFragment) with the CardbackFragment fragment. In addition, I also register OnClickListener on this fragment (CardbackFragment) to switch between it and the child fragment again.

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

The 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();
        }
    }
}

Fragss.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" />
+1

Source: https://habr.com/ru/post/1666113/


All Articles