gpt4 book ai didi

android - 未调用滑动时的 tabLayout.setOnTabSelectedListener

转载 作者:行者123 更新时间:2023-11-29 01:19:59 25 4
gpt4 key购买 nike

我试图在选择或滑动 viewPager 时突出显示选项卡图标。为此,我使用了 'tabLayout.setOnTabSelectedListener()'。但是当我滑动标签时它不会突出显示,但是当我按下标签时选择所有工作完美并且标签被突出显示。可能与gradle版本有关。我正在使用 'classpath 'com.android.tools.build:gradle:2.1.0'' 。这是 Activity :

 private int[] tabIcons = {
R.drawable.tab_icon_home,
R.drawable.tab_icon_cart2,
R.drawable.tab_icon_aksia,
R.drawable.tab_icon_menu
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_tabs);

toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);

frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(pageChangeListener);
viewPager.setOffscreenPageLimit(10);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();

}


private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

int currentPosition = 0;

@Override
public void onPageSelected(int newPosition) {

FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();

FragmentLifecycle fragmentToShow = (FragmentLifecycle) adapter.getItem(newPosition);
fragmentToShow.onResumeFragment();

currentPosition = newPosition;
adapter.notifyDataSetChanged();

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

public void onPageScrollStateChanged(int arg0) { }
};

private void setupTabIcons() {


tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);

tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);



tabLayout.setOnTabSelectedListener( new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);

tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);


}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);

tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
}

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

}
});


}

最佳答案

我找到了解决方案,在我使用的gradle中

dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0' }

我在 appcompat-v7 和设计中都从 23.0.0 更改为 23.0.1。现在一切都为我工作。

关于android - 未调用滑动时的 tabLayout.setOnTabSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com