Android 将ArrayList传递给片段
我有3个ArrayList>要传递给3个片段。除了使它们保持静态外,最好的方法是什么?您可以使用片段中的setArguments。请看一看,基本上,您在创建片段之前创建了一个Bundle,然后将其设置为参数 Android文档中的示例:Android 将ArrayList传递给片段,android,android-fragments,Android,Android Fragments,我有3个ArrayList>要传递给3个片段。除了使它们保持静态外,最好的方法是什么?您可以使用片段中的setArguments。请看一看,基本上,您在创建片段之前创建了一个Bundle,然后将其设置为参数 Android文档中的示例: public static class DetailsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {
public static class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line with the list so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
}
不使用getIntent.getExtras,而是创建绑定并设置参数
Bundle bundle = new Bundle();
bundle.putSerializable(YOUR_KEY, yourObject);
fragment.setArguments(bundle);
对于你的片段:
public static class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
return scroller;
}
}
您可以在片段中使用setArguments。请看一看,基本上,您在创建片段之前创建了一个Bundle,然后将其设置为参数 Android文档中的示例:
public static class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line with the list so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
}
不使用getIntent.getExtras,而是创建绑定并设置参数
Bundle bundle = new Bundle();
bundle.putSerializable(YOUR_KEY, yourObject);
fragment.setArguments(bundle);
对于你的片段:
public static class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
return scroller;
}
}
您可以创建侦听器回调接口并在片段中实现它们。大概是这样的:
@Override
public void onSomeEvent(List<SomeData> data) {
//do something with data
}
然后,您可以调用该接口的方法,您的片段将收到回调
片段和活动之间的第二种也是更简单的通信方式是广播接收者。您可以在片段中注册一些BroadcastReceiver,然后从activity调用sendBroadcast。您的数据列表可以放在广播消息的捆绑包中。您可以创建侦听器回调接口,并在片段中实现它们。大概是这样的:
@Override
public void onSomeEvent(List<SomeData> data) {
//do something with data
}
然后,您可以调用该接口的方法,您的片段将收到回调
片段和活动之间的第二种也是更简单的通信方式是广播接收者。您可以在片段中注册一些BroadcastReceiver,然后从activity调用sendBroadcast。您的数据列表可以放在该广播消息的捆绑包中