Fab 不可点击

Fab is not clickable

我正在开发一个 Android 应用程序,它有 4 个选项卡和 1 个可以在 4 个选项卡中看到的 fab。

在 Main_activity 中,我为不同的选项卡和不同的操作提供了不同的图标。我的问题是,当我打开应用程序时,第一个选项卡中的 fab 按钮无法点击。它只有在我 select 任何其他选项卡时才会被点击,并且重新 select 第一个选项卡,只有这样它才能正常工作。

这是我的Main_activity.java。

  public class Main_activity extends AppCompatActivity{

@Bind(R.id.main_activity)
View mView; 
private int[] iconIntArray = {
        R.drawable.book,
        R.drawable.car,
        R.drawable.cream,
        R.drawable.bus};
private int[] tabIcons = {
        R.drawable.school,
        R.drawable.park,
        R.drawable.ice,
        R.drawable.garage
};
private TabLayout tabLayout;
public FloatingActionButton fab; 
private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTransaction;
private NonSwipeableViewPager viewPager;

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

        setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    fab = (FloatingActionButton) findViewById(R.id.fab);

    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.viewpager, new bookFragment()).commit();

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

    viewPager = (NonSwipeableViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();

}

protected void animateFab(final int position) {
    fab.clearAnimation();

    // Scale down animation
    ScaleAnimation shrink = new ScaleAnimation(1f, 0.1f, 1f, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    shrink.setDuration(100);     // animation duration in milliseconds
    shrink.setInterpolator(new AccelerateInterpolator());
    shrink.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // Change FAB color and icon

            fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), iconIntArray[position]));

            // Rotate Animation
            Animation rotate = new RotateAnimation(60.0f, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            rotate.setDuration(150);
            rotate.setInterpolator(new DecelerateInterpolator());

            // Scale up animation
            ScaleAnimation expand = new ScaleAnimation(0.1f, 1f, 0.1f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            expand.setDuration(150);     // animation duration in milliseconds
            expand.setInterpolator(new DecelerateInterpolator());

            // Add both animations to animation state
            AnimationSet s = new AnimationSet(false); //false means don't share interpolators
            s.addAnimation(rotate);
            s.addAnimation(expand);
            fab.startAnimation(s);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    fab.startAnimation(shrink);
}

  private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    tabLayout.getTabAt(3).setIcon(tabIcons[3]);

    int tabIconColor = ContextCompat.getColor(Main_activity.this, R.color.green);

    tabLayout.getTabAt(0).getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);


    ColorStateList colors;
    if (Build.VERSION.SDK_INT >= 23) {
        colors = getResources().getColorStateList(R.color.ash, getTheme());
    }
    else {
        colors = getResources().getColorStateList(R.color.red);
    }

    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        assert tab != null;
        Drawable icon = tab.getIcon();

        if (icon != null) {
            icon = DrawableCompat.wrap(icon);
            DrawableCompat.setTintList(icon, colors);
        }
    }

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
            animateFab(tab.getPosition());

            switch (tab.getPosition()) {
                case 0:
                    viewPager.setCurrentItem(0);

                    fab.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                        //do this and that 
                        }
                    });
                    break;
                case 1:
          viewPager.setCurrentItem(1);
                        fab.setOnClickListener(view -> {
                          //do this and that 
                     });
                    break;
                case 2:
                  viewPager.setCurrentItem(2);
                        fab.setOnClickListener(view -> {
                            //do this and that 
                     });
                    break;
                case 3:
                    viewPager.setCurrentItem(3);
                    fab.setOnClickListener(view -> {

      //do this and that 
                    });
                    break;
                default:
                    break;
            }

        }


        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            tab.getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);

        }

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


        }
    });

}

      private void setupViewPager(NonSwipeableViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new bookFragment(), "books");
    adapter.addFragment(new carFragment(), "Cars");
    adapter.addFragment(new iceFragment(),"Ice");
    adapter.addFragment(new garage_fragment(), "garrage");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return null;
    }
}

   }

使用此代码,每次切换选项卡时,您都会为您的 fab 设置不同的 onClickListener。

您应该在全局范围内为您的 fab 设置一个 onClickListener,在其中您应该检查您所在的选项卡并调整您的行为。