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)
Related
How can I avoid a recyclerview to reload when the tabs change?
For example, I have a TabLayout with three Tabs, my ViewPager is:
class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tab1Fragment();
case 1:
return new Tab2Fragment();
case 2:
return new Tab3Fragment();
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "tab1";
case 1:
return "tab2";
case 2:
return "tab3";
}
return null;
}
}
In the first one, I have a recyclerview, when I change the tabs and back to the first one, the list is reloaded.
There is a way do avoid this reload? Just keep the data loaded.
Android keeps only one fragment on either side of the current fragment in a ViewPager by default. So when you come back to your first tab from the third one, it will recreate the first tab which is what is happening in your case. Here is the documentation.
You need to keep all the Fragments in memory with
viewPager.setOffscreenPageLimit(<(number of fragments in view pager) - 1>) which is 2 in your case.
Of course, as the docs suggest, this number should be kept low especially if its a complex layout.
yourViewPager.setOffscreenPageLimit(2);
My SlidingTabLayout contains three tabs that uses same layout and absolutely same code except of few lines... In order to use different SQLite database tables in each of them I keep them as separate classes. To make it more clear, my public Fragment getItem() method in public class ViewPagerAdapter extends FragmentStatePagerAdapter looks like that:
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
}
I don't like this approach and I would like to know how can I achieve the same result using one class(make each instance of the class use different table of database). I think it could be done by passing something like unique ID to each instance so it knows which table to use, but I don't have clear understanding how can I pass something like that in this situation.
I think using the bundle might help you:
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
TabFragment tab1 = new TabFragment();
Bundle args = new Bundle();
args.putString("tableName", tableName1);
tab1.setArguments(args);
return tab1;
case 1:
TabFragment tab2 = new TabFragment();
Bundle args = new Bundle();
args.putString("tableName", tableName2);
tab2.setArguments(args);
return tab2;
case 2:
TabFragment tab3 = new TabFragment();
Bundle args = new Bundle();
args.putString("tableName", tableName3);
tab3.setArguments(args);
return tab3;
}
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);
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.
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!