在其他标签上隐藏fab

时间:2016-09-05 02:13:15

标签: android floating-action-button

如何在其他选项卡上隐藏fab并仅在第一个选项卡上显示?我使用GET_ARGUMENTS在活动之间进行选择。请帮忙。这是我的代码。感谢。

public class OwnerTabs extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
SharedPreferences pref;
SharedPreferences.Editor editor;
private Button btnStart, btnStop;
private TextView tvCoordinates;
private BroadcastReceiver broadcastReceiver;
FloatingActionButton fab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_owner_tabs);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE);

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(2);

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

    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(OwnerTabs.this, InsertActivity.class);
            startActivity(in);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main,menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.action_logout){
        editor = pref.edit();
        editor.clear();
        editor.commit();
        Intent in = new Intent (getApplicationContext(), MainActivity.class);
        startActivity(in);
        finish();
    }

    return super.onOptionsItemSelected(item);
}


public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (getArguments().getInt(ARG_SECTION_NUMBER) == 1){
            View rootView = inflater.inflate(R.layout.content_ownerhome, container, false);
            return rootView;
        }
        else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
            View rootView = inflater.inflate(R.layout.content_rented, container, false);
            return rootView;
        }
        else if (getArguments().getInt(ARG_SECTION_NUMBER) == 3){
            View rootView = inflater.inflate(R.layout.content_gps, container, false);
            return rootView;
        }
        else {

            View rootView = inflater.inflate(R.layout.fragment_owner_tabs, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Your Cars";
            case 1:
                return "Pending Cars";
            case 2:
                return "GPS";
        }
        return null;
    }
}

感谢帮助人员,希望得到一个善意的回应,而不是一个傲慢的回应。 :)

1 个答案:

答案 0 :(得分:0)

添加一个on选项卡选定的侦听器,然后放入所需的选项卡索引,并调用fab.hide()