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

在Main_activity中,我为fab提供了不同的图标,用于不同的选项卡和不同的操作。我的问题是,当我打开应用程序时,第一个选项卡中的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;
    }
}

   }

最佳答案

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

您应该全局为工厂设置一个onClickListener,并在其中检查您所处的选项卡并适应您的行为。

关于android - Fab不可点击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43050519/

10-12 02:32