gpt4 book ai didi

java - Android:在父 fragment 和子 fragment 之间传递更新数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:44 25 4
gpt4 key购买 nike

如何将数据从“父”Fragment 传递到“子”Fragment?我已经了解了如何在 fragment 之间传递数据,但就我而言,我无法使用 Activity

在我的应用程序中,我有一个 Activity,它通过单击 ActivityListitem 创建一个 FragmentFragment 创建一个类,Fragment 从 Data-Creating-Class(通过循环创建)获取持久数据。创建Fragment后,它会创建多个Child-Fragment,每个都有不同的View,这些View应该显示更新的数据。

但我的问题是,我无法将数据从 Parentfragment 传递到 Childfragment。我不确定它是 Childfragment 的传递问题还是创建问题。

在我的例子中,“textValueView”和“mySeries”始终为空。为什么?

父 fragment

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();
}
}

child fragment

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;
}

}

最佳答案

您可以使用所需的数据作为参数向 fragment 添加公共(public)方法。然后您可以从父 fragment 调用此方法。您需要做的就是获取您创建的 fragment 的引用。

您可以使用 mPagerAdapter.getItem(intposition); 循环遍历 fragment

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

    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;
}
}

关于java - Android:在父 fragment 和子 fragment 之间传递更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36061207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com