简体   繁体   中英

How to change fragments for Android application development, with buttons inside each fragments?

I have an Android activity that has two fragments inside, say A and B. I want to click buttons inside A such that B would be switched to C, D, or E.

The layout each refers to a .xml and the all layout have different buttons calling other other fragments or doing other stuffs....

now in main.java:

public void stackAFragment(int page) {

//depending on which button on the list is clicked, the corresponding fragment is replaced
if (npage = 1){
Fragment f = new FragmentB();}

if (npage = 2){
Fragment f = new FragmentC();}

if (npage = 3){
Fragment f = new FragmentD();}

FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.replace(R.id.frag_content, f); 
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
ft.addToBackStack(null); 
ft.commit();}

then in A.java, there is a list view, and for the listener

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Main main = (Main) getActivity();
main.stackAFragment(arg2 + 1); }

and in B.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
View c = inflater.inflate(R.layout.frag_b, container, false);
return c;}

and in C.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
View c = inflater.inflate(R.layout.frag_c, container, false);
return c;}

and so on.....

So now, in the first page, it shows fragment A and B.

but when I clicked to change to fragment C, fragment B does not go away, instead, fragment C appears under fragment B

and when I clicked to change to fragment D, fragment C goes away but B still stays....

How could I solve my problem??

Thank very much!!! Highly appreciate if anybody could point me the right way.....

Additional information:::: the layout of main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frags"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SlidingMenu" >

<fragment
    android:id="@+id/frag_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="com.seapointcorp.enque01.ContentFragment" />

<fragment
    android:id="@+id/frag_title"
    android:layout_width="@dimen/titles_size"
    android:layout_height="match_parent"
    class="com.seapointcorp.enque01.TitleFragment" />

</FrameLayout>

The main problem with your code is that you're doing a replace FragmentTransaction using the id of a Fragment item from the layout file(so you basically make the replace in the fragment with id frag_content (which I assume is fragment B)). Instead you should have a wrapper layout(a simple FrameLayout , RelativeLayout etc) to act as an area to which the content fragment will be added/removed/replaced:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frags"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SlidingMenu" >

    <FrameLayout android:id="@+id/content"     android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <fragment
           android:id="@+id/frag_content"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           class="com.seapointcorp.enque01.ContentFragment" />
    </FrameLayout>
    <fragment
        android:id="@+id/frag_title"
        android:layout_width="@dimen/titles_size"
        android:layout_height="match_parent"
        class="com.seapointcorp.enque01.TitleFragment" />

    </FrameLayout>
</FrameLayout>

Then in your stackAFragment() method you'll do:

FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.replace(R.id.content, new tagetFragment()); 
 ft.addToBackStack(null);

            // Commit the transaction
            ft.commit();
//...

In your host activity

public static FragmentListener sFragmentListener;    

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Do normal work.
    sFragmentListener = new FragmentListener();
    // Do more work.
}

public interface FragmentListener {
        void onNextFragment();
    }

    public final class FragmentListenerListener {

        @Override
        public void onShowNextFragment() {
            // Do Fragment Transition
            mFragmentTransition.replace(R.id.awesomeContainer, new A.class);
        }

    }

in your Fragment 's OnClick method just call: HostActivity.sFragmentListener.onShowNextFragment() to execute the method.

You can modify this to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM