Fab不可点击

时间:2017-03-27 15:28:44

标签: android android-tabs android-tablayout floating-action-button

我正在开发一款Android应用程序,其中包含4个标签和1个工作室,可在4个标签中看到。

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

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

   }

1 个答案:

答案 0 :(得分:1)

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

你应该在全局设置一个onClickListener到你的工厂,在里面你应该检查你所在的选项卡并调整你的行为。