隐藏浮动操作按钮在CoordinatorLayout弹出 [英] Hidden Floating Action Button popping up in CoordinatorLayout

本文介绍了隐藏浮动操作按钮在CoordinatorLayout弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动 ViewPager ,其中显示了三种不同的片段。一切都是 CoordinatorLayout 里面。
我主要做的是一样的这样的回答:<一href=\"http://stackoverflow.com/questions/31596640/fab-animation-with-viewpager-tabslider/31663686#31663686\">FAB动画与viewpager / tabslider

I have an Activity with a ViewPager, which displays three different Fragments. Everything is inside a CoordinatorLayout. What I basically did is the same as in this answer: FAB animation with viewpager/tabslider

我不希望在第一个片段的FAB,所以我设置了FAB的能见度 GONE 在布局和显示/隐藏只有当次片段被选中。这部分实际上是正常工作。

I don't want to have the FAB in the first Fragment, so I set the FAB's visibility to GONE in the layout and show/hide it only when the 2nd Fragment is selected. This part actually works fine.

然而,当活动第一次创建(或屏幕旋转)的FAB上的第一个片段弹出一个快速的时刻,这实在是烦人。
当我更换 CoordinatorLayout 用别的东西,在FAB保持隐藏,当它应该的。

However, when the Activity is first created(or the screen is rotated) the FAB pops up on the first Fragment for a quick moment, which is really annoying. When I replace the CoordinatorLayout with something else, the FAB stays hidden when it should.

我使用的设计支持库23.0.1。该cheesesquare样品有同样的问题,当FAB设置为消失。

I'm using the design support library 23.0.1. The cheesesquare sample is having the same issue, when the FAB is set to be gone.

有人建议可以为这个解决方法?我无法找到源代码的 CoordinatorLayout ,所以我不能找一个之所以出现这种情况。

Can someone suggest a workaround for this? I couldn't find the sources for the CoordinatorLayout, so I couldn't look for a reason why this happens.

推荐答案

这里的code我想出了(没有,事先,它并不完美,也不是最优的。但它的工作。感觉免费优化您认为合适)。我会回来打扫一下,以一个更优雅的程度在以后的日子,但在得到你一个答案的兴趣与快速的工作,在这里你去。

Here's the code I came up with (and no, in advance, it is not perfect, nor is it optimal. But it does work. Feel free to optimize as you see fit). I will come back and clean this up to a more elegant degree at a later date, but in interest of getting you an answer to work with quickly, here you go.

private ViewPager viewPager;
private SparseArray<View.OnClickListener> floatingActionButtonOnClickListeners;
private FloatingActionButton floatingActionButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
    setupTabs();
    setFABOnClickListeners();
}

@Override
protected void onResume() {
    super.onResume();
    setFabVisibility(viewPager.getCurrentItem());
}

private void setupTabs() {
    FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            setFabVisibility(viewPager.getCurrentItem());
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            switch (state) {
                case ViewPager.SCROLL_STATE_DRAGGING:
                    floatingActionButton.hide();
                    break;
            }
        }
    });
}

private void setFabVisibility(int position) {
    View.OnClickListener floatingActionButtonClickListener = floatingActionButtonClickListeners.get(position);

    floatingActionButton.setOnClickListener(floatingActionButtonClickListener);

    if (floatingActionButtonClickListener == null) {
        hideFabForever();
    } else {
        showFabNormally();
    }
}

private void hideFabForever() {
    ((CoordinatorLayout.LayoutParams) floatingActionButton.getLayoutParams()).setBehavior(new FloatingActionButton.Behavior());
    floatingActionButton.hide();
}

private void showFabNormally() {
    ((CoordinatorLayout.LayoutParams) floatingActionButton.getLayoutParams()).setBehavior(new ScrollAwareFABBehavior(this, null, new ScrollBehaviorListener() {
        @Override
        public void onAnimatedOut(View view) {
        }

        @Override
        public void onAnimatedIn(View view) {
        }
    }));

    floatingActionButton.show();
}

private void setFABOnClickListeners() {
    if (floatingActionButtonOnClickListeners == null) {
        floatingActionButtonOnClickListeners = new SparseArray<>();
    }

    // An example, but populate the SparseArray with the position of the tab
    // that should have a FAB. This will be used to indicate that the FAB 
    // should be visible on that position.
    floatingActionButtonOnClickListeners.put(0, new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO handle click
        }
    });

     floatingActionButtonOnClickListeners.put(2, new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO handle click
        }
    });

    floatingActionButtonOnClickListeners.put(4, new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO handle click
        }
    });
}

一对夫妇的注意事项:

A couple of notes:

showFabNormally();

hideFabForever();

如果您有搅乱FAB的可见性的自定义行为仅仅是必要的。在这种情况下,我有一个自定义ScrollAwareFABBehavior导致该FAB滚动时向下滚动回升时再次出现消失。您可以选择只调用

are ONLY necessary if you have a custom behavior that messes up the visibility of the FAB. In this case, I have a custom ScrollAwareFABBehavior that causes the FAB to disappear when scrolling down and reappear when scrolling back up. You can opt to just call

floatingActionButton.show();

floatingActionButton.hide();

分别。我离开的行为code在那里来演示如何处理这使得一个FAB可用于所有选项卡,即使一个自定义的行为会影响其知名度。

respectively. I left the behavior code in there to demonstrate how to handle this so that one FAB can be used for all tabs, even if a custom behavior affects its visibility.

这篇关于隐藏浮动操作按钮在CoordinatorLayout弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