0
这是我的情况:如何重新加载片段?
我正在检索数据库中的数据在一个片段和结果是异步的。所以我想在从数据库中得到结果后重新加载视图。
我试过用“碎片交易”很多方面像这样的:
private void reloadView(ViewGroup container){
Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(container.getId());
if (currentFragment instanceof StatsFragment) {
FragmentTransaction fragTransaction = (getActivity()).getSupportFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();}
}
你认为有更好的方式来做到这一点?我可以放一个适配器或类似的东西吗? (我想这样做,但所有的教程,我已经找到关于适配器listview在片段)。
非常感谢您的回答!
检查[this](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html)。 用'RecycleView'替换你的'ListView'。 – FiN
在你的Fragment中做一个'updateView()',在''AsyncTask''的'onPostExecute()'里面你所有的。 –
我建议你只更新适配器,而不是做繁重的工作。 ListView或RecycleView只是将新数据更新到列表并使用'adapter.notifydatasetchanged()'就好了。 –