Android:在父片段和子片段之间传递更新数据
Android: Passing updating Data between Parent and Child-Fragment
如何将数据从 "Parent" Fragment
传递到 "Child" Fragment
?。我已经看过如何在片段之间传递数据,但在我的例子中我不能使用 Activity
.
在我的应用程序中,我有一个 Activity
,它通过单击 Activity
的 Listitem
创建一个 Fragment
。 Fragment
创建一个 class 并且 Fragment
从 Data-Creating-Class 获取持久数据,这是通过循环创建的。在 Fragment
创建后,它创建了几个 Child-Fragments,每个都有不同的视图,应该显示更新后的数据。
但我的问题是,我无法将数据从 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;
}
}
通常的方法是将它从第一个 Fragment 传递到主机 Activity,然后它会将它分派到第二个 Fragment。
我不知道你为什么不能那样做,但如果真的像你说的那样,你可以尝试使用 Jake Warton 的 Otto Bus 库,然后将你的两个片段都订阅到总线。
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/
您可以将 public 方法添加到您的片段中,并将您想要的数据作为参数。然后你可以从父片段调用这个方法。您需要做的就是获取您创建的片段的引用。
您可以使用 mPagerAdapter.getItem(int position);
遍历片段
您还可以在创建片段时为其添加标识符,以便更轻松地区分它们。例如,您可以 new GaugeFragment(identifierString)
而不是 new GaugeFragment()
。现在每个片段都可以告诉您有关 "who" 的信息。您还可以有不同类型的片段。
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;
}
}
如何将数据从 "Parent" Fragment
传递到 "Child" Fragment
?。我已经看过如何在片段之间传递数据,但在我的例子中我不能使用 Activity
.
在我的应用程序中,我有一个 Activity
,它通过单击 Activity
的 Listitem
创建一个 Fragment
。 Fragment
创建一个 class 并且 Fragment
从 Data-Creating-Class 获取持久数据,这是通过循环创建的。在 Fragment
创建后,它创建了几个 Child-Fragments,每个都有不同的视图,应该显示更新后的数据。
但我的问题是,我无法将数据从 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;
}
}
通常的方法是将它从第一个 Fragment 传递到主机 Activity,然后它会将它分派到第二个 Fragment。
我不知道你为什么不能那样做,但如果真的像你说的那样,你可以尝试使用 Jake Warton 的 Otto Bus 库,然后将你的两个片段都订阅到总线。
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/
您可以将 public 方法添加到您的片段中,并将您想要的数据作为参数。然后你可以从父片段调用这个方法。您需要做的就是获取您创建的片段的引用。
您可以使用 mPagerAdapter.getItem(int position);
遍历片段您还可以在创建片段时为其添加标识符,以便更轻松地区分它们。例如,您可以 new GaugeFragment(identifierString)
而不是 new GaugeFragment()
。现在每个片段都可以告诉您有关 "who" 的信息。您还可以有不同类型的片段。
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;
}
}