1

在我的项目中,我正在使用带有 viewpager 的操作栏,并且有三个选项卡,它们是片段,还有一个第四个片段,当我单击第一个片段中的按钮时,我将其替换为第四个片段,现在我在这里遇到问题时我单击第四个片段的列表项,我需要将列表数据传递到第一个选项卡(第一个片段)并显示它。我正在使用 Bundle 发送数据,如下面的第四个片段

public class PlaceSearchFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.autosearchactivity, container, false);

        return rootView;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parentView, View view, int position,
                    long arg3) {

                int product =lv.getId();
                Bundle bundle = new Bundle();
                bundle.putInt("y", product); //any string to be sent
                System.out.println("Bundle loaded  "+product);
                Fragment newFragment = new GetRideFragment();
                newFragment.setArguments(bundle);

                          Fragment productDetailFragment = new GetRideFragment();
                  FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                 transaction.remove(PlaceSearchFragment.this);
                  transaction.replace(R.id.replaceautosearch, productDetailFragment,"yourfragment").commit();   

}           
});     
}

在我的第一个片段中,我如下调用 bundle

public class Myplace extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.autosearchactivity, container, false);

        return rootView;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

  if ( b != null ){
            b = getArguments();
            int s = b.getInt("y");
            System.out.println("printinf bundle data==>"+s);
            getfrom.setText(""+s);
}}

任何帮助表示赞赏。

4

1 回答 1

0

Just use fragment constructor

private int value;

public MyFragment(int value) {
    this.value=value;
}

and call this fragment like this

MyFragment fragment = new MyFragment(25);
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
于 2014-12-03T06:52:00.923 回答