简体   繁体   中英

Page title does not appear in ViewPager after setting it from the viewpager adapter

the title not appear in the PagerTabStrip after i set it from the adapter

this is my activity

public class MainActivity extends AppCompatActivity {
    private ViewPager pager ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new PagerAdapter(getSupportFragmentManager(),this));
    }

}

this is my layout

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/titlepage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"

        />

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

and this is the adapter

public class PagerAdapter extends FragmentPagerAdapter {

public PagerAdapter(FragmentManager fm , Context ctxt) {
    super(fm);

}
@Override
public Fragment getItem(int position) {
    return MyFragment.newInstance(position);
}
@Override
public int getCount() {
    return 10;
}

@Override
public String getPageTitle(int position) {

    return "title" + position ;
}

and this is the theme used in application :-

  <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

the result :-

enter image description here

as u see in above picture there is no title appear in viewpager even i set the title in getPageTitle method , can someone tell me what is the error that prevent me to add the title in viewpager ?

Use this in your layout

<android.support.design.widget.AppBarLayout
            android:id="@+id/Parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="60dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />


            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </android.support.design.widget.AppBarLayout>

Its my code, you can customize it according to your convenience.

PagerTabStrip gets ignored by Google a lot. You may wish to consider switching to some other ViewPager tab indicator.

If your compileSdkVersion is 23, make sure that you are using the latest version of the v23 Android support libraries. Right now, that is 23.4.0 . So, for example, you might have a dependency on support-v4:23.4.0 . The problem that you are seeing was fixed .

If your compileSdkVersion is 24, keep an eye on this issue , as there seems to be a new bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM