Replacing Fragments isn't working/Am I executing this the proper way?

后端 未结 5 766
滥情空心
滥情空心 2020-12-14 08:43

It\'s taken me some time to wrap my head around fragments, but this should be my last question on fragments, since I think I just about have them down. I know this is a huge

相关标签:
5条回答
  • 2020-12-14 09:02

    Instead of <fragment> use <FrameLayout> in layout xml for activity.

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

    Then in FragmentActivity in onCreate add initial fragment (in your case SSFFragment):

        FragmentA fragmentA = new FragmentA();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.container_id, fragmentA);
        transaction.commit();
    

    From inside fragment you can replace fragment in container.

    class FragmentA extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            Button button = new Button(getActivity());
            button.setText("Replace");
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    FragmentB fragmentB = new FragmentB();
                    transaction.replace(R.id.container_id, fragmentB);
                    transaction.commit();
                }
            });
            return button;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 09:03

    In short, you CAN NOT replace fragment if u defined it in XML with fragment tag. Instead, as @pawelzieba adviced add Frame tag in your layout, find it and add,remove, replace fragments there.. Hope it helped. Cheers

    0 讨论(0)
  • 2020-12-14 09:11

    Here is the answer to your real question...since this was your second question resulting from your original post, I've modified the solution to get at that frag in another way:

    Fragment details = (Fragment)getSupportFragmentManager().findFragmentById(R.id.details);
    details = new ExamplesFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.details, details);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.commit();
    

    Also, the android.support.v4.app part is just not necessary, and frankly leads to possible hours of "going down the wrong road" type efforts by adding and removing it all over your code (as long as you're using:)

    import android.support.v4.app.FragmentTransaction;
    

    In this my example, you don't need to import the support for FragmentManager. However, if you're getting errors, make sure you've imported the library itself into your "libs" folder.

    This solution will fix the overlapping fragment problem, and hopefully save people hours of tinkering around with replacing frags.

    0 讨论(0)
  • 2020-12-14 09:12

    well i was facing the same problem and i just replace the fragment from main layout with linear lay out and guess what its working.. its strange dont know how but its working. i am using actionbar to switch between fragments for replacing my code is :

     protected class MyTabsListener1 implements ActionBar.TabListener{
            private Fragment frag ;
            public MyTabsListener1 ( Fragment frag){
                this.frag = frag;
            }
    
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                //  TODO Auto-generated method stub
    
            }
    
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
    
                    switch (tab.getPosition()){
                    case 0:
    
                            ft.replace(R.id.replace, homeFrag);
                        break;
    
                    case 1:
                        ft.replace(R.id.replace, createFrag);
                            break;
    
                    case 2:
                        ft.replace(R.id.replace, ioListFrag);
                        break;
                    case 3:
    
                        ft.replace(R.id.replace, settingFrag);
    
                        break;      
    
    
                    default: 
    
    
                        break;
    
                    }
    
                }
    

    and my main layout is this :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <LinearLayout
            android:id="@+id/replace"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
        </LinearLayout>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-14 09:19

    The main benefit of using fragments is to be able to make them take up portions of the screen rather than the whole screen, which is what Activities do.

    If you're just making an app for a small screen that will function like an Activity, but is coded in Fragments, just make a separate FragmentActivity for each of your Fragments.

    Make this the onCreate of your FragmentActivity:

    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.emptylayout);
    
        FragmentManager fragmentManager = getSupportFragmentManager(); //Or getFragmentManager() if you're not using the support library
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        YourFragment fragment = new YourFragment();
        fragmentTransaction.add(R.id.emptyview, fragment);
        fragmentTransaction.commit();
    }
    

    Where layout/emptylayout is an XML layout file with a FrameLayout. id/emptyview is that FrameLayout.

    If you want to use XML for your fragment's actual layout, make a separate XML layout for the actual fragment, and inflate it in the fragment's `onCreateView':

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.files, container, false);
                // Do stuff here
        return view;
    }
    

    Then just use startActivity(new Intent(getActivity(), YourFragmentActivity.class)) to launch a FragmentActivity from a Fragment.

    It seems redundant, yeah, but if you're going to be targeting larger screens later (if not, why are you bothering with fragments?), it'll make it easier in the long run.

    0 讨论(0)
提交回复
热议问题