更新ActionBar上的片段按钮单击

时间:2014-08-19 15:46:22

标签: android android-fragments

我有一个带标签滑动视图的ViewPager和一个位于操作栏中的“刷新”按钮。我希望能够做的是在用户单击刷新按钮时重新创建每个选项卡片段:

public class TabPagerAdapter extends FragmentStatePagerAdapter {

    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int i) {
        switch(i) {
        case 0:
            return new F1();
        case 1:
            return new F2();
        case 2:
            return new F3();
        case 3: 
            return new F4();
        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 4; //# tabs
    }
}

MainActivity onOptionsItemSelected

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_help:
                makeDialog();
                return true;
            case R.id.action_refresh:
                Toast.makeText(this, "Refreshing...", Toast.LENGTH_SHORT).show();
                //code to refresh fragments in here
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

谢谢!

0 个答案:

没有答案