当我单击按钮时,片段无法工作

时间:2018-10-14 12:23:03

标签: java android

我试图创建一个记忆游戏,在第一页上,当我单击播放按钮时,它会显示类别并使用片段。当我单击一个按钮转到另一个片段时,它不起作用。

Mainactivity.java

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageViewA = findViewById(R.id.play_title);
        imageViewA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                FragmentPlay fragA = new FragmentPlay();
                transaction.add(R.id.fragmentContainer_main, fragA);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
    }
}

FragmentPlay.java

public class FragmentPlay extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.fragment_play,container,false);
        return rootView;
    }

    public void addFragB(){
        FragmentManager childFragMan=getChildFragmentManager();
        FragmentTransaction childFragTrans = childFragMan.beginTransaction();
        FragmentAnimal fragmentAnimal = new FragmentAnimal();
        childFragTrans.add(R.id.fragmentContainer_main2, fragmentAnimal);
        childFragTrans.addToBackStack(null);
        childFragTrans.commit();
    }
}

FragmentAnimal.java

public class FragmentAnimal extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.fragment_animal,container,false);
        return rootView;
    }
}

0 个答案:

没有答案