gpt4 book ai didi

java - 如何在 FragmentActivity 中使用 putExtra

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:21 24 4
gpt4 key购买 nike

我有一个包含 (9) 个按钮的 Activity ,这些按钮代表不同的内容类别。单击按钮时,putExtra 用于在下一个 Activity 中提供单击的按钮 ID 并加载适当的数据。我现在尝试将 ViewPager 添加到以下子类别 Activity 中。我遇到的问题是我对 ViewPager 和 Fragments 有点不熟悉,需要一些关于如何继续的建议。现在,我对主类别 (BrowseStyle) Activity 中的所有按钮都有一个方法,这些按钮使用适当的按钮 id 链接到 ViewPagerFragmentActivity。我有 3 个 Fragment 类。一切都需要一些微妙的工作。如何让按钮单击使用适当的按钮 ID 信息来扩充我的子类别 Activity ,以及如何在该类中检索它? friend 们,请对我宽容点。

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

最佳答案

从 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 类中,

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;

}

关于java - 如何在 FragmentActivity 中使用 putExtra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43359433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com