Using separated Intent for each fragmented tabs in Android - android

I'm trying to create tabs with swipe view using fragment. I found a solution but it provides a single dummy text layout for each tabs instead of using separated layout. I'd like to create three different classes with three different layouts and use them in tabs and if possible then using a custom background for each tabs (while selected/not selected). I can do all these things without fragment but using fragment it seems challenging.
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}

To achieve your target you must use ActionBarSherlock library, and to call other fragments you should not need to use Intent,
I have written same kind of program few months ago to call different Fragments i used below code
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
and to call show different - different layouts for each and every Fragment you should call different - different Fragments and also need to write XMLs too .....
FragmentTab1.java :-
public class FragmentTab1 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
fragment1.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Your Layout Goes Here for Fragment1" />
</RelativeLayout>
in a same way you have to write classes and xmls for all needed Fragments....
for more detailed information or to try out an example, use this :
http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/

in the following link that help you how to create Tab fragment in sperated intents:
Tab Fragments

Related

Impossible to add or remove pages dinamically from ViewPager Android Activity

I've read all stackOverflow answers regarding this topic, and any of them worked for me, or I couldn't make it myself.
As you can see in the code below, I added the default ViewPager Activity from Android Studio, and now I need to add new pages or remove existing pages from the corresponding buttons.
So, the activity is loading correctly and gets the correct number of pages, and I can also change the information in each of them successfully. As you can see, the app is connected to Firebase and I get from there the total number of pages (dogs) that my activity should have, this is working well.
Now, How can I add a new page, move to that page, and update the adapter and total number of pages?
Same for removing, How can I remove the current page, then move to another one and update the adapter with no problems?
I'm getting totally crazy with this, spent the whole day with diffeent solutions I found in stackoverflow, but could not make it. I'm now confused about when to use the "notifyDataSetChanged()", if it is a good idea override the "getItemPosition" or not, etc.
I would appreciate if someone could finally help me adapting the answer to my project, it's the default ViewPager just modified small parts to try to fix it, but don't know what else I can do.
Thank you in advance and sorry for my english.
Note: Some functions like "updateIntValueFromCurrentUser()" are declared in MainActivity but working, they are related to firebase queries etc so I'm not focusing on this.
public class DummyViewPagerActivity extends MainActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private static ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy_view_pager);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
//Creating menu bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dummy_view_pager, menu);
return true;
}
//Creating the two buttons to save and delete the images
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.add:
updateIntValueFromCurrentUser("dogs_number", person.getDogs_number()+1);
return true;
case R.id.edit:
return true;
case R.id.delete:
updateIntValueFromCurrentUser("dogs_number", person.getDogs_number()-1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
try {
rootView = inflater.inflate(R.layout.fragment_dummy_view_pager, container, false);
TextView dogName = (TextView) rootView.findViewById(R.id.dog_name);
dogName.setText(allDogs.get((getArguments().getInt(ARG_SECTION_NUMBER)) - 1).getName());
} catch (Exception e) {
Log.e("RaisedException()", e.getMessage());
}
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getItemPosition(Object object) {
// POSITION_NONE makes it possible to reload the PagerAdapter
return POSITION_NONE;
}
#Override
public int getCount() {
// Show x total pages.
return person.getDogs_number();
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
Adding/Removing items from FragmentViewPager is a bit tricky thing to do:
Try using this adapter and let me know is it working for you.
public class CodebaseFragmentPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<Fragment>();
public CodebaseFragmentPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void removeItem(int position){
mFragmentList.remove(position == 0 ? 0 : position - 1);
this.notifyDataSetChanged();
}
public void clearAllItems(){
mFragmentList.clear();
this.notifyDataSetChanged();
}
public void updateItem(int position, Fragment fragment){
mFragmentList.set(position, fragment);
notifyDataSetChanged();
}
#Override
public int getItemPosition(Object object) {
if (mFragmentList.contains(object)) return mFragmentList.indexOf(object);
else return POSITION_NONE;
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment) {
mFragmentList.add(fragment);
notifyDataSetChanged();
}
}

how To add application name on the top android application in which pagesrTitleStrip is used

i am creating a android application for a news website activity_main.xml has the following code
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="235px"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
this helps me to swipe the diffent pages i accomplish this by writing the following code in MainActivity.java
public class MainActivity extends FragmentActivity
{
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment2=new Fragment();
switch(position)
{
case 0:
Fragment1 fragment=new Fragment1();
return (Fragment) fragment;
case 1:
Fragment2 fragment1=new Fragment2();
return fragment1;
default:break;
}
return fragment2;
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
by using above code i can nevigate through 4 child pages session1,session2,session3,session4
but i am not able to give a common title(name off app) to the in the top which is common to all sessions.
so please give me sollution to my problem.it is better if i am able to set a backgroung of a PagerTitleStrip as a image
All you need to do is to set same value (title you want) to all your strings R.string.title_section1 & R.string.title_section2 & R.string.title_section3 & R.string.title_section4 or you can also use only one string at all places, choice is yours.

How to show different layouts in each Tab in a TabLayout using Fragments

I have been trying to show different layouts in different tabs in the swipeable TabLayout using PagerTabStrip. Can anybody help?
I want to show one layout in first tab, second different layout in 2nd tab etc.
public class MainActivity extends FragmentActivity {
// create object of FragmentPagerAdapter
SectionsPagerAdapter mSectionsPagerAdapter;
// viewpager to display pages
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the five
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A FragmentPagerAdapter that returns a fragment corresponding to one of
* the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#SuppressLint("NewApi")
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: {
//Show 1st Layout(Here I need HELP)
//HELP HELP HELP
}case 1:
{
//Show 2nd Layout(Here I need HELP)
//HELP HELP HELP
}
default:
}
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 5 total pages.
return 6;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Section 1";
case 1:
return "Section 2";
case 2:
return "Section 3";
case 3:
return "Section 4";
case 4:
return "Section 5";
case 5:
return "Section 6";
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setTextSize(25);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return textView;
}
}
}
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1: {
rootView = inflater.inflate(R.layout.fragment_bba, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_bcom, container, false);
break;
}
case 3: {
rootView = inflater.inflate(R.layout.fragment_bca, container, false);
break;
}
}
return rootView;
Ok for the people who want to solve this problem using design patterns.
Find full working solution Here.
If u write the fragment based on if-else condition it may solve the problem
switch(fragmentId)
{
case 1:
{
fragment 1 related stuff
}
case 2:
{
fragment 2 related stuff
}
.......
.......
and so on
But the problem with this approach is if in future,
1) you decide to add more fragments
or
2) you decide to change some functionality of existing fragment
Then you will have to modify the existing code (inside if-else condition)
Not a preferred programming practice
Instead you can follow this approach
public abstract class BasicFragment extends Fragment {
public BasicFragment newInstance()
{
Log.d("Rohit", "new Instance");
Bundle args = new Bundle();
// args.putInt(ARG_PAGE, page);
BasicFragment fragment = provideYourFragment();
fragment.setArguments(args);
return fragment;
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanseState)
{
View view = provideYourFragmentView(inflater,parent,savedInstanseState);
return view;
}
public abstract BasicFragment provideYourFragment();
public abstract View provideYourFragmentView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanceState);
}
Your Fragment implementation
public class ImageFragment extends BasicFragment{
#Override
public BasicFragment provideYourFragment() {
return new ImageFragment();
}
#Override
public View provideYourFragmentView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.image_fragment,parent,false);
//Get your parent layout of fragment
RelativeLayout layout = (RelativeLayout)view;
//Now specific components here
ImageView imageView = (ImageView)layout.findViewById(R.id.myImage);
imageView.setImageResource(android.R.drawable.ic_media_play);
return view;
}
}
Happy coding

