简体   繁体   中英

Android Studio - Floating Action Button not working on ViewPager Initialization

Please Help In Floating Action Button

I am trying to load the sharedFab(FloatingActionButton) with ViewPager. But it is not working when I load for first time (Before Page Change).

在此处输入图像描述

viewPagerMainAct.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0 :
                        sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical));
                        sharedFab.show();
                        sharedFab.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "New Chat", Toast.LENGTH_SHORT).show();
                            }
                        });
                        break;
                    case 1:
                        sharedFab.setImageDrawable(getDrawable(R.drawable.ic_sticky_notes));
                        sharedFab.show();
                        sharedFab.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "New Post", Toast.LENGTH_SHORT).show();
                            }
                        });
                        break;
                    case 2:
                        sharedFab.hide();
                        break;
                    default:
                        sharedFab.hide();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

You use addOnPageChangeListener it wait for your request before do something. So, most easy way, it's just add this sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical)) before viewPagerMainAct.

Second way it add picture in xml, like default picture. And then change it in changeListener.

Insert this line of code into the onCreate.

sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical));
                            sharedFab.show();

The first time you run the app, you don't put photos inside the button. But in the first click, this photo is placed

I totally agree that ur answers are correct but I am using a single FAB with two viewpager fragments and different work. And if it not loading at initialization then what we do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM