Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

android - How can i create fragments for my three tabs tabbed layout dynamically

I have a three tabs tabbed layout. I want to replace the fragments in each one of them with new fragments on corresponding button click on each old fragment. I got the idea that in order to do that i have to include a container(preferably framelayout) which i can replace to show up the new fragment. I did that like this: Activity xml

<android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment"
        android:layout_below="@id/appbar"
        >
        <fragment
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/tabA"
            android:name="com.moodoff.Moods"
            tools:layout="@layout/fragment_moods" />
    </FrameLayout>

Now i created the dynamic fragment that would replace the first tab like this inside my activity's onCreateView method like this:

 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_all_tabs, container, false);

            Log.e("ALLTABS","I am called again..");

            /*mViewPager.setCurrentItem(Start.switchToTab);
            mViewPager.getAdapter().notifyDataSetChanged();*/

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            Fragment newFragment = Moods.newInstance("replacedA","b");
            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack if needed
            //transaction.replace(R.id.allmoods, newFragment);
            transaction.replace(R.id.fragment, newFragment);
            transaction.addToBackStack("mainA");
            //transaction.commitAllowingStateLoss();

            return rootView;
        }

Now what i do is from my original fragment button click i load new fragment in the same container and its working fine. But the problem is in the second and the third tab, the same fragment got displayed, and i don't see the other two tabs in the other two fragments. How can i resolve this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...