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;
}
Related
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.
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!
I'm learning developing for android but i don't know how to create the UI for a Fragment.
I created a new activity and during the creation process I selected the navigation type "Tabs + Swipe".
Now i have a layout xml which i can't modify using the WYSIWYG interface and if i -for example- create a button widget using java in the class file it creates it in every "tab view".
I basically want to create different interfaces for every tab (fragment).
Thank you
In just created Activity you can find inner class SectionsPagerAdapter. Look at this method:
#Override
public Fragment getItem(int i) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
This method for every tab returns instance of DummySectionFragment with only different bundles. If you want to create fragments with different views for every tab you should check value of i variable and according to this value create proper fragment. For example:
#Override
public Fragment getItem(int i) {
Fragment fragment;
switch(i){
case 0:
fragment = new MyFragment1();
break;
case 1:
fragment = new MyFragment2();
break;
case 3:
fragment = new MyFragment3();
break;
default:
throw new IllegalArgumentException("Invalid section number");
}
//set args if necessary
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
Instead of DummySectionFragment class create three classes: MyFragment1, MyFragment2, MyFragment2 and for each, inside method onCreateView create or inflate view, for example:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_fragment1.xml, null);
return v;
}
Where R.layout.my_fragment1.xml is layout of your MyFragment1 fragment.