【问题标题】:ActionBar tab - replace list fragment with detail fragmentActionBar 选项卡 - 用详细信息片段替换列表片段
【发布时间】:2012-08-02 16:51:53
【问题描述】:

我正在努力将 标签式 ActionBar片段 结合起来。

我正在使用 ActionBarSherlock 显示三个独立的选项卡,而我的问题仅与第一个选项卡有关。它应该以列表和详细视图的组合显示项目(称为“优惠券”)。其他两个选项卡显示不同的内容,在这里不发挥作用。

我正在做的事情如下:

  • 主要活动为三个选项卡创建三个片段
  • 选项卡 A(列表片段)填充并呈现其列表
  • 对于列表上的点击,我在主活动中编写了一个回调,该回调接收点击项目的 id 并将列表片段与选项卡上的详细片段交换(至少它应该这样做)
  • 在该回调中,我创建了要显示的详细信息片段并尝试用它替换列表片段。

不幸的是我没有超越:

java.lang.IllegalArgumentException:未找到 id 0x7f040027 的视图 对于片段 CouponDetailFragment{44f4e310 #1 id=0x7f040027}

我在问题的末尾附上了相关的 sn-ps。非常感谢任何帮助。

这些是相关的sn-ps:

public class MyCouponActivity extends SherlockFragmentActivity implements CouponListFragment.Callbacks

列表片段:

<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" />

片段交换交易:

@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();
}

【问题讨论】:

    标签: android android-fragments android-actionbar actionbarsherlock


    【解决方案1】:

    如果你在 XML 中定义一个片段,它是固定的,你不能用 FragmentTransaction 中的另一个片段替换它。

    最好的办法是将&lt;fragment /&gt; 替换为某种ViewGroup。不要忘记,您必须使用 FragmentTransaction 在 Activity 的 onCreate(..) 中添加 CouponListFragment。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      相关资源
      最近更新 更多