重新加载片段不起作用

Posted

技术标签:

【中文标题】重新加载片段不起作用【英文标题】:Reload the Fragment is not Working 【发布时间】:2015-06-20 08:07:09 【问题描述】:

我有以下情况: NavigationDrawer 创建一个 Fragment 来处理用户信息。 onCreateView() 上的片段调用 Web 服务并返回视图。当 Web 服务完成回调分离和附加自身的片段时。当第二次在 onCreateView() 上附加 Fragment 时,应该创建一个包含用户信息的视图。但实际上发生的事情是 View 没有改变。

我错过了什么?

提前谢谢你,

片段代码:

private boolean mIsRefresh = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 

    // Gets Application Context
    mContext = getActivity().getApplicationContext();

    // Set up environment to use DataBase
    mGlobal = (Global) getActivity().getApplicationContext();
    mDAOFactory = mGlobal.getDAOFactory();

    mFragment = this;

    mFragmentView = inflater.inflate(R.layout.fragment_user_info, container, false);


    buildView();

    if (mIsRefresh) 
        return mFragmentView;
    else 
        return null;
    

这里是 buildView() 代码:

private void buildView() 

    mEtUserName = (EditText) mFragmentView.findViewById(R.id.userName);
    mEtUserEmail = (EditText) mFragmentView.findViewById(R.id.userEmail);
    mEtPassword = (EditText) mFragmentView.findViewById(R.id.password);
    mEtPasswordConfirmation = (EditText) mFragmentView.findViewById(R.id.passwordConfirmation);
    mIbSendPassword = (ImageButton) mFragmentView.findViewById(R.id.sendPassword);
    mAcUserCountry = (AutoCompleteTextView) mFragmentView.findViewById(R.id.userCountry);
    mEtUserState = (EditText) mFragmentView.findViewById(R.id.userState);
    mEtUserCity = (EditText) mFragmentView.findViewById(R.id.userCity);
    mEtUserPhoneNumberCountry = (EditText) mFragmentView.findViewById(R.id.userPhoneNumberCountry);
    mEtUserPhoneNumberCity = (EditText) mFragmentView.findViewById(R.id.userPhoneNumberCity);
    mEtUserPhoneNumber = (EditText) mFragmentView.findViewById(R.id.userPhoneNumber);
    mCbTCAccepted = (CheckBox) mFragmentView.findViewById(R.id.cbTCAccepted);


    // User has not been created
    if (mGlobal.getUserUID() == 0) 
        getUserEmail();
        getUserLocation();

     else 
        if (mIsRefresh) 
            formatView();
         else 
            getDataOnServer();
        
    


这里是调用 Web Service 的代码:

private void getDataOnServer () 

    mUserDAO = mDAOFactory.getUserDAO(mContext);

    final ProgressDialog progressDialog = ProgressDialog.show (getActivity(),
            getString(R.string.msgConnectingServer), getString(R.string.msgGettingData), true);

    //TODO Retirar o set abaixo
    mGlobal.setUserUID(1);

    mUserDAO.getUserByUID(mGlobal.getUserUID(), new Callback<User>() 
        @Override
        public void success(User user, Response response) 
            mUser = user;
            mIsRefresh = true;
            refresh();
        

        @Override
        public void failure(RetrofitError error) 
            mUser = null;
            Log.e(getClass().getName(), "Retrofit Error = " + error);
            refresh();
        

        private void refresh() 
            progressDialog.dismiss();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.detach(mFragment)
                    .attach(mFragment)
                    .commit();

        
    );

最后是 onStart()

@Override
public void onStart() 
    super.onStart();

    if (mIsRefresh) 
        mIsRefresh = false;
        getFragmentManager().beginTransaction()
                .remove(mFragment)
                .add(R.id.content_frame, mFragment)
                .commit();
    


【问题讨论】:

【参考方案1】:

代替

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.detach(mFragment).attach(mFragment).commit();

这样做:

    YourFragment mFragment = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG"); //if you are using support library
     //OR
   YourFragment mFragment = getFragmentManager().findFragmentByTag("Your_Fragment_TAG"); //if you are using support library

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.detach(mFragment);
    ft.attach(mFragment).commit();

【讨论】:

以上是关于重新加载片段不起作用的主要内容,如果未能解决你的问题,请参考以下文章

导航 PopUpToInclusive 不起作用?重新打开应用程序时,它会打开登录片段而不是电子邮件登录片段

我想从片段中隐藏片段容器视图(在 MainActivity 布局内),但是当我单击任务按钮然后重新打开应用程序时它不起作用

使用 AJAX 重新加载部分视图不起作用

自定义 CollectionView 布局重新加载数据不起作用

重新加载的 Spark 模型似乎不起作用

Javascript代码片段在drupal中不起作用