如何在片段之间导航?

时间:2014-07-20 05:08:44

标签: java android android-fragments

我是Android Fragments的新手。我有2个不同的片段,想要使用按钮从一个片段导航到另一个片段。例如,在片段A中,当我按下按钮时,我会转到fragmentB和vise verse。

以下是我的简单代码:

FragmentA.java

public class FragmentA extends Fragment {

    public FragmentA(){}

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

        View rootView = inflater.inflate(R.layout.fragment_a, container, false);

        return rootView;
    }

}

fragmenta.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="670dp"
    android:gravity="top"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/txtLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:text="Fragment 1"
        android:textStyle="bold"
        android:textSize="22dp" />
</LinearLayout>

FragmentB.java

public class FragmentB extends Fragment {

    public FragmentB(){}

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

        View rootView = inflater.inflate(R.layout.fragment_b, container, false);

        return rootView;
    }

}

fragmentb.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="670dp"
    android:gravity="top"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/txtLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:text="Fragment 2"
        android:textStyle="bold"
        android:textSize="22dp" />
</LinearLayout>

我需要按钮的代码(包括java和xml)。从头开始的帮助将不胜感激。在此先感谢!!

2 个答案:

答案 0 :(得分:2)

选中此Tutorial及以下代码以获取参考

<强> Fragment1.java

    public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }

    public void onClick(View view) {
        Fragment2 fragment2 = new Fragment2();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment1, fragment2);
        fragmentTransaction.commit();
    }

    }

<强> Fragment2.java

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2, container, false);  
        }
}

答案 1 :(得分:0)

活动中的代码

 public class MyActivity extends FragmentActivity implements OnListenerChangeFragment {

        FragmentA mFragmentA;
        FragmentB mFragmentB;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); 

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            // transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }

        @Override
        public void onShowFragmenA() {
             //replace mFragmentB by mFragmentA

        }

        @Override
        public void onShowFragmenB() {
             //replace mFragmentA by mFragmentA
        }
}  

//片段中的代码

public class FragmentA extends Fragment {

        private OnListenerChangeFragment mCallback;

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

                Button mButton = new Button(getActivity());

                mButton.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                mCallback.onShowFragmenB();
                        }
                });


                return mButton;
        }

        @Override
        public void onAttach(Activity activity) {
                super.onAttach(activity);

                try {
                        mCallback = (OnListenerChangeFragment) activity;
                } catch (ClassCastException e) {
                        throw new ClassCastException(activity.toString()+ " must implement OnListenerChangeFragment");
                }
        }
}

public class FragmentB extends Fragment {

        private OnListenerChangeFragment mCallback;


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

                Button mButton = new Button(getActivity());

                mButton.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                mCallback.onShowFragmenA();
                        }
                });

                return mButton;
        }


        @Override
        public void onAttach(Activity activity) {
                super.onAttach(activity);

                try {
                        mCallback = (OnListenerChangeFragment) activity;
                } catch (ClassCastException e) {
                        throw new ClassCastException(activity.toString()+ " must implement OnListenerChangeFragment");
                }
        }
}