将时间从活动传递到片段

时间:2014-08-14 20:53:59

标签: android android-fragments

据我所知,活动/片段信息传递已经被问到了,但我无法想出这一点。我有一个活动(按下按钮)启动带有TimerTask的Timer并使片段从底部向上滑动。在打开的片段中,按下新按钮即可关闭。我想要发生的事情就是让那个按钮也停止那个时间任务。我该怎么做?

MainActivity

public class MainActivity extends FragmentActivity {

Timer timer;
protected boolean isTaskCompleted = false;
public boolean isTaskCompleted() {return isTaskCompleted; }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final CircularSeekBar sA = (CircularSeekBar)findViewById(R.id.bpm_scrubber);
    final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabbutton);
    final SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    final int electronic_sound = sp.load(getApplicationContext(), R.raw.high_metronome, 1);
    final int woodblock_sound = sp.load(getApplicationContext(), R.raw.low_metronome, 1);
    final int cowbell_sound = sp.load(getApplicationContext(), R.raw.cowbell, 1);
    final SharedPreferences settings = getSharedPreferences("progress", Activity.MODE_PRIVATE);
    final EditText editText = (EditText)findViewById(R.id.bpm_text);
    String test = settings.getString("sound_key", "1");
    final int lPInt = Integer.parseInt(test);

    sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int i, int i2) {

            fab.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if(sA.getProgress() > 0 && !isTaskCompleted()) {

                        timer = new Timer();
                        timer.schedule(new TimerTask() {
                            public void run() {
                                if (lPInt == 1) {
                                    sp.play(electronic_sound, 1, 1, 0, 0, 1);
                                } else if (lPInt == 2) {
                                    sp.play(woodblock_sound, 1, 1, 0, 0, 1);
                                } else if (lPInt == 3) {
                                    sp.play(cowbell_sound, 1, 1, 0, 0, 1);
                                }
                            }
                        }, 0, 1000 * 60 / sA.getProgress());

                        Bundle args = new Bundle();

                        PlayFragment fragment = new PlayFragment();
                        getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_in_up, R.animator.slide_out_up).add(R.id.relative_layout, fragment).commit();

                        isTaskCompleted = true;
                    } else {
                        Toast.makeText(getApplicationContext(), "BPM cannot equal 0", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    });

    sA.setOnSeekBarChangeListener(new CircularSeekBar.OnCircularSeekBarChangeListener() {
        @Override
        public void onProgressChanged(CircularSeekBar circularSeekBar, int progress, boolean fromUser) {
            EditText editText = (EditText)findViewById(R.id.bpm_text);
            editText.setText(Integer.toString(progress));
        }

        @Override
        public void onStopTrackingTouch(CircularSeekBar seekBar) {

        }

        @Override
        public void onStartTrackingTouch(CircularSeekBar seekBar) {

        }
    });

    int spInt = settings.getInt("progress_key", 0);
    editText.setText(Integer.toString(spInt));
    sA.setProgress(spInt);


    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            editText.setSelection(editText.getText().length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!editText.getText().toString().equals("")) {
                int i = Integer.parseInt(s.toString());
                sA.setProgress(i);
                SharedPreferences.Editor editor = settings.edit().putInt("progress_key", i);
                editor.apply();
            }

        }
    });

    final Spinner timeSpinner = (Spinner) findViewById(R.id.time_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.time_spinner, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    timeSpinner.setAdapter(adapter);
}

}

PlayFragemnt

public class PlayFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_play, container, false);

    FloatingActionButton fab2 = (FloatingActionButton) v.findViewById(R.id.fabbutton2);
    fab2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActivity().getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_in_up, R.animator.slide_out_up).remove(PlayFragment.this).commit();


        }
    });

    return v;
}

}

1 个答案:

答案 0 :(得分:1)

了解Communicating with the Activity

简而言之:

  1. PlayFragment
  2. 中创建回调界面
  3. 活动作为回调界面附加到 PlayFragment
  4. 中的私有字段
  5. 在活动上实现接口并添加代码以停止计时器:

    timer.cancel();
    
  6. onClick方法中的Inkove侦听器(活动)方法。