Android 碎片事务替换和操作栏选项卡
如何将actionbar选项卡替换为其自身,以便完全重新加载?我目前的方法(不起作用)是调用FragmentTransaction替换为片段的新实例,如下所示:Android 碎片事务替换和操作栏选项卡,android,android-fragments,Android,Android Fragments,如何将actionbar选项卡替换为其自身,以便完全重新加载?我目前的方法(不起作用)是调用FragmentTransaction替换为片段的新实例,如下所示: AgendaFragment agenda = new AgendaFragment(); String label = getResources().getString(R.string.title_activity_list); FragmentTransaction ft = getFragmentManager().beginT
AgendaFragment agenda = new AgendaFragment();
String label = getResources().getString(R.string.title_activity_list);
FragmentTransaction ft = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("date", date);
agenda.setArguments(bundle);
ft.replace(R.id.pager, agenda, label);
ft.commit();
在我的议程片段中,我有一个ExpandableListView,这就是我想要刷新的内容。不幸的是,上面的代码没有重新加载ExpandableListView,它保持不变。我哪里做错了
编辑:以下是来自AgendaFragment
的相关片段
public class AgendaFragment extends Fragment {
private Context mContext;
ArrayList<String> mDate;
Vector<List<String>> mMappedInfo;
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mContext = (Context) getActivity();
mDate = new ArrayList<String>();
mMappedInfo = new Vector<List<String>>();
//...
//Get necessary information from server.
serverCommunication();
}
private void serverCommunication() {
new AsyncTask<Void, Void, Boolean>() {
//get information
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
//parse data
apptXmlParsing(mCompleteXml);
}
}
}.execute();
}
public void apptXmlParsing(final XmlDom completeXml) {
//Parse received data
populateListView();
}
private void populateListView(){
final ExpandableListView eLV = (ExpandableListView) getActivity().findViewById(R.id.expListView);
//process data into acceptable format
ExpandableListAdapter listAdapter = new ExpandableListAdapter(mContext, mDate, mMappedInfo, mCurrDate);
eLV.setAdapter(listAdapter);
}
}
公共类代理片段扩展片段{
私有上下文;
ArrayList mDate;
向量mMappedInfo;
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mContext=(上下文)getActivity();
mDate=newarraylist();
mMappedInfo=新向量();
//...
//从服务器获取必要的信息。
服务器通信();
}
专用通信(){
新建异步任务(){
//获取信息
@凌驾
受保护的void onPostExecute(布尔结果){
super.onPostExecute(结果);
如果(结果){
//解析数据
apptXmlParsing(mCompleteXml);
}
}
}.execute();
}
公共void apptXmlParsing(最终XmlDom CompleteML){
//解析接收到的数据
populateListView();
}
私有void populateListView(){
最终ExpandableListView eLV=(ExpandableListView)getActivity().findViewById(R.id.expListView);
//将数据处理为可接受的格式
ExpandableListAdapter listAdapter=新的ExpandableListAdapter(mContext、mDate、mMappedInfo、mCurrDate);
eLV.setAdapter(列表适配器);
}
}
我最后做的是:
final ActionBar actionBar = getActionBar();
final Tab tab = actionBar.getSelectedTab();
int selTab = actionBar.getSelectedNavigationIndex();
String label = getResources().getString(R.string.title_activity_list);
Tab agendaTab = actionBar.newTab();
agendaTab.setText(label);
TabListener<AgendaFragment> tl3 = new TabListener<AgendaFragment>(this, label,
AgendaFragment.class, date);
agendaTab.setTabListener(tl3);
actionBar.addTab(agendaTab, selTab, true);
actionBar.removeTab(tab);
这样可以在删除旧选项卡的同时重新加载具有正确日期的选项卡
Bundle bundle = new Bundle();
if(mDate != null)
bundle.putString("date", mDate);
mFragment.setArguments(bundle);