问题描述
我正在使用 ViewPager 允许用户在片段之间滑动.
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;
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
private static 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<Fragment> fragments = new Vector<Fragment>();
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<Fragment> fragments;
public String getTitle(int position) {
// TODO Auto-generated method stub
return titulosPaginas[position]; // titulo de la pagina
}
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
}
}
但它没有显示 ViewPager 的标题,我不知道为什么.我以前使用 ViewPager 与标题,但没有与片段一起使用,我现在无法使标题正常工作.
But it doesn't show the titles of ViewPager and I don't know why. I used ViewPager with titles before but not with fragments and I cannot get titles working now.
推荐答案
您也可以使用 Jake Wharton 的 ViewPagerIndicator 库来得到想要的效果.Dave**04 的答案也有效,但它需要您引用整个 ActionBarSherlock 库,如果您只需要一个支持自定义/样式标题的 ViewPager,那么这不是可取的.
You can also use Jake Wharton's ViewPagerIndicator library to get the desired effect. Dave**04's answer works too, but it requires you to reference the entire ActionBarSherlock library, which isn't as preferable if you only need a ViewPager that supports custom/styled titles.
设置它以使其正常工作只需编写一点点 XML,在 Activity 中初始化 ViewPager,然后实现 FragmentAdapter(其中 extends FragmentPagerAdapter 实现 TitleProvider)指定哪些页面保存哪些 Fragment.
Setting it up to work correctly is simply a matter of writing a tiny bit of XML, initializing your ViewPager in your Activity, and implementing a FragmentAdapter (which extends FragmentPagerAdapter implements TitleProvider) to specify which pages hold which Fragments.
XML 布局
XML layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/titles"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
在你的Activity
//Set the pager with an adapter ViewPager pager = (ViewPager)findViewById(R.id.pager); pager.setAdapter(new CustomAdapter(getSupportFragmentManager())); //Bind the title indicator to the adapter TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles); titleIndicator.setViewPager(pager);
实现一个CustomFragmentAdapter
public static class CustomFragmentAdapter extends FragmentPagerAdapter
implements TitleProvider {
public static final int POSITION_PAGE_1 = 0;
public static final int POSITION_PAGE_2 = 1;
public static final int POSITION_PAGE_3 = 2;
private static final String[] TITLES = new String[] {
"Title 1",
"Title 2",
"Title 3"
};
public static final int NUM_TITLES = TITLES.length;
public CustomFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case POSITION_TITLE_1:
return PageOneFragment.newInstance();
case POSITION_TITLE_2:
return PageTwoFragment.newInstance();
case POSITION_TITLE_3:
return PageThreeFragment.newInstance();
}
return null;
}
@Override
public int getCount() {
return NUM_TITLES;
}
@Override
public String getTitle(int position) {
return TITLES[position % NUM_TITLES].toUpperCase();
}
}
<小时>
ViewPagerIndicator 库支持标题功能:
The ViewPagerIndicator library supports the titling features:
TitlePageIndicator
TabPageIndicator
CirclePageIndicator
生活不止眼前的狗血