Created
September 26, 2017 04:30
-
-
Save mikkipastel/33ff646cdede4ec32e347f0ba9dca2f9 to your computer and use it in GitHub Desktop.
Android Architecture Component; share ViewModel with Fragment ref. at https://codelabs.developers.google.com/codelabs/android-lifecycles/#5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017, The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example.android.lifecycles.step5_solution; | |
import android.arch.lifecycle.LifecycleOwner; | |
import android.arch.lifecycle.Observer; | |
import android.arch.lifecycle.ViewModelProviders; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.SeekBar; | |
import com.example.android.codelabs.lifecycle.R; | |
import com.example.android.lifecycles.step5.SeekBarViewModel; | |
/** | |
* Shows a SeekBar that is synced with a value in a ViewModel. | |
*/ | |
public class Fragment_step5 extends Fragment { | |
private SeekBar mSeekBar; | |
private SeekBarViewModel mSeekBarViewModel; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
View root = inflater.inflate(R.layout.fragment_step5, container, false); | |
mSeekBar = (SeekBar) root.findViewById(R.id.seekBar); | |
mSeekBarViewModel = ViewModelProviders.of(getActivity()).get(SeekBarViewModel.class); | |
subscribeSeekBar(); | |
return root; | |
} | |
private void subscribeSeekBar() { | |
// Update the ViewModel when the SeekBar is changed. | |
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { | |
if (fromUser) { | |
Log.d("Step5", "Progress changed!"); | |
mSeekBarViewModel.seekbarValue.setValue(progress); | |
} | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { } | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { } | |
}); | |
// Update the SeekBar when the ViewModel is changed. | |
mSeekBarViewModel.seekbarValue.observe( | |
(LifecycleOwner) getActivity(), | |
new Observer<Integer>() { | |
@Override | |
public void onChanged(@Nullable Integer value) { | |
if (value != null) { | |
mSeekBar.setProgress(value); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment