Search code examples
javaandroidandroid-actionbartabbarviewpagerindicator

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

and my output is,

enter image description here

My question is instead of displaying numbers while selecting each tabs, I want to go to new fragment activity (ie, newpage.java). Now some numbers are showing on each tab selection. How can i implement this.

I have 5 class (first.java,second.java,third.java,four.java,five.java) and 5 layout for each tabs (first.xml... respectively)

I tried a lot but no success. Does anyone know the answer.

Thanks.


Solution

  • Ok you can make your own Fragments and then return them in the getItem(int position); for example, you could have an array of your Fragments calendar, feed, etc. then on each position you return one.

    //SampleTabsWithIcons.java

    Fragment[] tabFragments = new Fragment[] {CalendarFragment.newInstance(), 
                                               FeedFragment.newInstance()}
    
    .....
    
    @Override
    public Fragment getItem(int position) {
        return tabFragments[position];
    }