使用一个片段视图替换多个片段类



我在理解和使用片段时遇到了一些困难,我有3个按钮和1个片段视图。使用按钮作为选项卡,我试图在点击按钮时为片段提供一个特定的类。我所做的是:

 <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignTop="@+id/linearLayout1" >
    <Button
        android:id="@+id/Tab1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Tab 1" android:background="@drawable/btn"/>
    <Button
        android:id="@+id/Tab2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Tab 2" android:onClick="switchToTab2" />
    <Button
        android:id="@+id/Tab3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Tab 3" android:onClick="switchToTab3" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/linearLayout1"
    android:layout_below="@+id/linearLayout1" >
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.newproject.fragmentclass"
        android:layout_width="match_parent"
        android:layout_height="394dp" />

</LinearLayout>

java类:

碎片类frt1
fragmentclass2 frt2
碎片3类frt3
FragmentTransaction ft

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_tabs_exp);
        ft = getSupportFragmentManager().beginTransaction();
        frt1 = new fragmentclass();                
        frt2 = new fragmentclass2();
        frt3 = new fragment3class();
        //ft.add(R.id.fragment1,frt1); 
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fragment_tabs_exp, menu);
    return true;
}
public void switchToTab2 (View v){
    ft.replace(R.id.fragment1, frt2).commit();
}
public void switchToTab3 (View v) {
    ft.replace(R.id.fragment1, frt3).commit();
}

当我点击按钮2时,片段类2是成功可见的,除了片段的默认类仍然可见之外,还有隐藏它并使新的片段类活动

在xml文件中添加片段将使整个应用程序保持静态,并且不能动态更改它。

如果您想动态添加Fragments,那么您需要在xml文件中添加FrameLayout,并在该布局中动态加载所有片段,并在加载一个又一个片段时将其替换。

在布局中进行以下更改:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/linearLayout1"
    android:layout_below="@+id/linearLayout1" >
    <FrameLayout
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="394dp" />
</LinearLayout>

从XML文件中删除fragment1(因此在布局创建时容器默认为空),并从代码中添加(即在父片段的onCreateView()或"活动"的onCreate()中),因为如果片段是XML布局文件的一部分,则无法从层次结构中删除。

如果您计划只在容器中有一个子容器,我也会将容器更改为FrameLayout而不是LinearLayout

最新更新