Android:在父片段和子片段之间传递更新数据



如何将数据从"父"Fragment传递到"子"Fragment?我已经研究了如何在片段之间传递数据,但就我而言,我无法使用Activity.

在我的应用程序中,我有一个Activity,它通过单击Activity Listitem来创建FragmentFragment创建一个类,Fragment从由循环创建的数据创建类获取持久数据。创建 Fragment 后,它会创建多个子片段,每个子片段都有不同的视图,这些视图应显示更新的数据。

但我的问题是,我无法将数据从Parentfragment传递到Childfragment。我不确定这是一个通过的问题还是Childfragment的创造问题.

在我的例子中,"textValueView"和"mySeries"总是一个空值。 为什么?

父片段

public class ScreenSlidePagerActivity extends Fragment {

    Bundle bundle = new Bundle();
    public boolean stop = false;
    private ServerFragment newServer;
   //Pager Widget for animation handeling
    private ViewPager mPager;
   //Pager Adapter provides the pages to view pager widget
    private PagerAdapter mPagerAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        ViewGroup sliderFragmentView = (ViewGroup)   inflater.inflate(R.layout.fragment_screen_slide, container, false);
        ConnectAndReadTask myTask = new ConnectAndReadTask(ScreenSlidePagerActivity.this , "opc.tcp://10.0.2.2:52520" +
            " - " + "mySinus", new NodeId(Integer.parseInt("2"), "mySinus"));
        myTask.execute(new String[]{"opc.tcp://10.0.2.2:52520"});

        // Initiate a ViewPager and a PagerAdapter
        mPager = (ViewPager) sliderFragmentView.findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        return sliderFragmentView;
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch(position){
            case 0:
                return newServer.newInstance("url","url","url");
            case 1:
                return new GaugeFragment();
            case 2:
                return new GaugeFragment();
            default:
                return new GaugeFragment();
        }
    }
    @Override
    public int getCount() {
        return 3;
    }
}
@Override
public void onDestroy() {
    stop = true;
    super.onDestroy();
}
/**
 *
 * @param value
 */
public void setData(String value)
{
    Log.d("ScreenSlide.SetData", value);
   // mCallback.onValueSelected(value);#
  bundle.putString("value", value);
    ServerFragment.create(value);
    newServer.setArguments(bundle);
   //newServer.setData(value);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    newServer = new ServerFragment();
}
}

儿童片段

public class ServerFragment extends Fragment {
TextView urlTextView, nodeTextView, valueTextView;
public static String passValue;
//Graph
GraphView graphView;
public LineGraphSeries<DataPoint>mySeries;
private double graph2LastXValue = 5d;

public View myFragmentView;
public String url, namespace, nodeName;
public ServerFragment() {}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   myFragmentView  = inflater.inflate(R.layout.fragment_server, container, false);
    urlTextView = (TextView)myFragmentView.findViewById(R.id.urlTextView);
    nodeTextView = (TextView)myFragmentView.findViewById(R.id.nodeTextView);
    valueTextView = (TextView)myFragmentView.findViewById(R.id.valueTextView);

    //Graph
    graphView = (GraphView)myFragmentView.findViewById(R.id.graphView);
    mySeries = new LineGraphSeries<DataPoint>();
    mySeries.setColor(Color.RED);
    graphView.addSeries(mySeries);
    graphView.getViewport().setXAxisBoundsManual(true);
    graphView.getViewport().setMinX(0);
    graphView.getViewport().setMaxX(100);
    graphView.getViewport().setMinY(-20);
    graphView.getViewport().setMaxY(70);
    url = getArguments().getString("url");
    urlTextView.setText(getArguments().getString("url"));
    nodeTextView.setText(getArguments().getString("ns") + " | " + getArguments().getString("nodeName"));
    return myFragmentView;
}

public void setData() {

    Bundle bundle = this.getArguments();
    String value = bundle.getString("value");
    graph2LastXValue += 1d;
    try {
        mySeries.appendData(new DataPoint(graph2LastXValue, Double.parseDouble(value)),
                true, 200);
    }
    catch (Exception e)
    {
        Log.d("mySeries.appendData", e.getMessage());
    }
    try {
        valueTextView.setText(value);
    }
    catch (Exception e)
    {
        Log.d("valueTextView", e.getMessage());
    }


    Log.d("Serverfragment setData:" , value);
}

@Override
public void onStart() {
    super.onStart();
        setData();
}

@Override
public void onDestroy() {
    Log.d("ServerFragment", "disconnected");
    super.onDestroy();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public static ServerFragment newInstance(String url, String ns, String nodeName) {
    ServerFragment sf = new ServerFragment();
    Bundle b = new Bundle();
    b.putString("url", url);
    b.putString("ns", ns);
    b.putString("nodeName", nodeName);

    sf.setArguments(b);
    return sf;
}
}

您可以将要作为参数的数据添加到片段中。然后,您可以从父片段调用此方法。您需要做的就是获取您创建的片段的引用。

您可以使用 mPagerAdapter.getItem(int position) 循环访问您的片段;

您还可以在创建片段时向片段添加标识符,以便更轻松地区分它们。 例如,您可以执行new GaugeFragment(identifierString)而不是new GaugeFragment()。现在每个片段都可以告诉你关于它"是谁"的信息。您还可以拥有不同类型的片段。

    PagerAdapter mPagerAdapter;
    ScreenSlidePagerAdapter screenSlidePagerAdapter;
    screenSlidePagerAdapter =  new ScreenSlidePagerAdapter(getFragmentManager());
    mPagerAdapter = screenSlidePagerAdapter;

稍后当您要设置数据时:

    int count = screenSlidePagerAdapter .getCount();
    for(int i=0;i<count;i++){
        Fragment fragment = screenSlidePagerAdapter .getItem(i);
        if(fragment instanceof GaugeFragment){
            ((GaugeFrament)fragment).gaugeFragmentMethod(data);
            break;
        }
        else if(fragment instanceof OtherTypeOfFragment){
            ((OtherTypeOfFragment)fragment).otherTypeOfFragmentMethod(data);
            break;
        }
    }

通常的方法是将其从第一个片段传递到主机活动,然后将其调度到第二个片段。

我不知道为什么你不能这样做,但如果真的像你说的那样,你可以尝试使用杰克·沃顿的奥托巴士库,然后把你的两个片段都订阅到巴士上。

class Fragment1 { 
  @Override
    public void onResume() {
        super.onResume();
        mBus.register(this);
    }
    @Override
    public void onPause() {
        mBus.unregister(this);
        super.onPause();
    }
    //let's assume you need to do it it on button click
    @Override
    public void onClick(View v) {
      mBus.post(new SomeDataEvent())
    }
} 
//======================
class Fragment2 { 
  @Override
    public void onResume() {
        super.onResume();
        mBus.register(this);
    }
    @Override
    public void onPause() {
        mBus.unregister(this);
        super.onPause();
    }
    //here we define method to handle your data
    @Subscribe
    public void onSomeData(SomeDataEvent object) {
      //do something with it
    }
} 

在此处查看更多信息 http://square.github.io/otto/

相关内容

  • 没有找到相关文章

最新更新