如何添加Tablayout图标

时间:2015-11-06 09:54:44

标签: java android

我有一个问题,如何为tablayout实现图标。

我有3个标签,对于所有标签,我选择了未选中的图标。 (白 - 黄色)。

但是,不知道如何实施这些。

这是我的UserActivity类:

public class UserActivity extends AppCompatActivity {
    FragmentPagerAdapter adapterViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        adapterViewPager = new UserFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapterViewPager);

        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabTextColors(getResources().getColorStateList(R.color.selector));
    }

UserFragmentPageAdapter:

 public UserFragmentPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {

        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0: // Fragment # 0 - This will show FirstFragment
                return SmallMoveFragment.newInstance(0, "Small Move");
            case 1: // Fragment # 0 - This will show FirstFragment different title
                return DeliveryFragment.newInstance(1, "Delivery");
            case 2: // Fragment # 1 - This will show SecondFragment
                return GarbageFragment.newInstance(2, "Garbage");
            default:
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: // Fragment # 0 - This will show FirstFragment
                return "Small Move";
            case 1: // Fragment # 0 - This will show FirstFragment different title
                return "Delivery";
            case 2: // Fragment # 1 - This will show SecondFragment
                return "Garbage ";
            default:
                return "No name";
        }
    }
}

以及3个标签中的一个:

   public class DeliveryFragment extends Fragment {
        // Store instance variables
        private String title;
        private int page;

        // newInstance constructor for creating fragment with arguments
        public static DeliveryFragment newInstance(int page, String title) {
            DeliveryFragment fragmentFirst = new DeliveryFragment();
            Bundle args = new Bundle();
            args.putInt("someInt", page);
            args.putString("someTitle", title);
            fragmentFirst.setArguments(args);
            return fragmentFirst;
        }

        // Store instance variables based on arguments passed
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            page = getArguments().getInt("someInt", 0);
            title = getArguments().getString("someTitle");
        }

        // Inflate the view for the fragment based on layout XML
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_delivery, container, false);
            TextView tvLabel = (TextView) view.findViewById(R.id.DeliveryTxt);
            tvLabel.setText(page + " -- " + title);
            return view;
        }
    }

任何想法在哪里以及如何......我会用TabHost来做,但我想这样做。

我在drawable / hdpi中有6个图标48x48。

1 个答案:

答案 0 :(得分:0)

有一个解决方案,但不是很好。

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int pos = tab.getPosition();
            switch (pos) {
                case 0 :
                    tab.setIcon(R.drawable.select_0);
                    break;
                case 1 :
                    tab.setIcon(R.drawable.select_1);
                    break;
                case 2 :
                    tab.setIcon(R.drawable.select_2);
                    break;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            int pos = tab.getPosition();
            switch (pos) {
                case 0 :
                    tab.setIcon(R.drawable.unselect_0);
                    break;
                case 1 :
                    tab.setIcon(R.drawable.unselect_1);
                    break;
                case 2 :
                    tab.setIcon(R.drawable.unselect_2);
                    break;
            }
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

但是此代码可能位于您的用户活动中,pagerAdapter不支持带有图标的标签。