Android - 使用 FragmentActivity + Loader 更新 FragmentStatePagerAdapter 的问题

Android - problems using FragmentActivity + Loader to update FragmentStatePagerAdapter(Android - 使用 FragmentActivity + Loader 更新 FragmentStatePagerAdapter 的问题)
本文介绍了Android - 使用 FragmentActivity + Loader 更新 FragmentStatePagerAdapter 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 FragmentActivityViewPager 来显示 Fragments 的动态列表.有很多关于如何制作它的静态版本的例子.我的问题是我显示的列表需要动态加载,并且可以根据用户输入(添加/删除)进行更改.我正在尝试使用自定义的 android.support.v4.content.Loader 来加载可用于构建列表的数据集.

I'm trying to use FragmentActivity with ViewPager to display dynamic list of Fragments. There are plenty of examples on how to do a static version of it. My problem is that the list I display needs to be loaded dynamically and also can change based on user input (add/delete). I'm trying to use customized android.support.v4.content.Loader to load set of data that I can use to build my list.

在我的设置中一切正常,直到我想要更新适配器并发出 FragmentStatePagerAdapter#notifyDataSetChanged() 调用,此时执行此代码(来自 FragmentStatePagerAdapter)

Everything goes fine in my setup until I get to the point when I want to update the adapter and issue FragmentStatePagerAdapter#notifyDataSetChanged() call at which point this code is executed (from FragmentStatePagerAdapter)

public void finishUpdate(View container)
{
    if(mCurTransaction != null)
    {
        mCurTransaction.commit(); // BANG!!! The exception is thrown
        mCurTransaction = null;
        mFragmentManager.executePendingTransactions();
    }
}

事务提交失败并显示以下消息:

The transaction commit fails with this message:

java.lang.IllegalStateException: 无法在 onLoadFinished 内部执行此操作

因为在 FragmentManagerImpl 中执行了这段代码:

because within FragmentManagerImpl this code is executed:

private void checkStateLoss() {
    if (mStateSaved) {
        throw new IllegalStateException(
                "Can not perform this action after onSaveInstanceState");
    }
    if (mNoTransactionsBecause != null) {
        throw new IllegalStateException(
                "Can not perform this action inside of " + mNoTransactionsBecause);
    }
}

原来mNoTransactionsBecause值不为null,返回结果返回onLoadFinished时在LoaderManagerImpl.LoaderInfo中设置mNoTransactionsBecause

It turns out that mNoTransactionsBecause value is not null and it is set in LoaderManagerImpl.LoaderInfo when the result is returned back to onLoadFinished

我正在查看各种变量,试图以某种方式将 tramsaction.commit() 更改为 transaction.commitAllowingStateLoss() 但与交易相关的所有内容似乎都是私有的或在最少的包保护.

I was looking at the various variables trying to somehow change tramsaction.commit() to transaction.commitAllowingStateLoss() but everything related to transaction seems to be private or at least package-protected.

如果我可以做我需要做的事情(以及如何做),有人可以给我一个大致的想法吗?

Can someone give me a general idea if I can do what I need to do (and how)?

请注意,如果我在 AsyncTask 中运行加载操作而不是使用 Loader,我的代码可以正常工作

Just to note that my code works fine if instead of using Loader I run the loading ops in AsyncTask

推荐答案

我有一个非常相似的问题,并通过在 Fragment 上使用处理程序解决了它.

I had a very similar problem to this and solved it by using a handler on the Fragment.

我想在 onLoadFinished() 上显示一个警告对话框,类似于以下内容

I wanted to show an alert dialog on onLoadFinished(), similar to the following

public class MyFragment extends Fragment 
    implements LoaderManager.LoaderCallbacks<T> {

    public void onLoadFinished(Loader<T> loader, T data) {
        showDialog();
    }

    public void showDialog() {
        MyAlertDialogFragment fragment = new MyAlertDialogFragment();
        fragment.show(getActivity().getSupportFragmentManager(), "dialog");

    }
}

但这给出了一个错误

无法在 onLoadFinished 内执行此操作

解决方案是向片段添加一个处理程序以处理显示对话框

The solution was to add a handler to the fragment to take care of showing the dialog

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if(msg.what == MSG_SHOW_DIALOG) {
            showDialog();
        }
    }
};

然后修改 onLoadFinished(...) 改为向处理程序发送消息

And then modify onLoadFinished(...) to send a message to the handler instead

   public void onLoadFinished(Loader<T> loader, T data) {
        handler.sendEmptyMessage(MSG_SHOW_DIALOG);
    }

这篇关于Android - 使用 FragmentActivity + Loader 更新 FragmentStatePagerAdapter 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)