如何在android中的滑动选项卡中显示每个选项卡的Toast消息?

时间:2015-04-23 12:16:54

标签: android android-fragments tabs

当我运行此代码时,两个不同片段中的toast显示在一个选项卡上。当我滑动到下一个标签时,不会显示任何内容。

这是我的主要标签活动:

   public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {


private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Offers", "Distance", "Happy Hours","Shop List" };

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            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) {
}

}

这是适配器类:

public class TabsPagerAdapter extends FragmentStatePagerAdapter {
Bundle bundle = new Bundle();

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {

    case 0:
        Fragment of = new OfferFragment();
        bundle.putString("OfferFragment", "OfferFragment");
        of.setArguments(bundle);
        return of;

    case 1:
        Fragment df = new DistanceFragment();
        bundle.putString("DistanceFragment", "DistanceFragment");
        df.setArguments(bundle);
        return df;

    case 2:
        Fragment hf = new HappyHoursFragment();
        bundle.putString("HappyHoursFragment", "HappyHoursFragment");
        hf.setArguments(bundle);
        return hf;

    case 3:
        Fragment sf = new ShoplistFragment();
        bundle.putString("ShoplistFragment", "ShoplistFragment");
        sf.setArguments(bundle);
        return sf;
    }

    return null;
}

@Override
public int getCount() {

    return 4;
}

}

这是我的第一个片段:

public class OfferFragment extends Fragment {
private String name;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // public static final String ARG_OBJECT = "object";

    View rootView = inflater.inflate(R.layout.offerfragment, container,
            false);

    Bundle args = getArguments();
    TextView txtview = (TextView) rootView.findViewById(R.id.offer);

    name = args.getString("OfferFragment");

    txtview.setText(args.getString("OfferFragment"));

    display();

    return rootView;
}

private void display() {
    // TODO Auto-generated method stub
    Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
}
}

这是我的第二个片段:

public class DistanceFragment extends Fragment {

private String name;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.distancefragment, container, false);

    Bundle args = getArguments();
    TextView txtview = (TextView) rootView.findViewById(R.id.distance);

    name = args.getString("DistanceFragment");

    txtview.setText(args.getString("DistanceFragment"));

    display();

    return rootView;
}

private void display() {
    // TODO Auto-generated method stub
    Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
}


}

任何建议表示赞赏。我坚持这个。

2 个答案:

答案 0 :(得分:0)

这是因为下一个片段是由寻呼机

创建的

在显示toast之前检查该片段是否可见

if(fragmentName.this.isVisible())
{
// do your stuff here
}

答案 1 :(得分:0)

在每个片段类中写入onResume()。

 @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) { 
        //Display Toast message here
    }
}