Android嵌套片段:如何将结果值传递给嵌套的子片段

时间:2013-12-30 06:21:47

标签: android android-fragments

您正在处理嵌套片段。我的Activity有两个名为Frag1和Frag2的Tab。 Frag1有子项Tab,名为FragA和Frag B.从FragA我调用一个活动,我将其结果返回到Fragment Activity。所以根据结果我需要重新加载或重新创建FragA。请有人告诉我,如何将来自ParentFragment的onActivityResult()的vales传递给子Fragment FragA。提前致谢。

顶级片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_tabhost);

    arg1 = new Bundle();
    arg1.putString("tab", "tab1");
    mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Tab1"),
            FragmentTestA.class, arg1);

    arg1 = new Bundle();
    arg1.putString("tab", "tab2");
    mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Tab2"),
            FragmentTestB.class, arg1);

    arg1 = new Bundle();
    arg1.putString("tab", "tab3");
    mTabHost.addTab(mTabHost.newTabSpec("Tab3").setIndicator("Tab3"),
            FragmentTestC.class, arg1);

    return mTabHost;
}

FragmentTestA

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

    // initially load default fragment here
    fg = new DefaultFragment();
    replaceFragment(fg);

    if (getArguments() != null) {
        mCurrentTab = getArguments().getString("tab");
    }

    mNotUsedBtn = (Button) mainView.findViewById(R.id.btn2);
    mNotUsedBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    });

    mUsedBtn = (Button) mainView.findViewById(R.id.btn3);
    mUsedBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    });

    return mainView;
}

DefaultFragment:

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

    mExpSoon = getArguments() != null ? 
            getArguments().getString("expiring_soon") : "";
    mStatus = getArguments() != null ? 
            getArguments().getString("status") : "";
    mCurrentTab = getArguments() != null ? 
            getArguments().getString("tab") : "";
        mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub
               Calling Activity from here
        }
    }); 



    return mainView;
}

因此调用了intent并返回了一些结果。我们如何将值传递给最后一个子节点以根据结果执行操作。我从onActivityResult()获得结果,如何将数据发送到上一个活动。

2 个答案:

答案 0 :(得分:0)

您可以使用ViewFlipper而不是Tab。在这里,您可以使用相同的FragmentActivity类和onActivityResult(...)方法。 e.g

   vFlipper = (ViewFlipper) view.findViewById(R.id.ViewFlipper_tab);

     info.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            vFlipper.setDisplayedChild(0);
        }
    });

    mat.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



            vFlipper.setDisplayedChild(1);
        }
    });

答案 1 :(得分:0)

你应该实现这个,这是我在网上看到的,但我不记得在哪里:

  

接口类

interface Communicator{
    void doSomething();      
    SomeClass getSomething();
}
  

MainActivity或CurrentActivity(调用片段的行为)

public class SomeActivity extends .... implements ....,Communicator{
    SomeClass object;
    .......
    public void doSomething(){
        object=someOperation;
    }
    public Someclass getSomething(){
        return object;
    }
    ........
}
  

任何片段

public class yFragment extends fragment implements ....{
    Communicator communicator;
    public onCreate(...................){
        .........
        communicator=(Communicator)getActivity();
        .........
    }
    ......................................
    public someMethod(){
         .............
         communicator.doSomething();
         ............
    }
    ...........
}
  

任何其他片段甚至是嵌套片段

public class XFragment extends fragment implements ....{
        Communicator communicator;
        public onCreate(...................){
            .........
            communicator=(Communicator)getActivity();
            .........
        }
        ......................................
        public someMethod(){
             .............
             variable=communicator.getSomething();
             ............
        }
        ...........
    }

这对我有用我将“数据”发送到我的主要活动商店那里然后当我想要它时我用界面恢复那些数据。
PSDT:.......意味着一些参数或方法在这里