public class HomeTabPagerAdapter extends FragmentPagerAdapter{
private String[] titles;
private Context context;
private LayoutInflater inflater;
private List<Fragment> fragments;
public List<Fragment> getFragments() {
return fragments;
}
public void setFragments(List<Fragment> fragments) {
this.fragments = fragments;
}
public void setTitles(String[] titles){
this.titles = titles;
}
public HomeTabPagerAdapter(FragmentManager fm,Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = null;
try {
fragment = (Fragment)super.instantiateItem(container, position);
}catch (Exception e){
}
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//super.destroyItem(container, position, object);
}
//此方法用来显示tab上的名字
@Override
public CharSequence getPageTitle(int position) {
return titles[position % titles.length];
}
/**
* 添加getTabView的方法,来进行自定义Tab的布局View
* @param position
* @return
*/
public View getTabView(int position){
View view=LayoutInflater.from(context).inflate(R.layout.home_tab_layout,null);
TextView tv= (TextView) view.findViewById(R.id.tab_textview);
tv.setText(titles[position]);
return view;
}
}
仿新浪TabLayout代码片段
最新推荐文章于 2022-12-12 11:54:56 发布