向标签适配器添加图标,扩展FragmentPagerAdapter

时间:2014-09-23 08:58:13

标签: android android-fragments tabs

如何在标签适配器中添加标签而不是标题,这会在android中扩展FragmentPagerAdapter? 我不想在我的项目中使用操作栏

任何帮助吗??

 public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    private final int[] icons = {R.drawable.home,R.drawable.buddies,R.drawable.notification,R.drawable.history};

    @Override
    public CharSequence getPageTitle(int position) {
        if(position == 0)
            return "Home";
        else if(position == 1)
            return "Buddies";
        else if(position == 2)
            return "History ";
        else
            return "Notifications";
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                HomeFragment home = new HomeFragment();
                return home;
            case 1:
                return new BuddiesFragment();
            case 2:
                return new HistoryFragment();
            case 3:
                return new NotificationsFragment();
        }

        return null;
    }


    @Override
    public int getCount() {
        return 4;
    }

}

我也试过这种方法但不起作用

@覆盖         public int getPageIconResId(int position){             返回图标[位置];         }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return o == view;
    }

2 个答案:

答案 0 :(得分:1)

我使用矢量绘图作为我的标签图片,这仅适用于API> 21。 但我相信你可以用同样的方式使用图像绘图。

这是我的代码:

class MyPagerAdapter extends FragmentPagerAdapter {

    private String[] tabText = getResources().getStringArray(R.array.tabs);

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        tabText = getResources().getStringArray(R.array.tabs);

    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment=null;

        if (position == 0)
            fragment = new FragmentA();
        if (position == 1)
            fragment = new FragmentB();
        if (position == 2)
            fragment=new FragmentC();

        return fragment;
    }




    @Override
    public CharSequence getPageTitle(int position) {

        SpannableString spannableString = null;

        if (position == 0) {
            //use the MrVector library to inflate vector drawable inside tab
            Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_add);
            //set the size of drawable to 36 pixels
            drawable.setBounds(0, 0, 36, 36);
            ImageSpan imageSpan = new ImageSpan(drawable);
            //to make our tabs icon only, set the Text as blank string with white space
            spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (position == 1) {
            //use the MrVector library to inflate vector drawable inside tab
            Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_list);
            //set the size of drawable to 36 pixels
            drawable.setBounds(0, 0, 36, 36);
            ImageSpan imageSpan = new ImageSpan(drawable);
            //to make our tabs icon only, set the Text as blank string with white space
            spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (position == 2) {
            //use the MrVector library to inflate vector drawable inside tab
            Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_settings);
            //set the size of drawable to 36 pixels
            drawable.setBounds(0, 0, 36, 36);
            ImageSpan imageSpan = new ImageSpan(drawable);
            //to make our tabs icon only, set the Text as blank string with white space
            spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    @Override
    public int getCount() {
        return 3;
    }

带图片:

 @Override
    public CharSequence getPageTitle(int position) {

        SpannableStringBuilder sb = new SpannableStringBuilder(" ");

        if (position == 0) {
            Drawable drawable = getDrawable(R.drawable.ic_action_add);
            drawable.setBounds(0, 0, 48, 48);
            ImageSpan imageSpan = new ImageSpan(drawable);
            //to make our tabs icon only, set the Text as blank string with white space
            sb.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (position == 1) {
            Drawable drawable = getDrawable(R.drawable.ic_action_list_2);
            drawable.setBounds(0, 0, 48, 48);
            ImageSpan imageSpan = new ImageSpan(drawable);
            //to make our tabs icon only, set the Text as blank string with white space
            sb.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        return sb;
    }

MrVector liabrary:https://github.com/telly/MrVector
我用来将SVG转换为VectorDrawable的工具:http://inloop.github.io/svg2android/

答案 1 :(得分:1)

我使用此库https://github.com/pizza/MaterialTabs在项目中添加带有文本和图标的标签。

以下是一个示例,说明如何仅使用标题标题中的图标:https://github.com/pizza/MaterialTabs/blob/master/sample/src/io/karim/materialtabs/sample/MainActivity.java#L397

更改的范围是让FragmentPagerAdapter类实现MaterialTabs.CustomTabProvider接口。这会添加getCustomTabView方法,您可以在其中设置选项卡中的图标。

@Override
public View getCustomTabView(ViewGroup parent, int position) {
    ImageView imageView = new ImageView(context);
    imageView.setImageDrawable(getResources().getDrawable(ICONS[position]));
    return imageView;
}