问题
I have an Activity (MainActivity) which extends to Activity and a related Fragment (FragmentFive)
There is a seekbar placed in Fragment whose value I want to access from MainActivity. How to do it? API level 18 or above only.
MainActivity has a Button which direct to FragmentFive when clicked: android:onClick= "goFag5"
An example code for Activity will be very useful! Code for Fragment is as below;
FragmentFive.java
public class FragmentFive extends Fragment {
private SeekBar RedBar;
public FragmentFive() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_five, container,
false);
RedBar = (SeekBar) v.findViewById(R.id.seekBar1);
RedBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
Log.d("HM", progress+": value");
}
});
return v;
}
}
code in Mainactivity
public void gofag5(View v) {
// TODO Auto-generated method stub
FragmentFive frag = new FragmentFive();
FragmentManager fm = getFragmentManager(); // import
FragmentTransaction ftr = fm.beginTransaction(); // import
ftr.add(R.id.mainlayout, frag, "keyFrag");
ftr.addToBackStack(null);
ftr.commit();
}
回答1:
Have can create a getter
and setter
for the SeekBar in your Fragment.
In your Activity you have to get a hold on to the Fragment. How depends on how you created the Fragment. If you did create it programmatically you probably already have an instance of it because you created it like:
MyFragment myFragment = new Fragment();
Or you have the Fragment embedded in your XML layout file, then you can use something like
myFragment = getFragmentManager().findFragmentById(R.id.myFragment);
Then you can do:
myFragment.getSeekBar();
Note: Above is pseudocode and will not work, but it should give you an idea. When using the second approach you will need to cast
the Fragment to MyFragment.
回答2:
Interfaces can be used to call from a fragment back to the Activity.
public class SettingFollow extends Fragment implements
OnSeekBarChangeListener {
public interface BestRidesFollowDialogListener {
void onSettingFollowChange(int progress);
}
BestRidesFollowDialogListener bestRidesFollowDialogListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// the activity may be null if this is called without implementing the
// interface
activity = getActivity();
bestRidesFollowDialogListener = (BestRidesFollowDialogListener.class
.isAssignableFrom(activity.getClass())) ? (BestRidesFollowDialogListener) activity
: null;
view = inflater.inflate(R.layout.settings_follow, container,
false);
return view;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// we save and process the value here
switch (seekBar.getId()) {
case R.id.sbFollowMillis:
//call through the interface if its non null the
// only time its null is when the programmer has
// not implemented the interface in the activity that started
// the fragment.
if (bestRidesFollowDialogListener != null) {
bestRidesFollowDialogListener.onSettingFollowChange(seekBar.getProgress());
}
}
Then in your activity you implement the interface for the fragment which in the above example is called BestRidesFollowDialogListener and let your ide create the stubs.
Note not shown save the values of the seekbar in the fragment. initialize the seekbar to the saved value in the fragment. Sorry about the long class names but if you have a couple fragment interfaces to deal with in the activity the long class and method names really help out.
来源:https://stackoverflow.com/questions/24149696/accessing-fragments-seekbar-value-from-activity