Populate 5 fragment pages with separate arraylists

I've been stuck for days on this. I have an activity that will parse html and sort the information into 5 arraylists. Each arraylist is a weekday. I want to take each arraylist and display the information in a listview on a separate page, so monday will be displayed on one page and then by swiping you move to tuesday and so on.
I have taken the eclipse default scrollable tabs + swipe for navigation and I'm trying to build from there.
So essentially I want to populate 5 pages of data with 5 arraylists, 1 arraylist for each page. Any ideas how I assign an arraylist to listview in a specific page?
Here is the code I have so far
public class DisplayOnlineTimetable extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
String value;
Document doc = null;
private ListView mainListView ;
static ViewGroup mViewGroup;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_display_online_timetable_dummy);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_online_timetable, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return DummySectionFragment.newInstance(i);
}
#Override
public int getCount() {
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
//monday
case 0:
return getString(R.string.title_section1).toUpperCase(l);
//tuesday
case 1:
return getString(R.string.title_section2).toUpperCase(l);
//wednesday
case 2:
return getString(R.string.title_section3).toUpperCase(l);
//thursday
case 3:
return getString(R.string.title_section3).toUpperCase(l);
//friday
case 4:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public void createTimetable(ArrayList<SingleClass> list, Elements elements, Day day)
{
}
private class CreateTimetables extends AsyncTask<URL, Integer, Long>
{
protected Long doInBackground(URL... urls) {
}
protected void onPostExecute(Long result)
{
}
}
}
DummySectionFragment Class
public class DummySectionFragment extends ListFragment {
private Integer arrayListId;
ViewGroup myViewGroup;
public static final String CATEGORY_POSITION = "section_number";
public static DummySectionFragment newInstance(int pos) {
DummySectionFragment f = new DummySectionFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt(CATEGORY_POSITION, pos);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
//get the id for your array list
arrayListId = getArguments() != null ? getArguments().getInt(CATEGORY_POSITION) - 1 : 1;
}
//create the list view layout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myViewGroup = container;
View v = inflater.inflate(R.layout.list_row, container, false);
return v;
}
//populate the list view row
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<SingleClass> arrayAdapter =
new ArrayAdapter<SingleClass>(getActivity(), android.R.id.list, R.layout.list_row);
setListAdapter(arrayAdapter);
}
}
What you need to do is standard, and you are almost there.
First in your ViewPager adapter's getItem methods, initiate the Fragment with which position it is on:
#Override
public Fragment getItem(int i) {
return DummySectionFragment.newInstance(i);
}
Next, in your Fragment class, create a constructor to instantiate it:
public static DummySectionFragment newInstance(int pos) {
DummySectionFragment f = new DummySectionFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt(CATEGORY_POSITION, pos);
f.setArguments(args);
return f;
}
And now make your DummySectionFragment extend a ListFragment, and in the onActivityCreated method you can populate it:
public class DummySectionFragment extends ListFragment {
private Integer arrayListId;
//the constructor from above
#Override
public void onCreate(Bundle savedInstanceState) {
//get the id for your array list
arrayListId = getArguments() != null ? getArguments().getInt(CATEGORY_POSITION) - 1 : 1;
}
//create the list view layout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myViewGroup = container;
View v = inflater.inflate(R.layout.fragment_list, container, false);
return v;
}
//populate the list view row
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this, android.R.layout.list, <your_array_list_row.xml>);
setListAdapter(adapter);
}
}
The above code for your Fragment class will get the id for your array based on the position of the fragment, and create an ArrayAdapter for it, and use this adapter to populate a listview fragment. Now all you have to do is create the xml layout for the list:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
One last thing is make sure all your Fragment class are from the same set of library (eg. either the regular Fragment lib or support.v4).

