TabLayout和Viewpager - 用图像替换文本

时间:2016-10-01 13:02:24

标签: java android android-layout android-studio android-fragments

所以我想要一个tablayout和一个viewpager能够刷过不同的片段。

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);


    tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
    viewPager = (ViewPager) view.findViewById(R.id.viewPager);

    viewPagerAdapter = new ViewPagerAdapter(getFragmentManager());
    viewPagerAdapter.addFragments(new AFragment(), "Test1", getContext());
    viewPagerAdapter.addFragments(new BFragment(), "Test2", getContext());
    viewPagerAdapter.addFragments(new CFragment(), "Test3", getContext());

    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

    return view;
}

我的ViewPagerAdapter类看起来像这样:

public class ViewPagerAdapter extends FragmentPagerAdapter {

ArrayList<Fragment> fragments = new ArrayList<>();
ArrayList<String> tabTitles = new ArrayList<>();
Context context;

public void addFragments(Fragment fragments, String titles, Context context) {
    this.fragments.add(fragments);
    this.tabTitles.add(titles);
    this.context = context;
}

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

@Override
public Fragment getItem(int position) {
    return fragments.get(position);
}

@Override
public int getCount() {
    return fragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
           return tabTitles.get(position);
}
}

所以我设置viewPager的适配器包含所有信息,例如tabLayout的标题和片段数量。这是对的吗?

所以我现在要做的是用我的tabLayout上的标题替换某些图标。 我应该删除我的ViewPagerAdapter构造函数中的String参数并删除我的Arraylist还是我必须传递一个空字符串?

我需要将哪些代码添加到我的ViewPageAdapter类中才能将图标而不是字符串添加到布局中? 我在这里找到了一个可能的解决方案:How to add page title and icon in android FragmentPagerAdapter

所以我把方法改为:

@Override
public CharSequence getPageTitle(int position) {

    myDrawable = ContextCompat.getDrawable(context, R.drawable.ic_mail_outline_black_24dp);
    SpannableStringBuilder sb = new SpannableStringBuilder(" Page #"+ position); // space added before text for convenience

    myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);
    sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return sb;
}

这将我的标题更改为PAGE#0#1 ...但没有将图标添加到我的TabLayout。 谢谢!

修改

这也有效。我不知道自己做错了什么

tabLayout.getTabAt(i).setIcon();

2 个答案:

答案 0 :(得分:1)

请使用此代码作为图标

     mTabLayout.setupWithViewPager(mViewPager);
      for (int i = 0; i < mTabLayout.getTabCount(); i++) {
mTabLayout.getTabAt(i).setIcon(R.drawable.your_icon);
  }

答案 1 :(得分:0)

请替换此代码才能正常工作

@覆盖 public CharSequence getPageTitle(int position){

    myDrawable = ContextCompat.getDrawable(context, R.drawable.ic_mail_outline_black_24dp);
    title = tabTitles.get(position);
    SpannableStringBuilder sb = new SpannableStringBuilder("   " + title); // space added before text for convenience
    try {
        myDrawable.setBounds(5, 5, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
        sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    } catch (Exception e) {
        // TODO: handle exception
    }

tabTitles.set(位置,SB);   return tabTitles.get(position);

}