我正在努力组合选项卡式ActionBar和 fragment 。
我使用 ActionBarSherlock 显示三个独立的选项卡,我的问题仅与第一个选项卡有关。它应该以列表和详细 View 的组合显示项目(称为“优惠券”)。另外两个选项卡显示不同的内容,在这里不起作用。
我正在做的事情如下:
- 主要 Activity 为三个选项卡创建三个 fragment
- 选项卡 A(列表 fragment )填充并呈现其列表
- 对于列表上的点击,我在主 Activity 中编写了一个回调,用于接收点击项目的 id 并将列表 fragment 与选项卡上的详细信息 fragment 交换(至少应该这样做)
- 在该回调中,我创建了要显示的详细信息 fragment ,并尝试用它替换列表 fragment 。
不幸的是我没有超越:
java.lang.IllegalArgumentException: No view found for id 0x7f040027 for fragment CouponDetailFragment{44f4e310 #1 id=0x7f040027}
我在问题末尾附上了相关 fragment 。非常感谢任何帮助。
这些是相关 fragment :
public class MyCouponActivity extends SherlockFragmentActivity implements CouponListFragment.Callbacks
列表 fragment :
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:name="foo.bar.CouponListFragment" android:id="@+id/coupon_list" android:layout_width="match_parent" android:layout_height="match_parent" />
fragment 交换交易:
@Override
public void onItemSelected(final String id) {
final Bundle arguments = new Bundle();
arguments.putString(CouponDetailFragment.ARG_ITEM_ID, id);
final CouponDetailFragment fragment = new CouponDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().replace(R.id.coupon_list, fragment).commit();
}
最佳答案
如果您在 XML 中定义 fragment ,则它是固定的,并且您无法将其替换为 FragmentTransaction 中的另一个 fragment 。
您最好的选择是替换 <fragment />
与某种ViewGroup
。不要忘记,您必须使用 FragmentTransaction 在 onCreate(..)
中添加 CouponListFragment您的 Activity 。
关于android - ActionBar 选项卡 - 用详细信息 fragment 替换列表 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11782194/