How to pass a string between fragments in a viewpager(如何在 viewpager 中的片段之间传递字符串)
问题描述
我试图在 viewpager 中的两个片段之间传递一个字符串,但我没有找到正确的方法.到目前为止,这是我的代码:
I'm trying to pass a string between two fragments in a viewpager but I didn't found the right way to do it. Here is my code so far :
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
final int PAGE_COUNT = 6;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new MyFragment();
case 1:
return new part2();
case 2:
return new Part3();
case 3:
return new Part4();
case 4:
return new Part5();
case 5:
return new Part6();
default:
return null;
}
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page #" + ( position + 1 );
}
}
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
我没有在 viewpager 中使用标签.我应该在片段中写什么?
I'm not using tabs with the viewpager. What should I write in the fragments?
更新:我尝试实现答案中建议的接口:
UPDATE : I tried to implement an interface like suggested in the answers :
public interface OnTogglePressListener {
public void onTogglePressed(String msg);
}
在第一个片段上:
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
try {
buttonListener = (OnTogglePressListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onTogglePressed");
}
super.onAttach(activity);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
buttonListener.onTogglePressed("Off");
... }
关于第二个片段:
public String getEtat(String etat)
{ state =etat;
return etat;
}
关于主要活动:
public void onTogglePressed(String etat)
{
Part5 p = (Part5)getSupportFragmentManager().findFragmentById(R.layout.frag_2);
p.getEtat(etat);
}
但我每次都收到 NullPointerException.
But I got a NullPointerException everytime.
推荐答案
所以我找到了解决方案,实现是正确的,我唯一应该在第二个片段的方法中做我想要的处理,就是这样!
So i found the solution, the implementation is correct, the only thing that I should do the treatement I want in the method on the second fragment and that's it !
这篇关于如何在 viewpager 中的片段之间传递字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 viewpager 中的片段之间传递字符串
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01