简体   繁体   中英

Android: change layout.xml in fragment

I have two fragments side by side. When I do an action on the left fragment I want the right fragment to change. It works as long as the layout.xml of the right fragment is not changed. What I want is to define a number of layouts, eg layout1.xml, layout2.xml, and so Depending what happens on the left on the right one of these layouts shall be shown. I found http://developer.android.com/guide/components/fragments.html#Transactions but I am not sure if that is the right approach. If not what would be the right way? If it is I struggle a bit with

transaction.replace(R.id.fragment_container, newFragment);

I need to tell the newFragment that it has now eg layout27.xml. How can I do this?

Edit:

My main.xml looks like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<fragment class="com.whatever.OverviewFragment"
    android:id="@+id/list"
    android:name="com.whatever.OverviewFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />

    <fragment class="com.whatever.DetailFragment"
    android:id="@+id/listB"
    android:name="com.whatever.DetailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />

</LinearLayout>

The second fragment should be swapped on user actions, for listB I have lets say 5 different layout.xml files.

DetailFragment.java looks basically like this:

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

    //this is called when I do an action in the other fragment
    public void setScreen(int id) {
        //This fragment is one of the 5 possible fragments
        DetailFragment2 newF = new DetailFragment2();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.listB, newF);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

I do something similar using the fragment arguments to pass which layout to choose

Fragment 1 (control)

Bundle data = new Bundle();
data.putInt("LayoutChoice",i); // i chooses which layout to use on fragment 2
Fragment f = new Fragment2();
f.setArguments(data);
fragmentTransaction.replace(R.id.fcontainer, f);

Fragment 2 (display)

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

    switch( getArguments().getInt("LayoutChoice") ) {
    case 1:
        rootView = inflater.inflate(R.layout.layout1, container, false);
        break;
    case 2:
        rootView = inflater.inflate(R.layout.layout2, container, false);
        break;
    }

So what you can do is use The fragment transaction manager and on myabe some kind of event (lets assume a button click event) you change the way you want the fragment to look.

For my code i have a tab click on which a new fragment is loaded so what i do is i replace the old fragment with the new one.

        RSSFragment rssfrag = new RSSFragment();
        FragmentManager fmi = getSupportFragmentManager();
        FragmentTransaction ftu = fmi.beginTransaction();
        ftu.replace(R.id.frame_fragment, rssfrag).addToBackStack(null).commit();

RSSFragment is a class which i am invoking and then using the FragmentManager and FragmentTransaction to replace the fragments. So you can inflate any fragment for whichever you want

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