【问题标题】:Replacing the same fragment after Changes in Shared Prefrence在共享首选项更改后替换相同的片段
【发布时间】:2015-02-10 21:40:57
【问题描述】:

我有五个片段。每个片段加载两种类型的列表视图(学生列表和员工列表)。用户必须从设置(共享首选项)中选择它在所有五个片段中看到的列表类型,无论是学生列表还是所有五个片段中的员工列表。效果很好。

现在我在导航抽屉中放置了一个文本(菜单项)。单击该文本后,(共享首选项应更改)现有列表应从学生列表切换为教职员工列表或教职员工列表切换为学生列表。

我尝试了某种方式,但在切换列表后我的片段没有重新加载

public void onNavigationDrawerItemSelected(int position, NavItem item){
        Fragment fragment;
        String switchs = item.getData();
        if(switchs.equals("switch")){

            SharedPreferences sharedPref= getApplicationContext().getSharedPreferences("switchview", 0);
            String my_view = sharedPref.getString("view", "");
            Log.v("INFO",switchs + my_view);

            switch (my_view){
                case "true" :
                        my_view = "false";
                break;

                case "false" :
                    my_view = "true";
                    break;
            }
            Log.v("INFO","myView" + my_view);
            SharedPreferences.Editor editor= sharedPref.edit();
            editor.putString("view", my_view);
            editor.commit();

            // Reload current fragment
            Fragment frg = null;
            frg = getSupportFragmentManager().findFragmentByTag("main");

            Log.v("INFO","fragment" + frg);
            final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.container, frg,"main").commit();

        }else{
        try {
            fragment = item.getFragment().newInstance();
            if (fragment != null) {
                //adding the data
                Bundle bundle = new Bundle();
                String extra = item.getData();
                bundle.putString(DATA, extra);
                fragment.setArguments(bundle);

                FragmentManager fragmentManager = getSupportFragmentManager();

                fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment,"main").commit();

                setTitle(item.getText());

                if (null != MainActivity.this.getSupportActionBar() && null != MainActivity.this.getSupportActionBar().getCustomView()){
                    MainActivity.this.getSupportActionBar().setDisplayOptions(
                            ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
                } 

             } else {
                // error in creating fragment
                Log.e("MainActivity", "Error in creating fragment");
             }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        }
    }

帮帮我,谢谢。

【问题讨论】:

    标签: android android-activity android-fragments


    【解决方案1】:

    尝试将 getSupportFragmentManager() 更改为 getFragmentManager(),除非您正在处理另一个片段内的片段

    除此之外,我不确定您为什么要重新加载片段只是为了更改 ListViews 的内容

    【讨论】:

    • 实际上我的自定义适配器膨胀了两种类型的列表视图,因此在执行此操作之后 editor.putString("view", my_view);我正在重新加载片段,以便我的 customadapter 相应地更改列表视图。
    • 我猜到了,但我不确定这是最好的方法。无论如何,改变我所说的并告诉我它是否有效,我对 Android 也有点陌生,但我已经使用过这些东西
    • 没有改善。片段不替换。 frg = getSupportFragmentManager().findFragmentByTag("main");返回片段名称,但还有一些不寻常的地址。 V/INFO:My_Firstfragment{4272ce28 #1 id=0x7f0b0079 main}
    • 重新加载列表所需的代码在片段中的 onResume() 方法中,对吧?尝试添加 ft.replace(frg);之前 ft.replace(R.id.container, frg,"main").commit();以便调用该方法。
    • 好吧,试试,或者调试看看它是否执行,就这么简单
    猜你喜欢
    • 2013-07-13
    • 2015-09-30
    • 1970-01-01
    • 2017-01-25
    • 2016-04-26
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多