我已经为四个选项卡实现了带有 viewPager
的 TabLayout
。
选项卡有一个 customView
,因为每个选项卡有两个 textView
(文本 + 不可见计数)。
因此,我可以在更改选项卡时处理很多 UI 内容,但是,我无法为看不见的 textView
设置动画。
我的自定义 XML 为 textView
及其父级 RelativeLayout
设置了 android:animateLayoutChanges="true"
。
另外,我已经尝试在实际选项卡上设置此功能,尝试了各种动画,但均无效。 Ps - 没有动画,我只是设置可见性(消失,可见),这是有效的。
private void updateUnseenOnTab(int position, int unseen) {
ViewGroup vg = (ViewGroup) mTabLayout.getChildAt(0);
ViewGroup vgTab = (ViewGroup) vg.getChildAt(position);
View view = vgTab.getChildAt(0);
if (view != null && view.findViewById(R.id.tab_unseen) instanceof TextView) {
final TextView unseenText = (TextView) view.findViewById(R.id.tab_unseen);
if (unseen <= 0) {
unseenText.setText("0");
Animation fadeOut = new AlphaAnimation(0, 1);
fadeOut.setDuration(1000);
unseenText.setAnimation(fadeOut);
} else {
String currentText = unseenText.getText().toString();
try {
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
unseenText.setAnimation(fadeIn);
unseenText.setText("" + ((TextUtils.isEmpty(currentText) ? 0 : unseen) + Integer.valueOf(currentText)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
最佳答案
我不知道你想为每个选项卡或选项卡内的每个 subview 设置动画。这将以 150 毫秒的延迟一个接一个地为每个选项卡设置动画。如果您想为一个选项卡中的每个子项设置动画,只需将其放入另一个循环即可。
tabs = (TabLayout) findViewById(R.id.tabs);
ViewGroup vg = (ViewGroup) tabs.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int i = 0; i < tabsCount; i++)
{
int delay = (i * 150) + 750; //this is starting delay
ViewGroup vgTab = (ViewGroup) vg.getChildAt(i);
vgTab.setScaleX(0f);
vgTab.setScaleY(0f);
vgTab.animate()
.scaleX(1f)
.scaleY(1f)
.setStartDelay(delay)
.setInterpolator(new FastOutSlowInInterpolator())
.setDuration(450)
.start();
}
关于android - 如何在 Android 的 TabLayout 中制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31493149/