0

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();

}
4

2 回答 2

0

可以在 Fragment 中为 SeekBar创建一个getter和。setter在您的活动中,您必须抓住片段。如何取决于您如何创建片段。如果您确实以编程方式创建它,您可能已经拥有它的一个实例,因为您创建它的方式如下:

MyFragment myFragment = new Fragment(); 

或者您将片段嵌入到您的 XML 布局文件中,然后您可以使用类似的东西

myFragment = getFragmentManager().findFragmentById(R.id.myFragment); 

然后你可以这样做:

myFragment.getSeekBar(); 

注意:上面是伪代码,不会工作,但它应该给你一个想法。使用第二种方法时,您需要cast将 Fragment 转为 MyFragment。

于 2014-06-10T19:59:26.377 回答
0

接口可用于从片段调用回活动。

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 创建存根。

注意未显示将搜索栏的值保存在片段中。将搜索栏初始化为片段中保存的值。对长类名感到抱歉,但如果您在活动中有几个片段接口要处理,那么长类和方法名真的很有帮助。

于 2014-06-10T23:24:09.717 回答