I'm making a very simple app using the ViewPager. I would like to be able to start at a custom position when the getItem method is called. Below is my current code.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment fragment1 = new Fragment1();
return fragment1;
case 1:
Fragment fragment2 = new Fragment2();
return fragment2;
case 2:
Fragment fragment3 = new Fragment3();
return fragment3;
default:
return null;
}
I was wondering how it would be possible to start at position 1 instead of 0 when the getItem method is called and then to be able to swipe to position 0 or 2 from position 1.
Why don't you simply set selected item to 1 after setting adapter?
mPager.setCurrentItem(1);
Related
In this image I have a list.
What I want is to change the view of this Fragment to the image given below.
On the header, I want to add a search button. By clicking on the button the second image layout will appear and and by clicking the button again, it will fire a web service that returns with the response.
First of all add the search button.Initialize it in your main activity.In your project I suggest you to do with a single onclicklistner and a longclicklistner.For double tap you have to initialize a gesture.
I can provide you a code for any approaches but I strongly suggest you go with the first one.Reply mi accordingly I will provide you a code.
I think that what you are actually asking is "How to get a reference of the ViewPager's Fragment and do something with that".
If this is the case, you can have a look here, you see, its a very common and not that easy problem.
One way to solve ot correctly (providing that you are already using a FragmentStateAdapter) is doing something like the following:
In your activity:
private CustomFragment mHomeFragment;
private CustomFragment mOfferFragment;
private CustomFragment mFavoriteFragment;
private CustomFragment mInfoFragment;
and then, in your adapter
#Override
public Fragment getItem(int position) {
return CustomFragment.newInstance(position, null);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// save the appropriate reference depending on position
switch (position) {
case 0:
mHomeFragment = (CustomFragment) createdFragment;
break;
case 1:
mOfferFragment = (CustomFragment) createdFragment;
break;
case 2:
mFavoriteFragment = (CustomFragment) createdFragment;
break;
case 3:
mInfoFragment = (CustomFragment) createdFragment;
break;
}
return createdFragment;
}
Doing that, it will allow you to do:
mViewPager.setCurrentItem(1);
mOfferFragment.doSomething();
in your parrent Activity
So far I have only implemented ViewPager with one type of fragment.
Now I want to add navigation tabs and be able to slide sideways from fragment of type A to fragment of type B . Do I need to contain both types of fragments in one activity ? If so , does it matter which fragment will have the view pager?
thank you.
Neither of the fragments contain the ViewPager. That would be contained in this case in your activity. The navigation tabs sit above the Viewager. Look at this tutorial: https://github.com/codepath/android_guides/wiki/Sliding-Tabs-with-PagerSlidingTabStrip . You can specify what fragments go in the ViewPager in the getItem method of a FragmentPagerAdater.
#Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0:
fragment = fragment1.newInstance();
break;
case 1:
fragment = fragment2.newInstance();
break;
case 2:
fragment = fragment3.newInstance();
break; }
return fragment;
}
I followed this tutorial to create an app with four tabs in the action bar [tab1][tab2][tab3][tab4].
When I change from a tab to the nearest one, the destination tab doesn t refresh for example from 1->2 , 3->4 or 3->2, but the problem is when I change from 1->3 or 4, 2->4 ...(a far tab) the destination tab refresh, how can I disable this ?
In the tutorial code, a new instance of fragment is getting generated every time it went to background.
switch (index) {
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return new GamesFragment();
case 2:
// Movies fragment activity
return new MoviesFragment();
}
Instead of it create fragment at the top level i,e create once you start the class.
public class TabsPagerAdapter extends FragmentPagerAdapter {
TopRatedFragment frag1;
GamesFragment frag2;
MoviesFragment frag3;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
if(frag1 == null)
frag1 = new TopRatedFragment();
return frag1;
case 1:
//repeat same here
case 2:
//repeat same here
}
By this, if an instance of fragment already exist, then it will return that instance instead of new one.
Ok so,I'm new to Android programing and I have a question. I have a swipeview similar to the one in the Play Store,and I want each tab to contain a different listview fragment. I have 2 fragments ,one called LP and other PL ,lets say. How to I edit this part of code so it uses my fragments ?
#Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new Fragment1();
case 1:
return fragment = new Fragment2();
case 2:
return fragment = new Fragment3();
default:
break;
}
return fragment;
EDIT : So after listening to the advice given,it still doesent work. :-?? I have 2 separate java files for my fragments and it doesent seem to work unless i copy the classes and put them inside the main activity...I may be missing something ,so please feel free to point out any posibility
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment fragment = new Fragment1();
return fragment;
case 1:
Fragment fragment = new Fragment2();
return fragment;
case 2:
ListFragment fragment = new Fragment3();//for list fragments
return fragment;
default:
break;
}
return fragment;
You could use something like this:
#Override
public Fragment getItem(int position) {
Fragment result;
switch (position) {
case 0:
result = new LPFragment();
case 1:
result = new PLFragment();
default:
result = new DefaultFragment();
}
return result
}
You just need to return an instance of whatever fragment you want for each case in the switch.
If the PL and LP classes extends from Fragment then it's fine (though you may have some need to cast them elsewhere in the code as the function returns Fragment and not your own class types)
I am using ViewPager and i am creating Fragments dynamically inside getItem method of FragmentStatePagerAdapter as follows and i want to avoid recreation of Fragment inside this method so that the application does not crash.
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
Fragment fragment = new Fragment();
switch (position) {
case 0:
fragment = new CourseOverViewFragment();
break;
case 1:
fragment = new CourseSchedueListFragment();
break;
case 2:
fragment = new CourseSchedueListFragment();
break;
case 3:
fragment = new CourseNoteListFragment();
break;
case 4:
fragment = new ProjectListFragment();
break;
default:
Log.i(StudyManagerDataSource.LOG_TAG, "default");
break;
}
return fragment;
}
How ever i fond this stackoverflow answer usefull. But i am not able to assign tag
inside getItem method.
Thanks!