Floating Action Button not showing fully inside a fragment

前端 未结 9 556
再見小時候
再見小時候 2020-12-04 09:11

I am using FAB button along with RecyclerView in a Fragment. This Fragment is an instance of a TabViewPager. I am having a issue with the FAB button. I have placed the Recyc

9条回答
  •  误落风尘
    2020-12-04 09:52

    I kind of did what Gabriele suggested with moving the FAB to the containing activity. Also you will need to update the FAB's color/clickListener in onTabSelected:

    public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            ...
    
            setFloatingActionButtonForImagesTab();
    
        }
    
        ...
    
        @Override
        public void onTabSelected(final TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                setFloatingActionButtonForImagesTab();
                break;
            case 1:
                setFloatingActionButtonForMusicTab();
                break;
            case 2:
                setFloatingActionButtonForVideoTab();
                break;
            }
        }
    
        ...
    
    }
    

    and then in the setup function just set the color and click listener:

    private void setFloatingActionButtonForImagesTab() {
        myViewPager.setCurrentItem(0);
        floatingActionButton.setBackgroundTintList(getResources().getColorStateList(R.color.purple));
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(coordinatorLayout, "images", Snackbar.LENGTH_LONG)
                    .show();
            }
        });
    }
    

    Make note of the call to setCurrentItem above. It is required now that you are implementing TabLayout.OnTabSelectedListener.

提交回复
热议问题