2

4つのタブに表示される4つのタブと1つのfabを持つAndroidアプリで作業しています。Fabはクリックできません

Main_activityでは、私は異なるタブとさまざまなアクションのために工場にさまざまなアイコンを与えました。私の問題は、私がアプリを開くと、最初のタブのファブボタンは解除できないということです。他のタブを選択するとクリックされるだけで、最初のタブを再選択すると、それだけでうまく動作します。

これは私の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

このコードでは、タブを切り替えるたびに、別のonClickListenerをあなたのfabに設定します。

あなたのfabにはonClickListenerをグローバルに設定する必要があります。その中には、どのタブにいるかを確認して動作を調整する必要があります。

+0

ありがとうございました。出来た。 –

関連する問題