2014-04-29 158 views
0

我正在构建一个包含2个片段的视图。充气布局工作正常。但我有另一个问题。在创建这些片段之前,我需要将一些数据传递给这些片段(FragmentListSchema和FragmentSchemaDetail)。我想到了应用程序变量,但我想这不是正确的方法。我该怎么做 ? 在此先感谢将数据传递给多个片段

膨胀:

public class FragmentSchemaTotal extends Fragment{ 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    View view = inflater.inflate(R.layout.fragment_schema,container); 
    return view; 
    } 



} 

布局:

<?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"> 
<fragment android:name="kine.gui.FragmentListSchema" 
     android:id="@+id/list" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" /> 
<fragment android:name="kine.gui.FragmentSchemaDetail" 
     android:id="@+id/viewer" 
     android:layout_weight="2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

回答

1

使用FragmentsetArguments传递数据

FragmentSchemaTotal fragment = new FragmentSchemaTotal(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("data","your data"); 
    fragment.setArguments(bundle); 
+0

感谢您的回答。也许不清楚,我需要将数据传递给FragmentListSchema和FragmentSchemaDetail。有任何想法吗 ?谢谢 – David

+0

你需要从布局xml传递数据吗? – Libin

+0

我需要将数据从FragmentSchemaTotal传递给FragmentListSchema和FragmentSchemaDetail,它们在XML中定义并创建,但我不知道如何将数据传递给这些片段。 – David

0

为什么不使用装载机的片段里面有他们加载数据本身?虽然,Loader类的有用性取决于你处理的数据类型。不知道更多,很难提出正确的方法。

如果装载机不符合法案,您可以让活动找到碎片并在其上设置数据。在这之前,片段可以显示一个微调而不是它的视图。一旦数据被设置,片段就可以显示它的视图。

最后一种选择是在代码中创建片段并将参数传递给它们。尽管如此,这还不够灵活。我会推荐装载机。