需要帮助使Android浮动操作按钮在使用ViewPager创建的两个选项卡上正常工作



正如标题所说,我的应用程序中有多个使用 ViewPager 创建的选项卡。我有一个 Android 浮动操作按钮,我只想在两个选项卡上显示。我[大部分]都在工作。

当我启动我的应用程序时,将显示选项卡 #1。 当我单击选项卡 #1 上的 FAB 时,没有任何反应。如果我滑动到选项卡 #2 并单击其 FAB,则可以完美运行。但是,如果我滑动回选项卡#1,那么FAB也可以正常工作。选项卡 #1 的 FAB 仅在我切换到新选项卡然后返回选项卡 #1 后才有效。

那么,如何在首次启动时修复默认选项卡(选项卡 #1(上的 FAB?

谢谢!

这是我所拥有的:

主活动.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>

只需在Oncreate中定义它,也希望它有效。

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();

最新更新