如何在Fragments中保存单选按钮的状态

时间:2015-05-25 10:45:29

标签: android android-fragments

如何在从左到右滑动视图的同时保存Fragment的状态,反之亦然,我在视图中有动态的radiobuttons,如果我滑动视图(从左到右),反之亦然。有人可以帮我保存状态。感谢

public class LayoutFragment extends Fragment {

    int fragVal;
    private String[] application = { "Country1", "Country2", "Country3", "Country4", "Country5", "Country6", "Country7", "Country8" };                     
    private String[] device = { "Country9", "Country10", "Country11", "Country12", "Country13", "Country14", "Country15", "Country16" }; 

    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioButton btn;
    private RadioButton btn2;
    private String text1;
    private String text2;
    private String text3;
    private String text4;
    RadioButton button1;
    RadioButton button2;
    Button selectall;
    Context thiscontext;

    static LayoutFragment init(int val) {
        LayoutFragment truitonFrag = new LayoutFragment();
        // Supply val input as an argument.
        Bundle args = new Bundle();
        args.putInt("val", val);
        truitonFrag.setArguments(args);
        return truitonFrag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        thiscontext = container.getContext();
        View layoutView = inflater.inflate(R.layout.activity_main, container, false);

        //Draw Radiobuttons
        radioGroup1 = (RadioGroup) layoutView.findViewById(R.id.radio1);
        radioGroup2 = (RadioGroup) layoutView.findViewById(R.id.radio2);


        ViewGroup hourButtonLayout = (ViewGroup) layoutView.findViewById(R.id.radio1);
        for (int i = 0; i < application.length; i++) {
            button1 = new RadioButton(thiscontext);
            button1.setId((int) System.currentTimeMillis());
            button1.setText(application[i]);

            hourButtonLayout.addView(button1);

            radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup2,
                                int checkedId2) {
                            for (int i = 0; i < mRadioGroup2.getChildCount(); i++) {
                                btn = (RadioButton) mRadioGroup2.getChildAt(i);
                                int t = mRadioGroup2.getId();
                                System.out.println(t);

                                if (btn.getId() == checkedId2) {
                                    text1 = btn.getText().toString();
                                    Toast.makeText(thiscontext,
                                            "You selected : " + text1,
                                            Toast.LENGTH_SHORT).show();



                                    return;
                                }
                            }
                        }
                    });

        }

        ViewGroup hourButtonLayout2 = (ViewGroup) layoutView.findViewById(R.id.radio2); 
        for (int i = 0; i < device.length; i++) {
            button2 = new RadioButton(thiscontext);
            button2.setId((int) System.currentTimeMillis());
            button2.setText(device[i]);

            hourButtonLayout2.addView(button2);

            radioGroup2
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup,
                                int checkedId) {
                            for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
                                btn2 = (RadioButton) mRadioGroup.getChildAt(i);
                                int t = mRadioGroup.getId();
                                System.out.println(t);

                                if (btn2.getId() == checkedId) {
                                    text2 = btn2.getText().toString();
                                    Toast.makeText(thiscontext,
                                            "You selected : " + text2,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

        return layoutView;
    }

}

MainActivity.java

public class MainActivity extends FragmentActivity {
     static final int ITEMS = 5;
     MyAdapter mAdapter;
     ViewPager mPager;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.fragment_pager);
     mAdapter = new MyAdapter(getSupportFragmentManager());
     mPager = (ViewPager) findViewById(R.id.pager);
     mPager.setAdapter(mAdapter);

     Button button = (Button) findViewById(R.id.first);
     Button submitaldatabutton = (Button) findViewById(R.id.first);
     button.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
     mPager.setCurrentItem(0);
     }
     });

     button = (Button) findViewById(R.id.last);
     button.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
     mPager.setCurrentItem(ITEMS - 1);
     }
     });

     submitaldatabutton = (Button) findViewById(R.id.submitalldata);
     submitaldatabutton.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {

             //Get all data

     }
     });
     }

     public static class MyAdapter extends FragmentStatePagerAdapter {
     public MyAdapter(FragmentManager fragmentManager) {
     super(fragmentManager);
     }

     @Override
     public int getCount() {
     return ITEMS;
     }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        switch (position) {
         case 0: // Fragment # 0 - This will show image
         return LayoutFragment.init(position);
         case 1: // Fragment # 1 - This will show image
         return LayoutFragment.init(position);
         default:// Fragment # 2-9 - Will show list
         return LayoutFragment.init(position);
         }
    }

  }

}

0 个答案:

没有答案