PagerTabStrip not showing in ViewPager in app targ

2019-05-07 04:52发布

问题:

I have an existing app that makes use of ViewPager and PagerTabStrip and this has been working for a long time with various releases of the Android SDK and the com.android.support:appcompat-v7 library. I've just bumped up the compile and target SDK versions of my app from 23 to 24 and the com.android.support:appcompat-v7 library version from 23.4.0 to 24.0.0... and I now see that the PagerTabStrip is totally missing from the ViewPager. (Not just missing titles in the PagerTabStrip... the PagerTabStrip is totally missing from the ViewPager when the views are rendered.)

This is how I'm declaring the ViewPager and PagerTabStrip views in my layout file:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pagerTabStrip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:textColor="@android:color/white"
        android:background="@color/view_bg_blue" />

</android.support.v4.view.ViewPager>

Can anyone spot something that I'm doing wrong or could this be a bug in ViewPager and PagerTabStrip?

回答1:

This is the temporary workaround until the issue is resolved in the Android support library:

((ViewPager.LayoutParams) pagerTabStrip.getLayoutParams()).isDecor = true;

See here for a fuller discussion on the status of the issue: https://code.google.com/p/android/issues/detail?id=213359



回答2:

We’ll start with the layout for our activity (**activity_pager_tab_strip.xml**), which in this case will just be a ViewPager containing a PagerTabStrip:
[![enter image description here][1]][1]
  [1]: http://i.stack.imgur.com/LUvF9.png

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class PagerTabStripActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pager_tab_strip);
        TabsPagerAdapter adapter = new TabsPagerAdapter(getSupportFragmentManager());
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
    }
}
Example: http://photo-wonder.blogspot.com/2016/09/using-pagertabstrip-with-viewpager.html