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

时间:2015-05-19 10:33:17

标签: java android android-actionbar tabbar viewpagerindicator

// 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();
    }
  }
}

我的输出是,

enter image description here

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

我有5个类(first.java,second.java,third.java,four.java,five.java)和每个标签的5个布局(分别是first.xml)

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

感谢。

3 个答案:

答案 0 :(得分:2)

好的,您可以创建自己的Fragments,然后在getItem(int position)中将其返回;例如,您可以拥有Fragments日历,Feed等数组,然后在每个位置返回一个。

<强> // SampleTabsWithIcons.java

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

.....

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

答案 1 :(得分:2)

您只需使用此选项并将标签设置为viewPager ...

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

答案 2 :(得分:1)

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

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