gpt4 book ai didi

java - 使用一个 fragment View 替换多个 fragment 类

转载 作者:行者123 更新时间:2023-12-02 06:26:02 24 4
gpt4 key购买 nike

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

 <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类:

fragmentclass frt1;
fragment 类2 frt2;
fragment3class 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时, fragment 类2成功可见,除了 fragment 的默认类仍然可见之外,无论如何都可以隐藏它并使新的 fragment 类处于 Activity 状态

最佳答案

在 xml 文件中添加 fragment 将使整个应用程序保持静态,并且无法动态更改它。

如果您想动态添加 fragment ,那么您需要在 xml 文件中添加 FrameLayout 并在该布局中动态加载所有 fragment ,并在加载 fragment 时逐个替换它。

在布局中进行以下更改:

<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>

关于java - 使用一个 fragment View 替换多个 fragment 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20515298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com