我有一个活动(MainActivity)扩展到活动和相关的片段(FragmentFive)
有一个搜索栏放置在Fragment的值,我想从MainActivity访问。怎么做呢?API级别18或以上。
MainActivity有一个按钮,当点击时直接到FragmentFive: android:onClick= "goFag5"
一个示例代码的活动将是非常有用的! Fragment代码如下;
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;
}
}
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();
}
有可以创建一个getter
和setter
的搜索栏在你的片段。在你的活动中,你必须抓住碎片。如何创建取决于您如何创建Fragment。如果你用编程方式创建它你可能已经有了它的一个实例因为你创建它的方式是:
MyFragment myFragment = new Fragment();
或者你将Fragment嵌入到XML布局文件中,那么你可以使用类似
的内容myFragment = getFragmentManager().findFragmentById(R.id.myFragment);
那么你可以这样做:
myFragment.getSeekBar();
注意:上面是伪代码,不会工作,但它应该给你一个想法。当使用第二种方法时,您将需要cast
片段到MyFragment。
接口可以用来从一个片段调用回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());
}
}
然后在你的活动中,你实现的接口片段,在上面的例子中被称为BestRidesFollowDialogListener,并让你的ide创建存根。
注意没有显示保存片段中搜索栏的值。将搜索栏初始化为片段中保存的值。对不起,长类名,但如果你有一对片段接口在活动中处理长类和方法名真的有帮助。