ViewPager片段中的CircularFloatingActionMenu

时间:2015-03-15 11:20:06

标签: android android-fragments android-activity android-viewpager fragment

在我的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();
    }

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

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

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

然后将其设置为viewPager

pager.setOnPageChangeListener(....);

我希望它可以帮到你

答案 1 :(得分:0)

这正是我的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

                    }
                }    
            }