片段和片段沟通

时间:2017-03-14 05:28:47

标签: android fragment communication pause

您好我的问题是这个。我在View Pager中有一个片段,它有一个按钮,当有标签的时候,底部的一张片打开,里面有另一个片段。底部的工作表有一个对象列表,当用户选择了一个我希望将该对象传递给我前面提到的第一个片段时,问题是该片段被暂停了。 如何将数据从一个片段发送到另一个片段暂停?

2 个答案:

答案 0 :(得分:0)

在片段之间传递数据有多种方法。很少有人:

  1. 您可以使用参与片段类的getter / setter
  2. 您可以使用其父活动
  3. 在片段之间共享数据
  4. 您可以使用界面在片段或活动之间更新/传递数据
  5. 您可以使用观察者设计模式在片段或活动之间共享/传递数据
  6. 您也可以使用Bundle传递数据
  7. 取决于您选择最适合您需求的方法。

答案 1 :(得分:0)

使用此

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        System.out.print(position + "to" + tab_position);


        Fragment fragment = null;
        switch (position) {
            case 0:

                Bundle bundle = new Bundle();
                bundle.putString("message", "getdata");
                bundle.putStringArrayList("name", running_veicle);
                bundle.putStringArrayList("loc", running_loc);
                bundle.putStringArrayList("running_device_id", running_device_id);
                fragment = new Vehicle_Status_fragment_running_info();
                fragment.setArguments(bundle);


                break;
            case 1:

                Bundle bundle1 = new Bundle();
                bundle1.putString("message", "getdata");
                bundle1.putStringArrayList("name", idle_name);
                bundle1.putStringArrayList("loc", idle_loc);
                bundle1.putStringArrayList("idle_device_id", idle_device_id);
                fragment = new Vehicle_Status_fragment_Idle_info();
                fragment.setArguments(bundle1);

                break;
            case 2:

                Bundle bundle2 = new Bundle();
                bundle2.putString("message", "getdata");
                bundle2.putStringArrayList("name", stop_name);
                bundle2.putStringArrayList("loc", stop_loc);
                bundle2.putStringArrayList("stop_device_id", stop_device_id);
                fragment = new Vehicle_Status_fragment_Stop_info();
                fragment.setArguments(bundle2);
                break;

            case 3:
                Bundle bundle3 = new Bundle();
                bundle3.putString("message", "getdata");
                bundle3.putStringArrayList("name", Innactive_name);
                bundle3.putStringArrayList("loc", Innactive_loc);
                bundle3.putStringArrayList("innactive_device_id", Innactive_device_id);
                fragment = new Vehicle_Status_fragment_Inactive_info();
                fragment.setArguments(bundle3);
                break;

        }
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 4 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();

        switch (position) {
            case 0:
                return getString(R.string.tab_Running).toUpperCase(l);
            case 1:
                return getString(R.string.tab_Idle).toUpperCase(l);
            case 2:
                return getString(R.string.tab_Stop).toUpperCase(l);
            case 3:
                return getString(R.string.tab_Innactive).toUpperCase(l);

        }
        return null;
    }


}

获取数据

 View view = inflater.inflate(R.layout.fragment_tab_veicle_status_list, container, false);
    listobj = (ListView) view.findViewById(R.id.gridview);
    String myValue = this.getArguments().getString("message");
相关问题