Change Fragment With Viewpager
I am using PagerSlidingTab Library for ViewPager. And I want to change Fragment while scrolling of tabs. It is working fine. Check out my code. I am using AsynTask() on each Fragm
Solution 1:
Explanation:
It turns out there is an easier implementation for scrollable tabs which doesn't involve another library. You can easily implement tabs into your app using normal Android code straight from the default SDK.
The Code
Main Class:
publicclassPageSlidingTabStripFragmentextendsFragment {
//VariablesprivateViewPager viewPager;
privatePagerTitleStrip pagerTitleStrip;
publicPageSlidingTabStripFragment() {
// Required empty public constructor
}
@OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Find your pager declared in XML
viewPager = (ViewPager) getView().findViewById(R.id.pager);
//Set the viewPager to a new adapter (see below)
viewPager.setAdapter(newMyAdapter(getFragmentManager()));
//If your doing scrollable tabs as opposed to fix tabs,//you need to find a pagerTitleStrip that is declared in XML//just like the pager
pagerTitleStrip = (PagerTitleStrip)
getView().findViewById(R.id.pager_title_strip);
}
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.[your layout name here], container, false);
}
}
Adapter:
//Note: this can go below all of the previous code. Just make sure it's//below the last curly bracket in your file!classMyAdapterextendsFragmentStatePagerAdapter {
publicMyAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int arg0) {
Fragmentfragment=null;
if (arg0 == 0) {
fragment = newInstantOpportunity();
}
if (arg0 == 1) {
fragment = newEvents();
}
if (arg0 == 2) {
fragment = newExperts();
}
return fragment;
}
@OverridepublicintgetCount() {
return3;
}
@Overridepublic CharSequence getPageTitle(int position) {
if (position == 0) {
return"Instant Opportunity";
}
if (position == 1) {
return"Events";
}
if (position == 2) {
return"Experts";
}
returnnull;
}
}
Conclusion:
I hope this helps you understand another way to make scrollable tabs! I have examples on my Github Page about how to make each type (That being Fixed or Scrollable).
Links:
- Fixed Tabs Example - Click Here
- Scrollable Tabs Example - Click Here
Hope this helps!
Edit:
When asked what to import, make sure you select the V4 support fragments.
Solution 2:
please use this example..its very easy.i already implement that.
hope its useful to you.its best example of pager-sliding-tabstrip.
Use
framelayout compulsory:
FrameLayout fl = new FrameLayout(getActivity());
fl.addView(urFragementView);
and then set your fragement view in this framelayout.
Post a Comment for "Change Fragment With Viewpager"