如何在选择相应的标签栏时跳转到 Fragment

how to jump to Fragment upon selection of respective tab bar

//SampleTabsWithIcons.java

public class SampleTabsWithIcons extends FragmentActivity {
    private static final int[] ICONS = new int[] {
        R.drawable.events,
        R.drawable.rss,
        R.drawable.user,
        R.drawable.bell,
        R.drawable.search
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.simple_tabs);

    FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());

    ViewPager pager = (ViewPager)findViewById(R.id.pager);
    pager.setAdapter(adapter);

    TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
    indicator.setViewPager(pager);
}

class GoogleMusicAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
    public GoogleMusicAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return TestFragment.newInstance(ICONS[position % ICONS.length]);
    }

    @Override public int getIconResId(int index) {
      return ICONS[index];
    }

  @Override
    public int getCount() {
      return ICONS.length;
    }
  }
}

//TestFragment.java

public final class TestFragment extends Fragment {
private static final String KEY_CONTENT = "TestFragment:Content";

public static TestFragment newInstance(int icons) {
    TestFragment fragment = new TestFragment();

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < 20; i++) {
        builder.append(icons).append(" ");
    }
    builder.deleteCharAt(builder.length() - 1);
    fragment.mContent = builder.toString();

    return fragment;
}

private String mContent = "???";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
        mContent = savedInstanceState.getString(KEY_CONTENT);
    }
}

@SuppressWarnings("deprecation")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    TextView text = new TextView(getActivity());
    text.setGravity(Gravity.CENTER);
    text.setText(mContent);
    text.setTextSize(20 * getResources().getDisplayMetrics().density);
    text.setPadding(20, 20, 20, 20);

    LinearLayout layout = new LinearLayout(getActivity());
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    layout.setGravity(Gravity.CENTER);
    layout.addView(text);

    return layout;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(KEY_CONTENT, mContent);
   }
}

//TestFragmentAdapter.java

class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
private static final int[] ICONS = new int[] {
    R.drawable.events,
    R.drawable.rss,
    R.drawable.user,
    R.drawable.bell,
    R.drawable.search
};

private int mCount = ICONS.length;

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

@Override
public Fragment getItem(int position) {
    return TestFragment.newInstance(ICONS[position % ICONS.length]);
}

@Override
public int getCount() {
    return mCount;
}

@Override
public int getIconResId(int index) {
  return ICONS[index % ICONS.length];
}

public void setCount(int count) {
    if (count > 0 && count <= 10) {
        mCount = count;
        notifyDataSetChanged();
    }
  }
}

我的输出是,

我的问题不是在选择每个选项卡时显示数字,而是想转到新片段 activity(即 newpage.java)。现在一些数字显示在每个选项卡选择上。我该如何实现它。

我有 5 个 class(first.java、second.java、third.java、four.java、five.java)和每个选项卡的 5 个布局(first.xml...分别)

我尝试了很多但没有成功。有谁知道答案吗

谢谢。

好的,你可以自己制作 Fragments 然后 return 它们在 getItem(int position);例如,您可以有一个 Fragments 日历、提要等数组,然后在每个位置 return 一个。

//SampleTabsWithIcons.java

Fragment[] tabFragments = new Fragment[] {CalendarFragment.newInstance(), 
                                           FeedFragment.newInstance()}

.....

@Override
public Fragment getItem(int position) {
    return tabFragments[position];
}

您可以简单地使用它并将选项卡设置为 viewPager ...

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    viewPager.setCurrentItem(tab.getPosition());
}

内部片段寻呼机适配器你可以这样做。

@Override
        public Fragment getItem(int position) {

            switch (position) {
            case 0:

                frag = UsageRecoSummaryFragemrent.newInstance(position);
                // changeColor(newColor);

                break;

片段内部

 public static UsageRecoSummaryFragemrent newInstance(int position) {
        UsageRecoSummaryFragemrent f = new UsageRecoSummaryFragemrent();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, 0);
        f.setArguments(b);

        return f;
    }