【问题标题】:FragmentTransaction.replace() not workingFragmentTransaction.replace() 不工作
【发布时间】:2012-11-19 02:37:17
【问题描述】:

我有一个ListFragment 显示用户创建的项目列表。我有一个ListView,其 id 设置为“@android:id/list”,一个TextView 其 id 为“@android:id/empty”。

我有一个操作栏按钮来显示一个片段,以允许用户为列表创建更多条目。这是onOptionsItemSelected() 方法:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Fragment frag = null;

    // Right now I only have code for the addCourse() fragment, will add more soon
    switch (item.getItemId()) {
    case R.id.addCourse:
        frag = new AddCourseFragment();
        break;
    default:
        return super.onOptionsItemSelected(item);
    }

    // The support library is being dumb and replace isn't actually 
    getFragmentManager().beginTransaction()
        .remove(this).add(getId(), frag).addToBackStack(null).commit();
    return true;

}

AddCourseFragment 代码如下:

public class AddCourseFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_addcourse, container, false);
//      if (view.findViewById(R.id.timesFrame)!=null) {
        Fragment timesFrag = new TimesFragment();
        getChildFragmentManager().beginTransaction()
            .add(R.id.timesFrame, timesFrag).commit();
//      }
    return view;
    }
}

正如所料,当列表未填充时,android 会显示TextView 中的文本。然后我点击添加课程按钮,这会发生:
它显示空文本以及新片段。

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    我遇到了类似的问题。

    根据this,当您需要以编程方式添加Fragments时,您应该将它们添加到现有的ViewGroup

    所以我从 xml 中删除了Fragment,并在replace() 方法中使用了FrameLayout 组件。

    你也可以看看here

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      在我的案例中解决问题的另一个问题是不同的 API 导入。

      请确保您使用“support.v4”或 API 11 中的“原始”实现,并且不要混合使用它们。 (这可能发生,在我的情况下:TransactionManager 是 v4,而 Activity 是旧版本,或者相反)

      【讨论】:

        【解决方案3】:

        为 childFragment 的主布局赋予背景颜色。

        喜欢 android:background="#FFFFFF"

        这对我有用!!!!

        【讨论】:

        • 视图还在,只是看不到。
        【解决方案4】:

        另一种解决方案是使用 LinearLayout 代替片段

         <Fragment
                android:name="path/to.package.MyClass"
                android:id="@+id/fragmentContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="100dp"
                 />
        

        像这样使用它

         <LinearLayout
                android:name="path/to.package.MyClass"
                android:id="@+id/fragmentContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="100dp"
                 />
        

        【讨论】:

          【解决方案5】:

          我解决了更改 LinearLayout 的 RelativeLayout...希望它有所帮助!

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/windowBackground">
          
          
              <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/fragment_container"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" />
          
          
          </LinearLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-01-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-22
            • 2015-09-13
            • 1970-01-01
            相关资源
            最近更新 更多