如何在FragmentActivity中使用putExtra

时间:2017-04-12 02:53:38

标签: java android android-fragments android-viewpager

我有一个带有(9)按钮的活动,代表不同的内容类别。单击按钮时,putExtra用于在下一个活动中提供单击的按钮ID并加载相应的数据。我现在正在尝试将ViewPager添加到以下子类别活动中。我遇到的问题是我有点对ViewPager和Fragments不熟悉,需要一些关于如何继续的建议。现在我对主类别(BrowseStyle)活动中的所有按钮都有一个方法,该活动链接到具有相应按钮ID的ViewPagerFragmentActivity。我有3个Fragment类。都需要一些微妙的工作。如何使用相应的按钮ID信息使按钮单击使我的子类别活动膨胀,如何在该类中检索它?试着对我很好,朋友们。

public class BrowseStyle extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_style);
    }

    public void bSubCategoryClicked(View view)
    {
        Intent startActivity = new Intent(this, ViewPagerFragmentActivity.class);
        startActivity.putExtra("buttonID", view.getId());
        startActivity(startActivity);
    }



    public class ViewPagerFragmentActivity extends FragmentActivity {
    /** maintains the pager adapter*/
    private PagerAdapter mPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.viewpager_layout);

        // retrieve "put extra" data from BrowseStyle class intent call
        // extra data is the button id that was clicked
        Intent mIntent = getIntent();
        // 0 is the default id
        int id = mIntent.getIntExtra("buttonID", 0);

        // initialize the pager
        this.initializePaging();

        if (savedInstanceState == ) {
            Bundle bundle = new Bundle();
            bundle.putInt("buttonID", id);
            // set Fragment class Arguments
            FragmentStyleSubCatMain loadMainFrag = new FragmentStyleSubCatMain();
            loadMainFrag.setArguments(bundle);
        }
    }

    /**
     * Initialize the fragments to be paged
     */
    private void initializePaging() {

        List<Fragment> fragments = new Vector<>();
        fragments.add(Fragment.instantiate(this, FragmentStyleSubCatLeft.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentStyleSubCatMain.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentStyleSubCatRight.class.getName()));
        this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);
        //
        ViewPager pager = (ViewPager)super.findViewById(R.id.vp_pager);
        pager.setAdapter(this.mPagerAdapter);
    }

    // create inner CustomAdapter class for pageViewer swipe design
    private class PagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public PagerAdapter(FragmentManager supportFragManager, List<Fragment> fragments) {
            super(supportFragManager);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case -1:
                    return new FragmentStyleSubCatLeft();
                case 0:
                    return new FragmentStyleSubCatMain();
                case 1:
                    return new FragmentStyleSubCatRight();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }
}



public class FragmentStyleSubCatMain extends Fragment {

    // handle variables for each dynamic xml object utilized in this [sub-category] activity
    private TextView header, featured;
    private RadioButton rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8, rb9, rb10, rb11, rb12;
    private CheckBox cbAdditives;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_style_sub_cat_left, container, false);

        // initialize the dynamic xml objects from this [sub-category] activity
        header = (TextView) getView().findViewById(R.id.tv_header);
        rb1 = (RadioButton) getView().findViewById(R.id.radioButton1);
        rb2 = (RadioButton) getView().findViewById(R.id.radioButton2);
        rb3 = (RadioButton) getView().findViewById(R.id.radioButton3);
        rb4 = (RadioButton) getView().findViewById(R.id.radioButton4);
        rb5 = (RadioButton) getView().findViewById(R.id.radioButton5);
        rb6 = (RadioButton) getView().findViewById(R.id.radioButton6);
        rb7 = (RadioButton) getView().findViewById(R.id.radioButton7);
        rb8 = (RadioButton) getView().findViewById(R.id.radioButton8);
        rb9 = (RadioButton) getView().findViewById(R.id.radioButton9);
        rb10 = (RadioButton) getView().findViewById(R.id.radioButton10);
        rb11 = (RadioButton) getView().findViewById(R.id.radioButton11);
        rb12 = (RadioButton) getView().findViewById(R.id.radioButton12);
        featured = (TextView) getView().findViewById(R.id.tv_featured);
        cbAdditives = (CheckBox) getView().findViewById(R.id.cb_additives);

        // retrieve "put extra" data from BrowseStyle class intent call
        // extra data is the button id that was clicked
        Intent mIntent = getIntent();
        // 0 is the default id
        int id = mIntent.getIntExtra("buttonID", 0);

        /* this switch statement will set the appropriate header text that needs to be displayed and
           call the appropriate method that will set the text and visibility of the radio buttons
         */
        switch (id)
        {
            case 0:
                // error handling if button id is invalid
                header.setText("Something went wrong");
                break;
            case R.id.b_lager_and_light_ales:
                header.setText(R.string.lagers);
                setLagerAndLightAlesRB();
                break;
            case R.id.b_wheat_ales:
                header.setText(R.string.wheats);
                setWheatAlesRB();
                break;
            case R.id.b_pale_ales_and_ipas:
                header.setText(R.string.pales);
                setPaleAlesAndIPAsRB();
                break;
            case R.id.b_belgian_ales:
                header.setText(R.string.belgians);
                setBelgianAlesRB();
                break;
            case R.id.ambers_and_browns:
                header.setText(R.string.ambers);
                setAmbersAndBrownsRB();
                break;
            case R.id.b_porters_and_stouts:
                header.setText(R.string.porters);
                setPortersAndStoutsRB();
                break;
            case R.id.b_strong_ales_and_barleywines:
                header.setText(R.string.strongs);
                setStrongAlesAndBarleywinesRB();
                break;
            case R.id.b_sours:
                header.setText(R.string.sours);
                setSoursRB();
                break;
            case R.id.b_ciders_and_meads:
                header.setText(R.string.ciders);
                setCidersAndMeadsRB();
                break;
        }
        return view;
    }



