1

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

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

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

我应该使用什么?片段,片段活动。

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

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

我所做的是制作了 4 个片段并将它们放入 Main_Activity

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

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

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

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

我很乐意为您提供建议!谢谢!

4

2 回答 2

3

您可以尝试检查FragmentTransaction

查看替换/添加/删除片段方法。我相信你会找到你正在寻找的东西。

前任 :

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

和类似的东西:

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

container一个简单的视图(RelativeLayout 或其他东西)在哪里。

编辑 :

例子 :

主要的.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 个片段,你正在尝试做什么。

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

Your containers will be the different IDs I put (big_fragment, fragment_snap1 & fragment_snap2.

于 2013-09-26T14:13:57.160 回答
0

你必须实现监听器..通过以下链接: -

http://developer.android.com/training/basics/fragments/communicating.html

于 2013-09-26T13:58:32.280 回答