替换片段时保持Seekbar值

时间:2016-04-28 19:10:10

标签: android android-fragments android-inflate

我有一个带有4个项目的导航抽屉(主页,设置,关于,帮助)。单击该项目时,与该项目对应的片段将替换另一个片段。

现在,我有3个搜索栏的设置片段,如果用户更改搜索栏的值,打开导航抽屉后点击主页然后返回设置片段,搜索栏又回到0“

如何在更改片段之前将seekBar保持在相同的状态。

这是在片段中创建视图时的代码,我认为问题是因为我膨胀了视图。

我尝试使用saveInstanceState,但我认为该实例永远不会被破坏,这就是为什么当我运行de代码时,永远不会调用saveInstanceState。

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    myFragmentView = inflater.inflate(R.layout.fragment_param, container, false);
    seekbarAffValeur();
    return myFragmentView;
}

这是我更改片段时的代码。

 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home_id:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new AcceuilFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Acceuil");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;

                case R.id.setting_id:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new ParamFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Paramètres");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();

                    break;
                case R.id.about_id:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new ProposFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("À propos");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.help_id:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new AideFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Aide");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
            }

谢谢。

好的,我找到了一个解决方案,最后很容易。

private static int disPosiVal;
private static int disCarteVal;
private static int minPosiVal;
//Pour afficher la valeur des SeekBar.
public void seekbarAffValeur(){
    skbDisPosiValu = (SeekBar)   myFragmentView.findViewById(R.id.skbDisPosi);
    tvDisPosi = (TextView) myFragmentView.findViewById(R.id.tvValeurDisPosi);
    skbDisCarteValu = (SeekBar)   myFragmentView.findViewById(R.id.skbDisCarte);
    tvDisCarte = (TextView) myFragmentView.findViewById(R.id.tvValeurDisCarte);
    skbMinuValue = (SeekBar)   myFragmentView.findViewById(R.id.skbMinPosi);
    tvMinuValu = (TextView) myFragmentView.findViewById(R.id.tvValeurMinPosio);

    skbDisPosiValu.setProgress(disPosiVal);
    tvDisPosi.setText(disPosiVal+ "");
    skbDisCarteValu.setProgress(disCarteVal);
    tvDisCarte.setText(disCarteVal+"");
    skbMinuValue.setProgress(minPosiVal);
    tvMinuValu.setText(minPosiVal+"");

    skbDisPosiValu.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            disPosiVal = progress;
            tvDisPosi.setText(progress + "");
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            tvDisPosi.setText(disPosiVal + "");

        }

    });
    skbDisCarteValu.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            disCarteVal = progress;
            tvDisCarte.setText(progress + "");
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            tvDisCarte.setText(disCarteVal + "");
        }
    });
    skbMinuValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            minPosiVal = progress;
            tvMinuValu.setText(progress + "");
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            tvMinuValu.setText(minPosiVal + "");
        }
    });

视图是破坏但不是碎片(我认为),所以我只是将我的值保存在一个静态变量中,当创建视图时,我将值设置为它所在的位置。

1 个答案:

答案 0 :(得分:0)

我认为这是问题 - fragmentTransaction.replace(R.id.main_container, new AcceuilFragment());

您基本上是删除旧片段。你需要做的是

fragmentTransaction.replace(R.id.main_container, new AcceuilFragment().addToBackStack(null).commit();

addToBackStack()基本上将片段放入堆栈。所以你可以调用popBackStack()来获取片段。enter code here

文档:

FragmentManager popBackStack

FragmentTransaction addToBackStack