How can I call ViewPagerOnTabSelectedListener programmatically?

时间:2017-08-30 20:31:50

标签: android tabs android-viewpager

I'm trying to change the color of the tab icons in my TabLayout, actually I achived this using addOnTabSelectedListener, but this only works when I swipe between tabs. So I wanna that the icon color of the first tab be the same that the color when you change tab.

I tried with viewPager.setCurrentItem(), but this only work when the index passed is diferent that 0 (the first tab).

So, How can I call ViewPagerOnTabSelectedListener programmatically?

This is my code:

def random_permutation(iterable, r=None):
    "Random selection from itertools.permutations(iterable, r)"
    pool = tuple(iterable)
    r = len(pool) if r is None else r
    return tuple(random.sample(pool, r))

This is the app when Run it for fist time. tablayout

When swipe to the second tab (and back to the first tab), the icon color change properly.

tablayout icon enter image description here

1 个答案:

答案 0 :(得分:0)

我不知道我能正确理解你的问题,但我使用寻呼机视图并通过此算法处理它的过程。 (检查一下,也许你找到了你想要的东西)。

1-首先确定标签。

private void createTabIcons(){
    // i get custom tab view.
    View profileTab = LayoutInflater.from(this)
            .inflate(R.layout.custom_tab,null); 
    // set it's views such as its text and icon
    ViewAppHolder.CustomTabViewHolder customTabProfileViewHolder =
            new ViewAppHolder.CustomTabViewHolder(profileTab);
     // set properties of icon ... note this is my main tab so i set 'color.active'
    customTabProfileViewHolder.TAB_IMAGE_VIEW
            .setImageDrawable(getDrawable(R.drawable.ic_person_black_24dp));
    customTabProfileViewHolder.TAB_IMAGE_VIEW
            .setColorFilter(ContextCompat.getColor(this,R.color.active_tab));

    tabLayout.getTabAt(Constants.USER_PROFILE_TAB).setCustomView(profileTab);
    // it's the next tab by the same previous steps
    View friendsTab = LayoutInflater.from(this)
            .inflate(R.layout.custom_tab,null);

    ViewAppHolder.CustomTabViewHolder customTabFriendsViewHolder =
            new ViewAppHolder.CustomTabViewHolder(friendsTab);
    // the diff is here .. i set the color of icon 'color.non_active'
    customTabFriendsViewHolder.TAB_IMAGE_VIEW
            .setImageDrawable(getDrawable(R.drawable.ic_group_black_24dp));
    customTabFriendsViewHolder.TAB_IMAGE_VIEW
            .setColorFilter(ContextCompat.getColor(this,R.color.non_active_tab));

/// .... and remain tabs also have the same thing with color non_active.

2-然后在onTabSelectedonTabUnselected我只调用名为setCurrentTab()的方法,它的实现是

private void setCurrentTab() {
    if (tabLayout != null) {
        int currentPosition = tabLayout.getSelectedTabPosition();
        int unSelectedTabs = currentPosition;

        do {
            unSelectedTabs = (unSelectedTabs + 1) % 4;

            Log.e("un selected ", String.valueOf(unSelectedTabs));
            Log.e("un selected ", String.valueOf(currentPosition));

            ViewAppHolder.CustomTabViewHolder customTabViewHolder =
                    new ViewAppHolder.CustomTabViewHolder(
                            tabLayout.getTabAt(unSelectedTabs).getCustomView()
                    );


            if (unSelectedTabs != currentPosition) {
                customTabViewHolder.TAB_IMAGE_VIEW
                        .setColorFilter(ContextCompat.getColor(this, R.color.non_active_tab));
            } else {
                customTabViewHolder.TAB_IMAGE_VIEW
                        .setColorFilter(ContextCompat.getColor(this, R.color.active_tab));
            }

        } while (unSelectedTabs != currentPosition);
    }
}