Java 如何根据第一个选项卡中微调器项的选择更改第二个选项卡的xml布局
我添加了3个选项卡,如下所示。在“IntroductionFragment”中有一个微调器(下拉列表)。我想根据“IntroductionFragment”中微调器项的选择以编程方式更改“RequirementFragment”的布局。请帮助我 HomeLoanTabMainActivity.javaJava 如何根据第一个选项卡中微调器项的选择更改第二个选项卡的xml布局,java,android,Java,Android,我添加了3个选项卡,如下所示。在“IntroductionFragment”中有一个微调器(下拉列表)。我想根据“IntroductionFragment”中微调器项的选择以编程方式更改“RequirementFragment”的布局。请帮助我 HomeLoanTabMainActivity.java public class HomeLoanTabMainActivity extends FragmentActivity implements ActionBar.TabListener { p
public class HomeLoanTabMainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Introduction", "Requirement", "Best offer" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_main_activity);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
TabsPagerAdapter.java
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new IntroductionFragment();
case 1:
return new RequirementFragment();
case 2:
return new BestOfferFragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
IntroductionFragment.java
public class IntroductionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(
R.layout.introductionform_layout, container, false);
return rootView;
public class RequirementFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(
R.layout.requirementFragment_layout, container, false);
return rootView;
}
}
}
}
RequirementFragment.java
public class IntroductionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(
R.layout.introductionform_layout, container, false);
return rootView;
public class RequirementFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(
R.layout.requirementFragment_layout, container, false);
return rootView;
}
}
试试看
在RequirementFragment.java中添加onResume方法,如下所示:
public class RequirementFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(
R.layout.requirementFragment_layout, container, false);
return rootView;
}
@Override
public void onResume() {
super.onResume();
Toast.makeText(getActivity(), "Just check if you see this every time", Toast.LENGTH_SHORT).show();
}
}
现在运行代码。如果您每次来回都会收到祝酒词,那么我们可以继续。您可以在活动级别将选择存储在变量中,然后在onCreateView中按要求更改布局根据此变量更改布局,或者您也可以尝试@AtulOHolic Sir我将微调器选择项存储在变量中“IntroductionFragment”但我如何在“RequirementFragment”中进行比较,因为“RequirementFragment”已在“HomeLoanTabMainActivity”中构造,并且onCreateView不会再次调用。是的,请尝试在onResume方法中访问它。我发现这个-@AtulOHolic谢谢你的快速回复,但是你能在我上面的示例代码中添加一些代码吗…如果我知道会显示Toast消息,那么我如何根据“onResume方法”中存储的变量更改“RequirementFragment”的布局..您能解释一下布局中需要做哪些更改吗?另外,您需要将所选内容保存在HomeLoanTabMainActivity中,以便在RequirementFragment中也可以访问。在恢复时检查该变量的值,并相应地更改布局。如果不清楚,请告诉我。实际上“IntroductionFragment”包含一个带有两个项目的微调器(比如说“Select your memberType”=>1.免费会员2.付费会员),然后根据选择类型,“RequirementFragment”必须显示相应的表单。请注意在“RequirementFragment”中有两种不同的表格必须由用户填写…有意义吗?