3

我已经TabLayoutviewPager, 实现了四个选项卡。选项卡有一个customView, 因为每个选项卡有两个textView(文本 + 看不见的计数)。因此,我可以在更改选项卡时处理很多 UI 内容,但是,我无法为看不见的textView.

我的自定义 XMLandroid:animateLayoutChanges="true"textView和它的 parent都有RelativeLayout

另外,我已经尝试在实际选项卡上设置此功能,尝试了各种动画,但均无效。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();
            }
        }
    }
4

3 回答 3

2

我不知道您是否要为每个选项卡或选项卡内的每个子视图设置动画。这将以 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();
            }
于 2015-08-10T03:14:07.120 回答
1

这是一个很好的库:

https://github.com/ToxicBakery/ViewPagerTransforms

这是开发人员文档:

http://developer.android.com/training/animation/screen-slide.html

于 2015-07-18T19:01:35.927 回答
1

要为每个选项卡内的视图设置动画,您可以首先像这样为每个选项卡设置自定义视图

tabLayout.getTabAt(0).setCustomView(R.layout.custom_view);

现在您可以custom_view像这样为每个项目充气

View tabView = mTabLayout.getTabAt(0).getCustomView();
TextViewPlus tabText = (TextViewPlus)tabView(R.id.tab_text);
tabText.setScaleX(1.5f);

现在,您可以在选择选项卡或任何其他事件时调用动画。

请注意,您可能需要更新构建工具 v23 才能使用getCustomView()

于 2015-12-04T07:16:33.423 回答