Android 如何重新加载碎片?
以下是我的情况: 我正在从数据库中检索片段中的数据,结果是异步的。所以我想在从数据库得到结果后重新加载视图 我已经尝试了很多“片段事务”的方法,比如:Android 如何重新加载碎片?,android,android-fragments,android-view,Android,Android Fragments,Android View,以下是我的情况: 我正在从数据库中检索片段中的数据,结果是异步的。所以我想在从数据库得到结果后重新加载视图 我已经尝试了很多“片段事务”的方法,比如: private void reloadView(ViewGroup container){ Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(container.getId()); if (currentFra
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适配器)
提前感谢您的回答 有多种方法可以做到这一点:
我个人认为数据绑定在任何地方都是可能的,因为它简直太神奇了。请查看。用
RecycleView
替换你的ListView
。在你的片段中,用一个函数updateView()
将它全部放在AsyncTask
的onPostExecute()
中。我建议你只更新适配器,而不是做繁重的工作。ListView或RecycleView无所谓,只需将新数据更新到列表中,然后使用适配器。notifydatasetchanged()就是这样。