public class FragmentStyleSubCatLeft extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_style_sub_cat_left, container, false);
    }



public class FragmentStyleSubCatRight extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_style_sub_cat_right, container, false);
    }

1 个答案:

答案 0 :(得分:0)

从BrowseStyle获取ButtonId,

  public class ViewPagerFragmentActivity extends FragmentActivity {
    /** maintains the pager adapter*/
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.viewpager_layout);
        String buttonId = getIntent().getStringExtra("buttonID");//call this line get button id from BrowseStyle Activity
        // initialize the pager
        FragmentStyleSubCatMain.mButtonId = (int) buttonId;// set your button Id like this
        this.initializePaging();
    }


}

在Fragment Class中,

public class FragmentStyleSubCatMain extends Fragment {

// handle variables for each dynamic xml object utilized in this [sub-category] activity
private TextView header, featured;
private RadioButton rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8, rb9, rb10, rb11, rb12;
private CheckBox cbAdditives;
publie static int mButtonId;// use this line

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_style_sub_cat_left, container, false);

    // initialize the dynamic xml objects from this [sub-category] activity
    header = (TextView) getView().findViewById(R.id.tv_header);
    rb1 = (RadioButton) getView().findViewById(R.id.radioButton1);
    rb2 = (RadioButton) getView().findViewById(R.id.radioButton2);
    rb3 = (RadioButton) getView().findViewById(R.id.radioButton3);
    rb4 = (RadioButton) getView().findViewById(R.id.radioButton4);
    rb5 = (RadioButton) getView().findViewById(R.id.radioButton5);
    rb6 = (RadioButton) getView().findViewById(R.id.radioButton6);
    rb7 = (RadioButton) getView().findViewById(R.id.radioButton7);
    rb8 = (RadioButton) getView().findViewById(R.id.radioButton8);
    rb9 = (RadioButton) getView().findViewById(R.id.radioButton9);
    rb10 = (RadioButton) getView().findViewById(R.id.radioButton10);
    rb11 = (RadioButton) getView().findViewById(R.id.radioButton11);
    rb12 = (RadioButton) getView().findViewById(R.id.radioButton12);
    featured = (TextView) getView().findViewById(R.id.tv_featured);
    cbAdditives = (CheckBox) getView().findViewById(R.id.cb_additives);

    // retrieve "put extra" data from BrowseStyle class intent call
    // extra data is the button id that was clicked
    Intent mIntent = getIntent();
    // 0 is the default id


    /* this switch statement will set the appropriate header text that needs to be displayed and
       call the appropriate method that will set the text and visibility of the radio buttons
     */
    switch (mButtonId)//use button Id like this
    {
        case 0:
            // error handling if button id is invalid
            header.setText("Something went wrong");
            break;
        case R.id.b_lager_and_light_ales:
            header.setText(R.string.lagers);
            setLagerAndLightAlesRB();
            break;
        case R.id.b_wheat_ales:
            header.setText(R.string.wheats);
            setWheatAlesRB();
            break;
        case R.id.b_pale_ales_and_ipas:
            header.setText(R.string.pales);
            setPaleAlesAndIPAsRB();
            break;
        case R.id.b_belgian_ales:
            header.setText(R.string.belgians);
            setBelgianAlesRB();
            break;
        case R.id.ambers_and_browns:
            header.setText(R.string.ambers);
            setAmbersAndBrownsRB();
            break;
        case R.id.b_porters_and_stouts:
            header.setText(R.string.porters);
            setPortersAndStoutsRB();
            break;
        case R.id.b_strong_ales_and_barleywines:
            header.setText(R.string.strongs);
            setStrongAlesAndBarleywinesRB();
            break;
        case R.id.b_sours:
            header.setText(R.string.sours);
            setSoursRB();
            break;
        case R.id.b_ciders_and_meads:
            header.setText(R.string.ciders);
            setCidersAndMeadsRB();
            break;
    }
    return view;

}