如何 send/receive SwipeView 中的 Fragments Activity
How to send/receive extras to Fragments in SwipeView Activity
我正在尝试在 Android 中使用带选项卡的滑动视图,我需要将一些变量传递给选项卡的片段以显示自定义内容,我的问题是我总是有捆绑包0 所以我认为我在从片段容器页面发送额外内容时做错了。这是包含选项卡的片段和主片段页面的代码。
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
prof = getIntent().getIntExtra("prof", 0);
id = getIntent().getIntExtra("id", 0);
bund = new Bundle();
bund.putInt("prof", prof);
bund.putInt("id", id);
switch (position) {
case 0:
// Text fragment activity
FragmentOne fragment = new FragmentOne();
fragment.setArguments(bund);
return fragment;
case 1:
// Gallery fragment activity
FragmentTwo fragment2 = new FragmentTwo();
fragment2.setArguments(bund);
return fragment2;
case 2:
//Audio fragment
FragmentThree fragment3 = new FragmentThree();
fragment3.setArguments(bund);
return fragment3;
}
return null;
}
而片段代码放在onCreateView方法中
Bundle bundle = getArguments();
if (bundle!=null){
prof = bundle.getInt("prof");
id = bundle.getInt("id");
}
我可以读取进入片段容器的参数,但无法读取进入片段的参数,我忘记了什么?
您可以在此处尝试使用 SharedPreference
而不是 Bundle
:
在 FragmentStatePagerAdapter 中实例化 SharedPreference :
SharedPreference mPref = mContext.getSharedPreferences("myPreference",
Context.MODE_PRIVATE);
存储 prof
和 id
到 mPref
:
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
prof = getIntent().getIntExtra("prof", 0);
id = getIntent().getIntExtra("id", 0);
// store value in mPref
mPref.putInt("prof", prof);
mPref.putInt("id", id);
// commit changes
mPref.commit();
switch (position) {
case 0:
// Text fragment activity
FragmentOne fragment = new FragmentOne();
return fragment;
case 1:
// Gallery fragment activity
FragmentTwo fragment2 = new FragmentTwo();
return fragment2;
case 2:
//Audio fragment
FragmentThree fragment3 = new FragmentThree();
return fragment3;
}
return null;
}
检索任何片段 onCreateView()
内的值:
SharedPreference mPrefFrag = getActivity().getSharedPreferences("myPreference",
Context.MODE_PRIVATE);
int prof = mPrefFrag.getInt("prof",0);
int id = mPrefFrag.getInt("id",0);
我正在尝试在 Android 中使用带选项卡的滑动视图,我需要将一些变量传递给选项卡的片段以显示自定义内容,我的问题是我总是有捆绑包0 所以我认为我在从片段容器页面发送额外内容时做错了。这是包含选项卡的片段和主片段页面的代码。
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
prof = getIntent().getIntExtra("prof", 0);
id = getIntent().getIntExtra("id", 0);
bund = new Bundle();
bund.putInt("prof", prof);
bund.putInt("id", id);
switch (position) {
case 0:
// Text fragment activity
FragmentOne fragment = new FragmentOne();
fragment.setArguments(bund);
return fragment;
case 1:
// Gallery fragment activity
FragmentTwo fragment2 = new FragmentTwo();
fragment2.setArguments(bund);
return fragment2;
case 2:
//Audio fragment
FragmentThree fragment3 = new FragmentThree();
fragment3.setArguments(bund);
return fragment3;
}
return null;
}
而片段代码放在onCreateView方法中
Bundle bundle = getArguments();
if (bundle!=null){
prof = bundle.getInt("prof");
id = bundle.getInt("id");
}
我可以读取进入片段容器的参数,但无法读取进入片段的参数,我忘记了什么?
您可以在此处尝试使用 SharedPreference
而不是 Bundle
:
在 FragmentStatePagerAdapter 中实例化 SharedPreference :
SharedPreference mPref = mContext.getSharedPreferences("myPreference",
Context.MODE_PRIVATE);
存储 prof
和 id
到 mPref
:
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
prof = getIntent().getIntExtra("prof", 0);
id = getIntent().getIntExtra("id", 0);
// store value in mPref
mPref.putInt("prof", prof);
mPref.putInt("id", id);
// commit changes
mPref.commit();
switch (position) {
case 0:
// Text fragment activity
FragmentOne fragment = new FragmentOne();
return fragment;
case 1:
// Gallery fragment activity
FragmentTwo fragment2 = new FragmentTwo();
return fragment2;
case 2:
//Audio fragment
FragmentThree fragment3 = new FragmentThree();
return fragment3;
}
return null;
}
检索任何片段 onCreateView()
内的值:
SharedPreference mPrefFrag = getActivity().getSharedPreferences("myPreference",
Context.MODE_PRIVATE);
int prof = mPrefFrag.getInt("prof",0);
int id = mPrefFrag.getInt("id",0);