我试图使用 FragmentActivity
与 ViewPager
来显示片段动态列表
。有很多例子就如何做一个静态的版本。我的问题是,在列表显示我需要动态加载的,也可以根据用户输入改变(添加/删除)。我试图使用自定义的 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()
呼叫此时该code为执行(从 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
这code执行:
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
值不为空,它是在 LoaderManagerImpl.LoaderInfo
设置时,其结果是返回到 onLoadFinished
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)?
只是要注意,我的code正常工作,而不是使用装载机当我运行负荷OPS中的AsyncTask
Just to note that my code works fine if instead of using Loader I run the loading ops in AsyncTask
我有一个非常类似的问题,这和使用上的片段的处理程序解决了这个问题。
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");
}
}
但是,这给了一个错误
But this gave an error
不能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);
}