【发布时间】:2016-01-21 12:43:46
【问题描述】:
我正在为PagerAdapter编写以下代码
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragment_edit_delivery_location = new Fragment_edit_delivery_location_Map();
Bundle arg = new Bundle();
arg.putString("delivery_to_title_map", "Map Location");
fragment_edit_delivery_location.setArguments(arg);
return fragment_edit_delivery_location;
case 1:
fragment_edit_delivery_locationHome = new Fragment_edit_delivery_locationHome();
Bundle arg1 = new Bundle();
arg1.putString("delivery_to_title_home", "Home");
fragment_edit_delivery_location.setArguments(arg1);
return fragment_edit_delivery_location;
}
}
我收到错误作为错误 java.lang.IllegalStateException: Can't change tag of fragment Fragment_edit_delivery_location_Map
适配器的构造函数
public ActionTabAdapter( FragmentManager fm) {
super(fm);
}
我在 MainActivity 中使用 ViewPager 作为:
adapter = new ActionTabAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
我正在尝试从当前 Fragment 获取动态值,并再次在 MainActivity 中使用以将字符串值作为意图从 MainActivity 发送到 SecondActivity 。 ....但也无法从 Current Fragment 获得准确的价值 .....感谢您提供任何帮助谢谢
【问题讨论】:
-
思考 MVC 模式。构建一个自定义适配器并将模型对象传递给它,该对象包含片段中显示的值。使片段将更改写回模型对象。由于活动首先创建了适配器,因此它也可以访问模型对象。 不要试图“跨”视图传递信息,你会受到伤害。
-
对片段使用静态工厂方法,而不是在客户端构建它们。它也是一种添加参数的简单机制。
标签: java android android-activity android-viewpager fragmentpageradapter