在Recycler视图项目上单击新片段

时间:2018-03-07 14:06:31

标签: android fragment

我有一个代码,用于在使用共享首选项单击回收器视图中的项目时打开活动。我试图用片段做同样的事情。

这是带活动的代码。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch(position){

                    case 0: {

                        startActivity(new Intent(WeekActivity.this, DayDetail.class));

                        sharedPreferences.edit().putString(SEL_DAY, "Monday").apply();

                        break;

                    }

                    case 1: {

                        startActivity(new Intent(WeekActivity.this, DayDetail.class));

                        sharedPreferences.edit().putString(SEL_DAY, "Tuesday").apply();

                        break;

                    }

                    case 2: {

                        startActivity(new Intent(WeekActivity.this, DayDetail.class));

                        sharedPreferences.edit().putString(SEL_DAY, "Wednesday").apply();

                        break;

                    }

                    case 3: {

                        startActivity(new Intent(WeekActivity.this, DayDetail.class));

                        sharedPreferences.edit().putString(SEL_DAY, "Thursday").apply();

                        break;

                    }

                    case 4: {

                        startActivity(new Intent(WeekActivity.this, DayDetail.class));

                        sharedPreferences.edit().putString(SEL_DAY, "Friday").apply();

                        break;

                    }

                    case 5: {

                        startActivity(new Intent(WeekActivity.this, DayDetail.class));

                        sharedPreferences.edit().putString(SEL_DAY, "Saturday").apply();

                        break;

                    }

                    default:break;
                }

            }

        });

我尝试过片段

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Fragment selectedFragment = null;

                switch (position) {

                    case 0: {

                        selectedFragment = Timetable.newInstance();

                        sharedPreferences.edit().putString(SEL_DAY, "Monday").apply();

                        break;

                    }

                    case 1: {

                        selectedFragment = Timetable.newInstance();

                        sharedPreferences.edit().putString(SEL_DAY, "Tuesday").apply();

                        break;

                    }

                    case 2: {

                        selectedFragment = Timetable.newInstance();

                        sharedPreferences.edit().putString(SEL_DAY, "Wednesday").apply();

                        break;

                    }

                    case 3: {

                        selectedFragment = Timetable.newInstance();

                        sharedPreferences.edit().putString(SEL_DAY, "Thursday").apply();

                        break;

                    }

                    case 4: {

                        selectedFragment = Timetable.newInstance();
                        sharedPreferences.edit().putString(SEL_DAY, "Friday").apply();

                        break;

                    }

                    case 5: {

                        selectedFragment = Timetable.newInstance();
                        sharedPreferences.edit().putString(SEL_DAY, "Saturday").apply();

                        break;

                    }

                    default:
                        break;

                }
                FragmentTransaction transaction = ((AppCompatActivity) getActivity()).getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.rel_layout, selectedFragment);
                transaction.commit();


            }

        });

我哪里出错了?我应该做些什么改变?我没有错。同时没有任何事情发生。帮助我:))

1 个答案:

答案 0 :(得分:0)

尝试:

case 0: {
         loadFragment(fragment);// method to load a fragment i.e
                               //(loadFragment(new MyFragment())

         sharedPreferences.edit().putString(SEL_DAY, "Monday").apply();
         break;
        }

然后创建loadFragment函数,如:

private void loadFragment(Fragment fragment) {
    // create a FragmentManager
    FragmentManager fm = getFragmentManager();
    // create a FragmentTransaction to begin the transaction and replace the Fragment
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    // replace the FrameLayout with new Fragment
    fragmentTransaction.replace(R.id.YOUR_REPLACEMENT_LAYOUT, fragment);
    // save the changes
    fragmentTransaction.commit(); 
}