简体   繁体   中英

Need assistance getting an Android Floating Action Button working properly on two tabs created with a ViewPager

As the title says, I have multiple tabs in my app that have been created with a ViewPager. I have an Android Floating Action Button that I want displayed only on two of the tabs. I've got this [mostly] working.

When I start my app, Tab #1 is shown. when I click the FAB on Tab #1, nothing happens. If I swipe to Tab #2 and click its FAB, that works perfectly. However, if I swipe back to Tab #1, then it's FAB works properly as well. Tab #1's FAB only works after I switch to a new tab, then back to Tab #1.

So, how do I fix the FAB on the default tab (Tab #1) when it first starts up?

Thanks!

Here is what I have:

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        final FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_01));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_02));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_03));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_04));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        viewPager = (ViewPager) findViewById(R.id.pager);
        pagerAdapter = new PagerAdapter (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                if (tab.getPosition() == Constants.TAB_INDEX_TAB_01) {
                    if (fab != null) {
                        fab.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent activity01Intent = new Intent(getApplicationContext(), Activity01.class);
                                if (activity01Intent != null) {
                                    try {
                                        startActivity(activity01Intent);
                                    } catch (ActivityNotFoundException anfe) {
                                        Toast.makeText(getApplicationContext(), "ERROR ...", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        });
                    }
                    fab.show();
                } else if(tab.getPosition() == Constants.TAB_INDEX_TAB_02){
                    if(fab != null) {
                        fab.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent activity02Intent = new Intent(getApplicationContext(), Activity02.class);
                                if (activity02Intent != null) {
                                    try {
                                        startActivity(activity02Intent);
                                    } catch (ActivityNotFoundException anfe) {
                                        Toast.makeText(getApplicationContext(), "ERROR ...", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        });
                    }
                    fab.show();
                } else {
                    fab.hide();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) { }

            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:backgroundTint="@color/DIMGRAY"
        android:src="@android:drawable/ic_input_add" />

</android.support.design.widget.CoordinatorLayout>

Just define it in Oncreate also hope it works.

if (fab != null) {
                            fab.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent activity01Intent = new Intent(getApplicationContext(), Activity01.class);
                                    if (activity01Intent != null) {
                                        try {
                                            startActivity(activity01Intent);
                                        } catch (ActivityNotFoundException anfe) {
                                            Toast.makeText(getApplicationContext(), "ERROR ...", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }
                            });
                        }
                        fab.show();

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