gpt4 book ai didi

android - 如何动态交换 fragment ?

转载 作者:行者123 更新时间:2023-11-29 17:56:23 25 4
gpt4 key购买 nike

我有点困惑如何在 Android 应用程序中使用 Fragments!我是 android 的初学者,我正在尝试为 tablet 7 Landscape 构建应用程序,如下图所示!

A, B, D 如何交换 fragment !通过点击事件。当用户点击 A 时,A 的内容移动并显示在 D 中,D 的内容显示在 A 中,依此类推。

我希望能够动态交换 A、B、D 的内容!

我应该使用什么? fragment , fragment Activity 。

例如,如果 D 在 VideoView 中显示视频,那么当用户单击 A 我希望 D 显示 TextView 或任何类型的数据(但不是视频 View )以及在 A 中显示视频。

<pre>
<!-->
////////////////////////////////////////////
/ ------- ----------------------------- /
/ - A - - - /
/ - - - - /
/ ------- - - /
/ - C - - D - /
/ - - - - /
/ ------- - - /
/ - B - - - /
/ - - - - /
/ ------- ----------------------------- /
////////////////////////////////////////////
<-->
</pre>

我所做的是制作了 4 个 fragment 并将它们放在 Main_Activity 中

fragment_A 我在与 D 交换内容时遇到问题。

fragment_B 我在更改 D 的内容时遇到问题。

fragment_C 我连接到显示一些静态数据的 Fragment_C 类。

fragment_D 我在将内容与 A 交换时遇到问题。

很高兴为您提建议!谢谢!

最佳答案

你可以试试去查FragmentTransaction

检查替换/添加/删除 fragment 方法。我相信您会找到您要找的东西。

例如:

FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(container, fragment);
fragmentTransaction.commit();

和类似的东西:

FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(container, newFragment);
fragmentTransaction.commit();

其中 container 是一个简单的 View (RelativeLayout 或其他东西)。

编辑:

示例:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
android:id="@+id/fragment_snapshots"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<fragment
android:id="@+id/fragment_snap1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<fragment
android:id="@+id/fragment_snap2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

<fragment
android:id="@+id/big_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

</LinearLayout>

这个布局几乎指定了 - 除了我在左边只有 2 个 fragment ,你正在尝试做什么。

然后使用 onClickListeners 和我上面给你的功能,你也许可以做你想做的事。

您的容器将是我放置的不同 ID(big_fragment、fragment_snap1 和 fragment_snap2。

关于android - 如何动态交换 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19030296/

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