- 基础
planFragmentAdapter = new PlanFragmentAdapter(((FragmentActivity) context).getSupportFragmentManager(), titles, context);
viewPager.setAdapter(planFragmentAdapter);
viewPager.setCurrentItem(0);
factory = new TabIndicatorView.ViewPagerIndicatorFactory(viewPager);
tabIndicatorView.setTabIndicatorFactory(factory);
2.adapter:
package com.zjugis.onemap.mobile.patrolplan;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import com.zjugis.onemap.mobile.persistence.entity.Plan;
import com.zjugis.onemap.mobile.service.impl.PlanServiceImpl_;
import java.util.ArrayList;
import java.util.List;
public class PlanFragmentAdapter extends FragmentPagerAdapter {
private String[] titles;
private PlanListFragment planListFragment;
private ArrayList<Plan> results;
private Context context;
public PlanFragmentAdapter(FragmentManager fm) {
super(fm);
}
public PlanFragmentAdapter(FragmentManager fm, String[] titles, Context context) {
super(fm);
this.titles = titles;
this.context = context;
}
@Override
public Fragment getItem(int position) {
//position =1->fragment2;position =2->fragment3;;position =3->fragment4;
planListFragment = new PlanListFragment();
Bundle bundle = new Bundle();
if (position == 1 || position == 2 || position == 3) {
bundle.putInt("flag", position);
} else {
bundle.putInt("flag", 4);
}
planListFragment.setArguments(bundle);
return planListFragment;
}
@Override
public int getCount() {
return titles.length;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position] + "(" + PlanServiceImpl_.getInstance_(context).getplanCountByStatus(position, "1") + ")";
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
/** * 刷新标题 */ if (factory != null) { factory.notifyDataSetChanged(); } /** * 刷新内容 */ if (viewPager != null && viewPager.getAdapter() != null) { viewPager.getAdapter().notifyDataSetChanged(); }