ViewPager 片段中的 CircularFloatingActionMenu

CircularFloatingActionMenu in Fragment of ViewPager

在我的 android 应用程序中,我有带有一些片段的 ViewPager。我想在其中一个片段中使用和显示圆形浮动操作按钮菜单,但 FAB 在所有片段中都可见。如何解决这个问题并仅在一个片段中显示 FAB。顺便说一句,它是那个图书馆:github

Fragment.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Floating Action Button
        //Create a button to attach the menu
        ImageView movieFAB = new ImageView(getActivity());
        movieFAB.setImageResource(R.drawable.ic_launcher);

        //Create menu items
        FloatingActionButton actionButton = new FloatingActionButton.Builder(getActivity()).setContentView(movieFAB).build();
        ImageView iconSortName = new ImageView(getActivity());
        iconSortName.setImageResource(R.drawable.icon_star);
        ImageView iconSortDate = new ImageView(getActivity());
        iconSortDate.setImageResource(R.drawable.icon_cloud);
        ImageView iconSortRatings = new ImageView(getActivity());
        iconSortRatings.setImageResource(R.drawable.icon_communication);

        SubActionButton.Builder itemBuilder = new SubActionButton.Builder(getActivity());
        SubActionButton buttonSortName = itemBuilder.setContentView(iconSortName).build();
        SubActionButton buttonSortDate = itemBuilder.setContentView(iconSortDate).build();
        SubActionButton buttonSortRatings = itemBuilder.setContentView(iconSortRatings).build();

        //Create the menu with the items
        FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(getActivity())
                .addSubActionView(buttonSortName)
                .addSubActionView(buttonSortDate)
                .addSubActionView(buttonSortRatings)
                .attachTo(actionButton)
                .build();
    }

感谢您的帮助!

创建 OnPageChangeListener 的实现,其中覆盖 onPageSelected 方法。

@Override
public void onPageSelected(int position) {
    if (position == VALID_POSITION) {
        fabMenu.setVisibility(View.GONE);
    } else {
        fabMenu.setVisibility(View.VISIBLE);
    }
}

然后设置到viewPager

pager.setOnPageChangeListener(....);

希望对你有帮助

这正是我的 OnPageChangeListener 的设置方式。我做了@Eldar Mensutov 所做的,但也确保隐藏了按钮!在我下面的具体案例中,我只希望菜单显示在我的第二个片段 (pageNumber == 1) 上,并为所有其他片段隐藏:

           mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int pageNumber) {
                    if (pageNumber == 1) {
                        actionButton.setVisibility(View.VISIBLE);
                        button2.setVisibility(View.VISIBLE);
                        button3.setVisibility(View.VISIBLE);
                        button4.setVisibility(View.VISIBLE);
                        button5.setVisibility(View.VISIBLE);
                    } else {
                        actionButton.setVisibility(View.GONE);
                        button2.setVisibility(View.GONE);
                        button3.setVisibility(View.GONE);
                        button4.setVisibility(View.GONE);
                        button5.setVisibility(View.GONE);
                    }
                    if(pageNumber == 0) {
                        TextView textElement = (TextView) findViewById(R.id.title);
                        textElement.setText("Fragment 1 Title");

                        // Do stuff here

                    }
                    if(pageNumber == 1) {
                        TextView textElement = (TextView) findViewById(R.id.title);
                        textElement.setText("Fragment 2 Title");

                        // Do stuff here

                    }
                    else if(pageNumber == 2) {
                        TextView textElement = (TextView) findViewById(R.id.title);
                        textElement.setText("Fragment 3 Title");

                        // Do stuff here

                    }

                    else if(pageNumber == 3) {
                        TextView textElement = (TextView) findViewById(R.id.title);
                        textElement.setText("Fragment 4 Title");

                        // Do stuff here

                    }
                }    
            }