我有一种情况,我需要在从对该数据库进行一些修改的Activity返回时为片段设置不同的布局。
如何在该片段的onResume()方法中为片段设置不同的布局?
我基本上需要将完全相同的代码复制到onResume方法,但问题在于这部分代码,我无法理解如何在on onResume()方法中实现它:
rootView = inflater.inflate(R.layout.expandable_list, container, false);
以下是OnCreateView方法的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity().getApplicationContext();
dataSource = new ExpensesDataSource(context);
dataSource.open();
//if Expenses dataSource is not empty create the list of expenses and set a listview
if (dataSource.getAllExpenses().size() != 0) {
rootView = inflater.inflate(R.layout.expandable_list, container, false);
//prepare the list of Expenses to be put into an adapter
preparedListData = prepareListData();
//load the sorted list of lists of expense into the adapter
listAdapter = new ExpandableListAdapter(context, preparedListData);
expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);
expListView.setAdapter(listAdapter);
expListView.setOnChildClickListener(this);
}
//else show a layout saying the list is empty
else {
rootView = inflater.inflate(R.layout.expandable_list_empty, container, false);
}
return rootView;
}
答案 0 :(得分:0)
我找到了解决这个问题的方法。这有点讨厌,但这就是我所能做的。假设您在活动中保存了当前选择的片段,那么您需要在活动onResume中执行此操作:
if(currentFragment instanceof YourDynamiclyChangingFrament) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
YourDynamiclyChangingFrament yourDynamiclyChangingFrament= new YourDynamiclyChangingFrament();
transaction.replace(R.id.container, yourDynamiclyChangingFrament, YourDynamiclyChangingFrament.class.getSimpleName());
transaction.commit();
}