仿新浪TabLayout代码片段

来源:互联网 发布:因为你是范晓萱 知乎 编辑:程序博客网 时间:2024/05/16 19:49
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;    }}

0 0
原创粉丝点击