Android: "Navigation Type: Fixed Tabs + Swipe"

I'm trying to use Tabs + Swipe in an App and want to use the Navigation Type "Fixed Tabs + Swipe" which the ADT provides me when creating an Activity.
Sooo now the ADT spits out nice Code, which I slightly modified...
I completely understand the code and what's going on... But how can I teach the App to use my three Fragments instead of the stupid Dummy Frag? :(
I cannot find any tutorial which deals with the ADTs "Navigation Types"...
Thanks for your help!
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
//Adding Tabs
actionBar.addTab(actionBar.newTab().setText("Tab 1").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab 2").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab 3").setTabListener(this));
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
a switch-case to set the fragments is easy and makes it really clear. Let each of your fragment inflate the root view in your xml
#Override
public Fragment getItem(int index) {
Fragment fragment = null;
switch(index){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
default:
break;
}
//set args if necessary (which it isn't?)
Bundle args = new Bundle();
args.putInt(ObjectFragment.ARG_OBJECT, index + 1);
fragment.setArguments(args);
//return fragment
return fragment;
}
But how can I teach the App to use my three Fragments instead of the stupid Dummy Frag?
You will notice that DummySectionFragment is referenced in getItem() of the SectionsPagerAdapter:
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
If you want to use different fragments, modify getItem() to return the fragment you want, given the supplied position (0-based page number).

Categories

Resources