如何根据第一个选项卡android中的微调项目的选择来更改第二个选项卡的xml布局

时间:2014-12-02 12:24:13

标签: java android

我添加了3个标签如下。“IntroductionFragment”中有一个微调器(下拉列表)。我想根据“IntroductionFragment”中微调项的选择以编程方式更改“RequirementFragment”的布局。请帮助我

HomeLoanTabMainActivity.java

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;

}   }

RequirementFragment.java

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

1 个答案:

答案 0 :(得分:0)

试一试

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

  }

}

现在运行你的代码。如果你每次往返都会得到Toast,那么我们就可以继续了。