android - 如何在 Android 的 TabLayout 中制作动画?

标签 android android-animation android-tablayout

我已经为四个选项卡实现了带有 viewPagerTabLayout。 选项卡有一个 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/

相关文章:

android - Google Play 为我的 React Native 应用程序发出的调用/短信许可最终警告电子邮件

android - 导航架构组件 singleTask 或 singleInstance 在返回堆栈中

Android:方向改变时的后退按钮动画

java - 如何在用户可见时加载一个 fragment 数据并在加载一次后保留数据

android - 未选中选项卡下划线的 TabLayout 颜色

java - ClassCastException : android. widget.TextView 无法转换为 android.widget.ListView

java - Android 开发首选 XML API

android - fragment 在移除和动画结束后闪烁

java - 我想在连接到 websocket 时显示加载动画

android - TabLayout+ViewPager+Fragment 第一个tab 没有显示数据?