I am using ViewPager to allow user to swipe between fragments.
How can I add a the title of each fragment to the screen?
package com.multi.andres;
i
@Alex Lockwood Thanks you :) I still have a problem :/ It is my XML:
lcmeter.xml
Whit my original code in the first post if a put "TitlePageIndicator" before "android.support.v4.view.ViewPager" i can see the titles but i cannot swype only clicking the titles change the pages and my fragments are not showed but whit the XML on that way i can swype through my fragments but i cannot see the titles :/. I tried whit constructors on this way:
package com.multi.andres;
import java.util.List;
import java.util.Vector;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitleProvider;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class ViewPagerFragment extends FragmentActivity{
private PagerAdapter mPagerAdapter; //contiene el pager adapter
String[] titulosPaginas = { "APP 1", "APP 2" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.lcmeter); //layout que contiene el ViewPager
initialisePaging(); //inicializo las paginas
}
private void initialisePaging() {
List fragments = new Vector();
fragments.add(Fragment.instantiate(this, FragmentPrueba1.class.getName()));
fragments.add(Fragment.instantiate(this, FragmentPrueba2.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)super.findViewById(R.id.pager);
pager.setAdapter(this.mPagerAdapter);
//Agrega los titulos
TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titulos); //layout XML
titleIndicator.setViewPager(pager);
}
/** *************************************************************************************************
/** Clase: public class PagerAdapter extends FragmentPagerAdapter implements TitleProvider
/** Notas: extends FragmentPagerAdapter permite el uso de las paginas de ViewPager pero con Fragments
/** implements TitleProvider permite el uso de los titulos en las paginas
/** Funcion: crea paginas por las cuales deslizarse horizontalmente las cuales son usadas
****************************************************************************************************/
public class PagerAdapter extends FragmentPagerAdapter implements TitleProvider {
private List fragments;
public String getTitle(int position) {
// TODO Auto-generated method stub
return titulosPaginas[position]; // titulo de la pagina
}
public PagerAdapter(FragmentManager fm, List fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public Fragment getItem(int position) {
try {
FragmentPrueba1.class.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.fragments.get(position);
}
}
}
But i have a FC when i open the application. Here is my fragment both are equal:
package com.multi.andres;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class FragmentPrueba1 extends Fragment {
FragmentPrueba1 (){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return (LinearLayout)inflater.inflate(R.layout.prueba1, container, false);
}
}
It is fragments XML:
I have read about Fragments but i cannot get working Fragments whit titles and it doesnt make sense :/ I am new at this but i am learning and is pretty complicated to me this kind of things thank you very much for your help i really appreciate it :)
EDIT:
I think i have founded the problem, it is not on Java code, the problem is on XML file ViewPager:
That seems to "get over" the titles because if i use XML file of Google example which adds android:layout_weight="1" on android.support.v4.view.ViewPager, i can see the titles on the bottom of the screen, not on top instead but is a progress :)