如何动态地改变片段的类 [英] how to change fragment's class dynamically
本文介绍了如何动态地改变片段的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我有一个包含2个片段的LinearLayout中,我添加标签与code到此布局。当我点击TAB1它是确定片段指定类填充本身我想是的,但在TAB2我想这个类更改为另一个类。谢谢
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=横向
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID /断肢>
<片段类=com.tugce.MitsActionBar.DoktorlarFragment
机器人:ID =@ + ID / frag_title
机器人:能见度=水涨船高
机器人:layout_marginTop =机器人:ATTR / actionBarSize
机器人:layout_width =@扪/ titles_size
机器人:layout_height =match_parent/>
<片段类=com.tugce.MitsActionBar.ContentFragment
机器人:ID =@ + ID / frag_content
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
解决方案
修改<片段/>
在XML布局<的FrameLayout />
<的FrameLayout
机器人:ID =@ + ID / frag_title
机器人:能见度=水涨船高
机器人:layout_marginTop =机器人:ATTR / actionBarSize
机器人:layout_width =@扪/ titles_size
机器人:layout_height =match_parent/>
<的FrameLayout
机器人:ID =@ + ID / frag_content
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
和编程方式添加片段:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment片段=新ExampleFragment();
fragmentTransaction.replace(R.id.frag_content,片段);
fragmentTransaction.commit();
但在第一次读到这。
Hi I have a linearLayout containing two fragments and I add tabs with code to this layout. What I want is when I click tab1 it is ok to fragment fill itself from indicated class, but in tab2 I want to change this class to another class. Thank you
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frags">
<fragment class="com.tugce.MitsActionBar.DoktorlarFragment"
android:id="@+id/frag_title"
android:visibility="gone"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_width="@dimen/titles_size"
android:layout_height="match_parent" />
<fragment class="com.tugce.MitsActionBar.ContentFragment"
android:id="@+id/frag_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
解决方案
Change <fragment/>
in xml layout to <FrameLayout/>
<FrameLayout
android:id="@+id/frag_title"
android:visibility="gone"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_width="@dimen/titles_size"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/frag_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and add fragments programmatically:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.replace(R.id.frag_content, fragment);
fragmentTransaction.commit();
But at first read this.
这篇关于如何动态地改变片段的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文