如何通过点击其中一个来切换两个片段?

时间:2014-07-09 15:29:06

标签: android android-fragments

假设我们有两个片段Frag1和Frag2,现在我的问题是如何通过使用“FragmentTransaction Replace”点击其中一个片段来切换它们

这是我的代码:

    public class Frags extends Fragment{

    private boolean mShowingBack = false;
    CardFrontFragment fr=new CardFrontFragment();
    CardBackFragment bk=new CardBackFragment();
    static View view;

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


            if (!mShowingBack)
            {
                return fr.onCreateView(inflater, container, savedInstanceState);
            }

            else
            {
                return bk.onCreateView(inflater, container, savedInstanceState);

            }


    }













    public static class CardFrontFragment extends Fragment {
        public CardFrontFragment() {
        }
        @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) {


                }




             });



             return view;
        }
    }


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

        @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) {

                }




             });
             return view;
        }

    }










    public void flipCard() {
        if (mShowingBack) {
            getChildFragmentManager().popBackStack();
            mShowingBack = !true;
            return;
        }
        mShowingBack = true;
        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.container, this.bk)
                .addToBackStack(null)
                .commit();



}

和问题:无法找到容器 logcat:找不到ID为0x7f600054(id / container)的视图

所以我错了? 有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

这通常意味着您没有在布局文件中使用id容器定义片段。

在片段布局中

<LinearLayout
    android:layout_width = "fill_parent"
    android:layout_height = "fill_parent">

    <FrameLayout              
        android:id="@+id/container"   <!-- this part is missing that is why view cannot find -->
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

此外,您似乎没有在片段onCreateView中夸大您的布局。这也可能导致问题,因为系统可能不会想要创建哪个视图。