[英]How pass fragment arguments while using a view pager with different fragments/layouts?
Objective : To use fragment arguments to pass along the string value from a TextView
to a new fragments TextView
, BUT while using a ViewPager
with different layouts/fragments
in the FragmentPagerAdapter
. 目标 :使用片段参数将字符串值从
TextView
传递到新片段TextView
,但在FragmentPagerAdapter
使用具有不同layouts/fragments
的ViewPager
时。
Problem : The new fragment never receives the fragments arguments from the previous fragment. 问题 :新片段永远不会从前一个片段接收片段参数。
My Set up : I have my Activity
hosting the ViewPager
and FragmentPagerAdapter
. 我的设置 :我的
Activity
托管了ViewPager
和FragmentPagerAdapter
。 I have overridden FragmentPagerAdapters
getItem(int position)
method to create a new fragment
instance depending on the current position of the ViewPager
. 我重写了
FragmentPagerAdapters
getItem(int position)
方法,根据ViewPager
的当前位置创建一个新的fragment
实例。 Right now I only have two Fragments
in my adapter, but will be adding more after getting over this obstacle. 现在我的适配器中只有两个
Fragments
,但是在克服这个障碍后会添加更多Fragments
。 I am using the newInstance()
technique to pass along the Fragment
's arguments, but nothing is ever passed. 我使用
newInstance()
技术传递Fragment
的参数,但没有传递任何东西。
Pertinent Code : 相关代码 :
My FragmentPagerAdapter code: 我的FragmentPagerAdapter代码:
//this is a variable that I pass in as the newInstanct() methods parameter,
// I update this variable from my fragments constantly
public static String fragmentArgumentsValue = "";
mViewPager.setAdapter(new FragmentPagerAdapter(fm) {
@Override
public int getCount() {
return NUMBER_OF_VIEWS;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Log.d(TAG, "---In getPosition(), position 0---");
return Frag1
.newInstance(fragmentArgumentsValue);
case 1:
Log.d(TAG, "---In getPosition(), position 1---");
return frag2
.newInstance(fragmentArgumentsValue);
default:
Log.d(TAG, "---In getPosition(), DEFAULT---");
return frag1
.newInstance(fragmentArgumentsValue);
}
}
});
One of the fragments newInstance() method: 其中一个片段newInstance()方法:
public static Fragment newInstance(String fragmentArgumentsValue) {
Frag1 f = new Frag1();
Bundle bun = new Bundle();
bun.putString(KEY_FRAGMENT_ARGUMENTS_STRING, fragmentArgumentsValue);
f.setArguments(bun);
return f;
}
Getting the fragments arguments: 获取片段参数:
String argString = getArguments().getString(
KEY_FRAGMENT_ARGUMENTS_STRING);
if (argString.length() != 0) {
Log.d(TAG, "Trying to set the frag args to:" + argString);
mWorkingTextView.setText("" + argString);
} else {
Log.d(TAG, "Couldn't set frag args to: " + argString);
}
What I've Tried : I've tried giving the Activity
that hosts the ViewPager
and FragmentPagerAdapter
a static
variable that I constantly update in each one of my fragments
, I include the static
variable in the fragment.newInstance(staticVariableInActivity)
method, but this doesn't seem to work. 我试过的 :我试过给
ViewPager
托管ViewPager
和FragmentPagerAdapter
的Activity
一个static
变量,我在每个fragments
不断更新,我在fragment.newInstance(staticVariableInActivity)
方法中包含static
变量,但是这个似乎不起作用。 I've also tried using the ViewPager
callback viewPager.setOnPageChangeListener()
I had overridden the onPageSelected(int pos)
and tried to pass the fragment arguments there, nevertheless it didn't work... so please help me SO!!! 我也试过使用
ViewPager
回调viewPager.setOnPageChangeListener()
我已经覆盖了onPageSelected(int pos)
并尝试在那里传递片段参数,但它没有用......所以请帮助我!
My thoughts : I do not have the different fragments
and layouts
in an ArrayList
or any list for that matter, I just instantiate the Fragments
via the newInstance()
method depending on the positions of the FragementPagerAdapter
, could this be a problem? 我的想法 :我没有在
ArrayList
或任何列表中有不同的fragments
和layouts
,我只是通过newInstance()
方法实例化Fragments
,具体取决于FragementPagerAdapter
的位置,这可能是个问题吗? Should I create a singleton
list of the layouts/fragments
? 我应该创建
layouts/fragments
的singleton
列表吗? So I can change the values of the Fragments
TextViews
via the singleton
list? 所以我可以通过
singleton
列表更改Fragments
TextViews
的值? (excuse me if that's a dumb or not possible thing to do). (对不起,如果这是一个愚蠢或不可能的事情)。
Note : I am am saving away the TextViews values via public void onSaveInstanceState(Bundle outState)
to handle screen rotations. 注意 :我正在通过
public void onSaveInstanceState(Bundle outState)
保存TextViews值以处理屏幕旋转。 Could this be a problem? 这可能是个问题吗?
Alright so I this link will help in the communication between fragments: Communication with other Fragments . 好吧,所以我这个链接将有助于片段之间的沟通: 与其他片段的沟通 。
You need to define a interface that the Activity will implement and the fragments will use to send data onward to the activity, and the activity will then find the fragment by tag doing something like this: 您需要定义一个Activity将实现的接口,并且片段将用于向前发送数据到活动,然后活动将按标签找到片段,执行以下操作:
frag = (Fragment2) getSupportFragmentManager()
.findFragmentByTag(
"android:switcher:" + R.id.viewPager + ":2");
and then update the fragment by calling a public method implemented within the fragment. 然后通过调用片段中实现的公共方法来更新片段。
The link provided will help greatly, it is from the Android Development website. 提供的链接将有很大帮助,它来自Android开发网站。
Adds the bundle inside your Adapter. 在适配器中添加捆绑包。
example in the constructor of the adapter : 适配器的构造函数中的示例:
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
fragments.add(TileFragments.newInstance());
Bundle bundleFeatures = new Bundle();
bundleFeatures.putString(ContentName.FEATURES.toString(),ContentName.FEATURES.toString());
fragments.get(0).setArguments(bundleFeatures);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.