FragmentTransaction替换和ActionBar选项卡

时间:2014-04-16 21:40:46

标签: android android-fragments

如何将操作栏选项卡替换为自身以便完全重新加载?我当前的方法(不起作用)是调用FragmentTransaction替换片段的新实例,如下所示:

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);

    }

}

1 个答案:

答案 0 :(得分:0)

我最终做的是:

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);

在我的TabListener类的onTabSelected方法中:

Bundle bundle = new Bundle();
if(mDate != null)
    bundle.putString("date", mDate);
mFragment.setArguments(bundle);

这样可以在删除旧标签时重新加载具有正确日期的标签。