无法将导出抽屉中的活动包发送到片段

时间:2014-11-19 01:15:58

标签: android android-fragments bundle navigation-drawer

我正在尝试从Main活动发送一个名为jsonList的HashMap包:

public Bundle authBundle(){

    jsonList = EndpointsParser.parseJSON(endpoints);
    extras = new Bundle();
    extras.putSerializable("ParsedList", jsonList);
    return extras;
}


@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position))
            .commit();
    extras = authBundle();
    switch (position) {
        case 0:
            OverviewFragment overviewFragment = new OverviewFragment();
            overviewFragment.setArguments(extras);
            fragmentManager.beginTransaction().replace(R.id.container, OverviewFragment.newInstance(position)).commit();
            break;

到片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    Bundle extras = getArguments();
    Serializable parsedList = extras.getSerializable("ParsedList");
    jsonList = (ArrayList<HashMap<String, String>>)parsedList;
    View rootView = inflater.inflate(R.layout.fragment_overview, container, false);

    recyclerView = (RecyclerView)rootView.findViewById(R.id.overviewRV);

    return rootView;
    }

然而,无论我尝试什么,Bundle总是会在没有数据的情况下进入片段。在调试之后,我可以在片段管理器调用片段之前在Activity开关上看到“overviewFragment.setArguments(extras)”上的数据但是它没有到​​达那里。

我提前道歉,如果它太明显但这是我的第一个应用程序而我无法让它工作。我真的很感激一些帮助。

更新:

我设法通过添加以下注释行来获取数据到片段:

switch (position) {
        case 0:
            OverviewFragment overviewFragment = new OverviewFragment();
            overviewFragment.setArguments(extras);
            //fragmentManager.beginTransaction().add(R.id.container,overviewFragment).commit();
            fragmentManager.beginTransaction().replace(R.id.container, OverviewFragment.newInstance(position)).commit();
            break;

但是我的列表不再滚动,且项目不响应点击。它似乎在同一个地方膨胀2个片段并阻止所有交互。显然“.replace”不会发送捆绑包。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

管理以使其与&#34;添加&#34;然后是&#34;替换&#34;:

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position))
            .commit();
    extras = authBundle();
    switch (position) {
        case 0:
            OverviewFragment overviewFragment = new OverviewFragment();
            overviewFragment.setArguments(extras);
            fragmentManager.beginTransaction().add(R.id.container, OverviewFragment.newInstance(position)).commit();
            fragmentManager.beginTransaction().replace(R.id.container,overviewFragment).commit();
            break;