【发布时间】:2015-12-29 19:30:05
【问题描述】:
我有一个FragmentAcivity 用于创建动态 UI。我希望能够根据 FragmentActivity 收到的意图,将仅包含文本的片段替换为可能必须在线发布内容的片段,例如 Twitter 或服务器。
我使用了一个开关(如下所示),Android Studio 告诉我它无法解析我的事务对象上的第二个 add()(我在其中添加 postFrag)方法。我错过了什么?如果使用开关是个坏主意,那还能怎么做呢?
'fragView` 是片段容器。
private void showContentInContainer() {
// Get intent to determine next action
int workType = getIntent().getExtras().getInt(MainActivity.TAG_FRAGWORK);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
// Add the appropriate frag
switch (workType) {
case MainActivity.VIEW_RIGHTS :
// TODO: Pass a rights fragment
RightsFragment rightsFrag = new RightsFragment();
transaction.add(R.id.fragView, rightsFrag);
break;
case MainActivity.VIEW_SRA :
// TODO: Pass a view SRA fragment
PostFragment postFrag = new PostFragment();
transaction.add(R.id.fragView, postFrag);
break;
default: break;
}
transaction.commit(); // publish changes
}
编辑:添加片段容器视图的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<fragment
android:id="@+id/fragView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<fragment
android:id="@+id/adFragment_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.frankbrenyahapps.citisafe.adFragment"
tools:layout="@layout/fragment_ad" />
</LinearLayout>
【问题讨论】:
标签: java android android-fragments android-studio