I have an android DrawerLayout and it has 2 items, one of them is a fragment with a PagerTabStrip and it load their fragments but when I change the item of the DrawerLayout and I come back to the PagerTabStrip it loads the tabs, but doesn't their fragments.
This is my PagerTabStrip class
public class maintabs extends Fragment {
FragmentPagerAdapter adapterViewPager;
PagerTabStrip pagerTabStrip;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tabs_main, container, false);
ViewPager vpPager = (ViewPager) rootView.findViewById(R.id.vpPager);
pagerTabStrip = (PagerTabStrip) rootView.findViewById(R.id.pager_header);
pagerTabStrip.setDrawFullUnderline(true);
adapterViewPager = new SampleFragmentPagerAdapter(getFragmentManager());
vpPager.setAdapter(adapterViewPager);
return rootView;
}
}
I've found a solution.
Just use a FragmentStatePagerAdapter instead FragmentPagerAdapter
Related
I have a TabsFragment, that is used to display FragmentStatePagerAdapter. To display tabs, I firstly create a TabsFragment, which creates multiple fragments, stored in tabs. The problem is, when I destroy a TabsFragment and create a new one, tab fragments created by previous TabsFragment are kept in memory for some reason. So does look like my TabsFragment class:
public class TabsFragment extends AbstractFragment {
public static final String TAG = "Tabs_Fragment";
private ViewPagerParallax viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tabs, container, false);
viewPager = (ViewPagerParallax)view.findViewById(R.id.viewpager);
viewPager.set_max_pages(CustomFragmentPagerAdapter.PAGE_COUNT);
viewPager.setBackgroundAsset(R.drawable.background);
viewPager.setAdapter(new CustomFragmentPagerAdapter(getFragmentManager()));
PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
tabsStrip.setViewPager(viewPager);
return view;
}
...
}
Like this I replace my TabsFragment with some other fragment:
public void transactOver(AbstractFragment what, int direction) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_content, what, what.getClassTag());
ft.commit();
}
Where R.id.fragment_content is a framelayout, that stores my fragmens. CustomFragmentPagerAdapter is coded as usual.
Finished up changing
viewPager.setAdapter(new CustomFragmentPagerAdapter(getFragmentManager()));
with
viewPager.setAdapter(new CustomFragmentPagerAdapter(getChildFragmentManager()));
I'm looking at a guide on how to use TabLayout. This is how they show to attach the PagerAdapter class.
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
However, their TabLayout is in an Activity, and mine is in a Fragment. I'm having trouble adapting their code to my use case, because I don't fully understand the practical differences between activities and fragments. Right now, when I try and swipe between tabs, nothing happens. Here is my code:
public class CandidateListFragment extends Fragment {
public static final String TAG = "candidates";
#InjectView(R.id.tabLayout) TabLayout tabLayout;
public CandidateListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_candidatelist, container, false);
initInstances(v);
return v;
}
private void initInstances(View v){
//TODO: Change to support below API level 21
getActivity().findViewById(R.id.toolbar_actionbar).setElevation(R.dimen.toolbar_candidatelist_elevation);
ViewPager viewPager = (ViewPager) v.findViewById(R.id.viewpager);
viewPager.setAdapter(new TabsFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
getActivity()));
ButterKnife.inject(this, v);
tabLayout.setupWithViewPager(viewPager);
}
#Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
I figured it out!!!
I had my xml layout file for the fragment set to RelativeLayout instead of LinearLayout, so my ViewPager wasn't on the screen, which meant I couldn't swipe. It's the simple mistakes that get me
I have ViewPager and when I open fragment I update my bd. But the previous and next Fragment update too.
I haven't more code for this just :
PagerAdapter pagerNewsAdapter = new PagerNewsAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(pagerNewsAdapter);
pager.setCurrentItem(getIntent().getExtras().getInt(ITEM_POSITION));
How to prevent the update Fragment n-1 and n + 1 ?
Fragment code :
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new, container,
false);
TextView txtTitle = (TextView) view.findViewById(R.id.titleNew);
txtTitle.setTypeface(custom_font_bold);
txtTitle.setText(ac.getTitre());
TextView resume = (TextView) view.findViewById(R.id.res);
resume.setTypeface(custom_font_bold);
resume.setText(Html.fromHtml(ac.getResume()));
resume.setTextColor(getResources().getColor(R.color.grey_500_transparent));
BD_ACTUALITE.updateViewAct(ac);
return view;
}
My ViewPager :
ArrayList<Fragment> fragments = new ArrayList<>();
for(Act act : arListNews) {
FragmentNew frg = new FragmentNew(act);
fragments.add(frg);
}
PagerAdapter pagerNewsAdapter = new PagerNewsAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(pagerNewsAdapter);
pager.setCurrentItem(getIntent().getExtras().getInt(ITEM_POSITION));
Thx all for answers.
It's ok,
I implement the ViewPager.OnPageChangeListener and Override onPageSelected()
Having problem showing Fragment inside a ViewPager.
I have a ViewPager which was hosted inside Fragment where this Fragment was inside the Activity.
This ViewPager has a pagerTabStrip. Each page in ViewPager are Fragment of special usage.
This is how I setup the viewpager with PagerAdapter
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pager_screen,
container, false);
ViewPager pager = (ViewPager) rootView.findViewById(R.id.vp_main);
pager.setAdapter(new MainTabAdapter(rootView.getContext(),getFragmentManager()));
return rootView;
}
This is fine, I can see the pager. Within the MainTabAdapter I have a getItem which I select differnt fragments.
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Log.w("ALBUM", types[arg0].toString());
Fragment fragment = FragmentType.getFragment(types[arg0]);
return fragment;
}
This is fine too. I can get the right fragment. When showing the Fragment of different page, none of them showing. Below is my code on one of the Fragment to inflating an xml
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_local_screen,
container, false);
Bundle data = new Bundle();
data.putStringArray("PROJECTION", new String [] {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,
MediaStore.Video.VideoColumns.BUCKET_ID,
MediaStore.Video.VideoColumns.DATE_TAKEN,
MediaStore.Video.VideoColumns.DATE_ADDED,
MediaStore.Video.VideoColumns.DESCRIPTION,
MediaStore.Video.VideoColumns.DURATION});
data.putString("SELECTION", null);
data.putStringArray("SELECTIONARRAY", null);
data.putString("SORTORDER", MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME + " LIMIT " + mLimit);
VideoCursorAdapter adapter = new VideoCursorAdapter(getActivity(),null,true);
LocalVideos videos = new LocalVideos(getActivity(), adapter);
getLoaderManager().initLoader(LocalVideos.LoaderID.THUMBNAIL.getID(), data, videos);
ListView listView = (ListView)rootView.findViewById(R.id.listVideos);
//listView.setAdapter(adapter);
videos.setView(listView);
return rootView;
This works, if I remove the viewpager. When I put the ViewPager code, this fragment doesn't show up.
I have the following code for my fragment. I pretty much took this from a basic view pager example and slapped it into a fragment to test for a project I'm working on.
I'm using ACtionBarSherlock, and viewPagerIndicator libraries. The Fragment below contains a viewPager that in this example is supposed to swipe between simple title strings.
I tried the example with the ViewPager and ViewPagerIndicator in an activity, and no problem. But the second I put it into a fragment. BOOM, nothing but a black screen... NO errors, just a black screen...I feel like I must be inflating, or accessing the layout incorrectly or something...
Any ideas?
Here's the code:
public class TestFragment extends SherlockFragment {
//~Constants----------------------------------------------
//~Data Fields--------------------------------------------
//~Constructors--------------------------------------------
#Override
public void onCreate(Bundle savedInstanceState) {
//Setup view
super.onCreate(savedInstanceState);
View view = onCreateView(getLayoutInflater(savedInstanceState), null, savedInstanceState);
ViewPagerAdapter adapter = new ViewPagerAdapter( this.getSherlockActivity() );
ViewPager pager =
(ViewPager)view.findViewById(R.id.free_time_day_pager);
TitlePageIndicator indicator =
(TitlePageIndicator)view.findViewById(R.id.titles);
pager.setAdapter( adapter );
indicator.setViewPager( pager );
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.free_time_fragment_layout, null, false);
return view;
}
//~Methods-------------------------------------------------
}
Dont call onCreateView that is wrong.
onCreateView is an overridden method which is called by the FragmentManger to handle the Fragment lifecycle.
private ViewPager mViewPager;
private TitlePagerIndicator mIndicator;
// Called first
#Override
public void onCreate(Bundle savedInstanceState) {
//Setup view
super.onCreate(savedInstanceState);
}
// Called second
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.free_time_fragment_layout, null, false);
return view;
}
// Called third
#Override
public View onViewCreated(View v){
super.onViewCreated(v);
mViewPager = (ViewPager) v.findViewById(R.id.free_time_day_pager);
mIndicator = (TitlePageIndicator)v.findViewById(R.id.titles);
}
public void onActivityCreated(Bundle bdl){
super.onActivityCreated(bdl);
ViewPagerAdapter adapter = new ViewPagerAdapter( getSherlockActivity() );
mViewPager.setAdapter(adapter);
mIndicator.setViewPager(pager);
}
Also be aware if you are using a FragmentPagerAdapter you require a special method to create the ViewPagerFragment, its a bit tricky but refer to this gist.
Read about Fragment Lifecycles here too.
Cheers
Use this.
compile 'com.android.support:appcompat-v7:23.0.1'
work for me.