从访问片段活动的搜索栏值 [英] Accessing Fragment's seekBar value from Activity

查看:225
本文介绍了从访问片段活动的搜索栏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须延伸到活动和相关的片段(FragmentFive)的活动(MainActivity)

有是放置在片段我想从MainActivity访问,其价值搜索栏。
怎么做? API级别18 或以上版本。

MainActivity具有直接点击时FragmentFive一个按钮:安卓的onClick =goFag5

一个例子code的活动将是非常有用 code的片段如下;!

FragmentFive.java

 公共类FragmentFive扩展片段{私人搜索栏RedBar;公共FragmentFive(){
    //必需的空公共构造
}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    //充气的布局该片段
    视图V = inflater.inflate(R.layout.fragment_five,集装箱,
            假);    RedBar =(搜索栏)v.findViewById(R.id.seekBar1);    RedBar.setOnSeekBarChangeListener(新OnSeekBarChangeListener(){        @覆盖
        公共无效onStopTrackingTouch(搜索栏搜索栏){
            // TODO自动生成方法存根        }        @覆盖
        公共无效onStartTrackingTouch(搜索栏搜索栏){
            // TODO自动生成方法存根        }        @覆盖
        公共无效onProgressChanged(搜索栏搜索栏,INT进步,
                布尔FROMUSER){
            Log.d(HM进步+:值);
        }
    });    返回伏;
    }

}

code在Mainactivity

 公共无效gofag5(视图v){
    // TODO自动生成方法存根
    FragmentFive FRAG =新FragmentFive();
    FragmentManager FM = getFragmentManager(); //进口
    FragmentTransaction FTR = fm.beginTransaction(); //进口
    ftr.add(R.id.mainlayout,FRAG,keyFrag);
    ftr.addToBackStack(NULL);
    ftr.commit();}


解决方案

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

 公共类SettingFollow扩展片段器具
    OnSeekBarChangeListener {公共接口BestRidesFollowDialogListener {
    无效onSettingFollowChange(INT进展);
}BestRidesFollowDialogListener bestRidesFollowDialogListener;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    //活动可以为null,如果这就是所谓的不实施
    //接口    活性= getActivity();
    bestRidesFollowDialogListener =(BestRidesFollowDialogListener.class
            .isAssignableFrom(activity.getClass()))? (BestRidesFollowDialogListener)活动
            : 空值;
    鉴于= inflater.inflate(R.layout.settings_follow,集装箱,
            假);
    返回视图。
}@覆盖
公共无效onStopTrackingTouch(搜索栏搜索栏){
    //我们保存和处理这里的价值    开关(seekBar.getId()){
    案例R.id.sbFollowMillis:
    如果非空//通过接口调用
            //只有它的空当程序员有时间
            //没有实现在启动该活动界面
            //片段。                如果(bestRidesFollowDialogListener!= NULL){
        bestRidesFollowDialogListener.onSettingFollowChange(seekBar.getProgress());
    }

}

然后在你的活动中,你实施这在上面的例子中被称为BestRidesFollowDialogListener片段的接口,让你的IDE中创建的存根。

请注意未显示保存在片段SeekBar的值。初始化SeekBar以在片段中保存的值。很抱歉的长类的名字,但如果你有一对夫妇片段接口,以应对该活动的长期类和方法名真正助阵。

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

}

解决方案

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.

这篇关于从访问片段活动的搜索栏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