繁体   English   中英

android-更新和提交事务后未调用片段onCreateView

[英]android - Fragment onCreateView not called after update and commiting transaction

更改数据后,我无法获取要更新的片段。 我打电话

getFragmentManager()。beginTransaction()。detach(this).attach(this).commit();

但不会调用onCreateView。 我在refreshFragment()中尝试了几种不同的方法。 我在上面的代码行中输入了“ I / InjectionManager:dispatchCreateOptionsMenu”的消息后,确实发生了某些情况,但未调用onCreateView()。

 

public class MainActivityFragment extends Fragment { private static Logger logger = Logger.getLogger(MainActivityFragment.class); String poolWord = "ABCDE"; String newWord = ""; public MainActivityFragment() { } @Override public void onCreate(Bundle savedInstanceState) { logger.info("onCreate------------------------"); super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { logger.info("onCreateView------------------------"); // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_main, container, false); GridView gridViewPool = (GridView) view.findViewById(R.id.gridviewPool); gridViewPool.setAdapter(new LetterImageAdapter(getActivity(), poolWord)); gridViewPool.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { logger.error("po " + position); updateWords(position); refreshFragment(); } }); GridView gridviewNewWord = (GridView) view.findViewById(R.id.gridviewNewWord); gridviewNewWord.setAdapter(new LetterImageAdapter(getActivity(), "")); return view; } private void refreshFragment() { logger.info("refreshFragment------------------------"); getFragmentManager().beginTransaction().detach(this).attach(this).commit(); // getFragmentManager().beginTransaction().replace(R.id.fragment, this).attach(this).commit(); // Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment); //getFragmentManager().beginTransaction().detach(fragment).attach(fragment).commit(); getFragmentManager().executePendingTransactions(); }

几个小时后,我开始工作了。 不确定所有细节,因为这是我的第一个android项目。 如果其他人有同样的问题,请发布答案。

我怀疑问题的症结在于,Intellij生成的代码没有在Activity类中启动事务。 它不包括

 if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MainActivityFragment())
                .commit();
    }

我不知道如何创建MainActivityFragment而不使用它,但是确实如此。我还用教程中的内容替换了生成的activity-main.xml。 Intellij的一个没有可用于容器对象的android:id。 它还包含了我此时不需要的其他内容。新的activity-main.xml如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
         android:layout_width="match_parent" android:layout_height="match_parent"
         tools:context=".MainActivity" tools:ignore="MergeRootFrame"/>

旧生成的是:

<android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main"/>

</android.support.design.widget.CoordinatorLayout>

还有一个我删除的content-main.xml。 这可能就是android用来在没有显式代码的情况下生成我的MainActivityFragment的原因。 对Android开发更有经验的人也许可以回答这个问题。

  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/fragment"
      android:name="com.ultramondo.mywordandroid.MainActivityFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:layout="@layout/fragment_main">

  </fragment>

我敢肯定,我可以回溯一些有用的东西,但是现在我想要简单。 我还必须进行一些更改,才能将android.support.v4.app包用于Fragment和Transaction类。 就像进入电影中间并弄清楚剧情是什么。 一旦看到整部电影,我相信它将变得更加清晰。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM