39

我在我的应用程序中使用片段。这是我第一个简单地扩充 xml 文件的片段:

public class FragmentA extends SherlockFragment
{
    Context myContext,appContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    myContext = getActivity();
    appContext=getActivity().getApplicationContext();
    arguments = getArguments();
    doctor_id=arguments.getInt("doctor_id");
    userType=arguments.getString("userType");
    return inflater.inflate(R.layout.left_panel, container,false);
}

这是 left_panel .xml,其中包含一个片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:id="@+id/titles"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.example.sample.ListFrag" />

</LinearLayout>

这是我的 ListFrag 类:

public class ListFrag extends Fragment 
{
    Context myContext,appContext;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        layoutView = inflater.inflate(R.layout.activity_doctor_list,container);
        myContext = getActivity();
        appContext=getActivity().getApplicationContext();
        arguments=getArguments();
        int doctor_id=arguments.getInt("doctor_id");
}
}

我不知道如何Bundle arguments从 FragmentA 传递到 ListFrag。

4

4 回答 4

78

在您的 FragmentA 片段中,将捆绑包设置为参数。

Bundle args = new Bundle();
args.putInt("doctor_id",value);    
ListFrag newFragment = new ListFrag ();
newFragment.setArguments(args);

在您的 ListFrag 片段中,将捆绑包获取为

Bundle b = getArguments();
int s = b.getInt("doctor_id");
于 2013-06-12T10:49:04.667 回答
17

片段到片段集并获取参数:

开始活动:

     int friendId = 2; //value to pass as extra 
     i = new Intent(firstActivity, SecondActivity.class);
     i.putExtra("friendsID", friendId);
     firstActivity.startActivity(i);

第二活动:

     Fragment_A mFragment_A = new Fragment_A();
     mFragment_A.setArguments(getIntent().getExtras());

片段_A:

    Bundle bundle = new Bundle();
    String Item = getArguments().getString("friendsID");
    bundle.putInt("friendsID", Integer.parseInt(Item));

    // code

    Fragment_B mFragment_B = new Fragment_B();
    mFragment_B.setArguments(bundle);

片段_B:

    Bundle bundle = getArguments();
    int value = bundle.getInt("friendsID");

    Log.e("value Fragment get Argument ", "friendsID :" + value);

这对我有用,试试这可能是这个样本对你有帮助。

于 2014-02-13T07:28:34.653 回答
1

你有两个选择:

A. 如果我有您的 ListFragment 的引用, 您可以通过执行以下操作为 FragmentA 设置目标片段:

  1. 片段A this.setTargetFragment(yourListFragment);
  2. 然后this.getTargetFragment().setArguments(yourBundle);
  3. 并在ListFragment中使用this.getArguments();

B. 最合乎逻辑的方式

我敢打赌你的片段在同一个活动中,所以她有它们的参考。您可以将数据从FragmentA传递给您的活动,并将其传递给ListFragment

FragmentA --Data --> FragmentActivity --Data --> ListFragment

于 2013-06-12T11:29:45.130 回答
1

在 ListFrag 中创建 ListFrag 的静态方法如:

public static ListFrag newInstance(int doctorId) {
  ListFrag frag = new ListFrag();
  Bundle args = new Bundle();
  args.putExtra("doctor_id", doctorId);
  frag.setArguments(args);
  return frag;
}

当您从 Fragment A 创建 ListFrag 时,您会调用:

Fragment frag = ListFrag.newInstance(this.doctor_id);
// use frag with getSupportChildFragmentManager();
于 2013-06-12T10:45:17.683 回答