TabLayout与片段上的ViewPager - Lollipop设备

时间:2015-12-19 16:17:10

标签: android android-viewpager android-tablayout

我正在开发一个应用程序,在我的MainActivity中,我使用导航抽屉来切换框架布局上的碎片。

其中一个片段是帮助'片段,我使用Tab布局和viewpager在三个页面之间滑动,关于我们','帮助'& '与我们联系'

下面的棒棒糖设备上一切正常。操作栏似乎坚持使用标签布局(使用工具栏作为操作栏)。

但是在Lollipop设备上,它显示为与操作栏和tablayout分开。

如何在棒棒糖设备上实现相同目的?

MainActivity:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

NavigationView navigationView;
ActionBarDrawerToggle toggle;
DrawerLayout drawer;
FragmentManager fm = getSupportFragmentManager();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Title");

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.nav_help) {
         getSupportActionBar().setTitle("Help");
        fm.beginTransaction().replace(R.id.l_frame_layout, new Help(),"help").commit();
    } 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;}

Help.java

public class Help extends Fragment {
ViewPager pager;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.layout_help, container, false);
    return(rootView);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final TabLayout tabLayout = (TabLayout)getActivity(). findViewById(R.id.tllayout_helpTABLAYOUTtl);
    tabLayout.addTab(tabLayout.newTab().setText("ABOUT"));
    tabLayout.addTab(tabLayout.newTab().setText("HELP"));
    tabLayout.addTab(tabLayout.newTab().setText("CONTACT"));

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    actionBar = getActivity().getActionBar();

    pager = (ViewPager)getActivity().findViewById(R.id.vplayout_helpVIEVPAGERvp);
    pager.setAdapter(new MyPagerAdapter(getFragmentManager()));

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            pager.setCurrentItem(tab.getPosition());
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }
        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }
        @Override
        public void onPageSelected(int position) {


        }
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

class MyPagerAdapter extends FragmentStatePagerAdapter {
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos) {
        switch(pos) {

            case 0: AboutUs tab1 = new AboutUs();
                return  tab1;

            case 1: HelpRecharge tab2 = new HelpRecharge();
                return  tab2;

            case 2: ContactUs tab3 = new ContactUs();
                return  tab3;

            default: return  null;
        }
    }

   @Override
    public int getCount() {
        return 3;
    }
}}

enter image description here

2 个答案:

答案 0 :(得分:2)

好像你的风格xml中的操作栏设置了高程。

对于Android 5.0+,如果您要将操作栏样式设置如下:

<item name="android:elevation">0dp</item>

并且对于支持库兼容性,请使用:

<item name="elevation">0dp</item>

AppCompat灯光主题的样式示例:

<style name="Theme.MyApp.ActionBar" parent="style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
   <!-- remove shadow below action bar -->
   <!-- <item name="android:elevation">0dp</item> -->
   <!-- Support library compatibility -->
   <item name="elevation">0dp</item>
</style>

然后将此自定义ActionBar样式应用于应用主题:

<style name="Theme.MyApp" parent="Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/Theme.MyApp.ActionBar</item>
</style>

对于5.0之前的Android,也可以将其添加到您的应用主题:

<!-- Remove shadow below action bar Android < 5.0 -->
<item name="android:windowContentOverlay">@null</item>

答案 1 :(得分:1)

尝试通过添加以下代码动态设置工具栏的高程:

        appBarLayout.setElevation( 0 );