子片段之间的动态数据交换

时间:2014-05-22 13:29:32

标签: android android-fragments android-viewpager android-nested-fragment otto

有一个活动包含碎片及其子碎片。需要在两个子片段之间交换动态生成的字符串数组。但是,无论我使用哪种技术,它每次都会返回null

  

我也使用自定义界面实现,即使它们在我的设计中很痛苦。如果你能为这个问题提出任何解决方案,我将不胜感激。

enter image description here

Fragment-X和Fragment-Y在ViewPager适配器中定义,onPauseFragment()onResumeFragment()方法由自定义OnPageChangeListener()接口方法实现。

public class Fragment_X extends SherlockListFragment implements FragmentLifecycle
{
  ...
    @Override
    public void onPauseFragment() 
    {
      String[] outpuStringArray = {"text1","tex2"};
      Fragment_X fragment = new Fragment_X();
      Bundle bundle = new Bundle();
      bundle.putStringArray("selectedItems", outputStringArray);
      fragment.setArguments(bundle);
      Log.d("posted bundle", String.valueOf(bundle));

      bus = new Bus();
      bus.post(bundle);
    }
   ...
 }

当ViewPager项目位置发生变化时,Fragment_Y应该接收捆绑消息

public class Fragment_Y extends SherlockListFragment implements FragmentLifecycle
{
 ...
    private static String[] list_subject_name;
 ...
    @Override
    public void onResumeFragment() 
    {
      Bundle bundle = getArguments();
      Log.d("received bundle", String.valueOf(bundle));
      if(getArguments()!=null)
      {
        list_subject_name = bundle.getStringArray("selectedItems");
      } else {
            list_subject_name = new String[] {""};
      } 
      bus = new Bus();
      bus.register(this);    
    }
    ...
}

用于控制片段之间状态更改的FragmentLifecycle接口类

public interface FragmentLifecycle {

public void onPauseFragment();
public void onResumeFragment();
...
}

EDIT :ViewPager适配器

public class ViewPagerAdapter extends FragmentPagerAdapter
{
public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;
private String titles[] = new String[] { "Fragment_X", "Fragment_Y", "Fragment_Z" };

@Override
public Fragment getItem(int position)
{
    switch (position)
    {
    case 0:
        Fragment_X tab1 = new Fragment_X();
        return tab1;

    case 1:
        Fragment_Y tab2 = new Fragment_Y();
        return tab2;

    case 2:
        Fragment_Z tab3 = new Fragment_Z();
        return tab3;
    }


    return null;
}


public CharSequence getPageTitle(int position)
{
    return titles[position];
}

@Override
public int getCount()
{
    return PAGE_COUNT;
}

}

EDIT: Fragment_B

public class Fragment_B extends SherlockFragment
{
private ViewPagerAdapter pageAdapter; 

public void onAttach(FragmentActivity activity)
{
    super.onAttach(activity);
    setHasOptionsMenu(false);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.layout_fragment_viewpager, container, false);

    pageAdapter = new ViewPagerAdapter(getChildFragmentManager());

    ViewPager mViewPager = (ViewPager) view.findViewById(R.id.registrationViewPager);
    mViewPager.setAdapter(pageAdapter);

    mViewPager.setOnPageChangeListener(pageChangeListener);

    return view;
}

private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

    int currentPosition = 0;

    @Override
    public void onPageSelected(int newPosition) {

        FragmentLifecycle fragmentToShow = (FragmentLifecycle)pageAdapter.getItem(newPosition);
        fragmentToShow.onResumeFragment();

        FragmentLifecycle fragmentToHide = (FragmentLifecycle)pageAdapter.getItem(currentPosition);
        fragmentToHide.onPauseFragment();

        currentPosition = newPosition;
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) { }

    public void onPageScrollStateChanged(int arg0) { }
};


@Override
public void onDetach()
{
    super.onDetach();

    try {
        Field childFragmentManager =
                Fragment.class.getDeclaredField("mChildFragmentManager");

        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
}

EDIT: 以下是ViewPger片段(Fragment_XFragmentY),我尝试选择列表项,收集Strin数组并使用bundle发布在Fragment_Y中查看。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用事件总线来完成此操作。

Fragment_Y注册到一个事件,Fragment_X发布该事件。然后将Fragment_Y通知更改。

库的例子: