ViewPager中的PagerTabStrip在運行測試的時候不顯示問題解決

   
   剛在使用ViewPager的時候,向其中添加PagerTabStrip,然而在運行的時候PagerTabStrip完全沒顯示出來,仔細審查了好幾遍代碼,算是完全沒問題,再運行還是不顯示,這是什麼問題,然後使用了PagerTitleStrip試了試,運行正常,然而PagerTabStrip是怎麼回事呢,苦思冥想,回過頭看其API,用法完全正確啊,哎,浪費了兩個多小時,實在無法了,stackoverflow一下PagerTabStrip,在搜索首頁就有個這樣的爲題:

在裏面有兩個回答:

1,這個問題和這個是同樣的問題:

PagerTabStrip not showing in ViewPager in app targeting Android 6.X (N)

目標是Android N的應用, 需要使用下面的解決辦法:

在 onCreateView(), 添加:

((ViewPager.LayoutParams) (view.findViewById(R.id.tab_strip)).getLayoutParams()).isDecor = true;


2,把下面這行從 tab1.onCreateView(...).方法中移除..

((ViewPager.LayoutParams) (view.findViewById(R.id.tab_strip)).getLayoutParams()).isDecor = true;

... 添加它到 MainActivity.onCreate(...) 中.  app 就不會出錯了.

你的 tab1 Fragment 不會知道 R.id.tab_strip view控件. 這是 MainActivity class的控件.

    解決辦法已經得到,我把上面代碼添加到我的主Activity中(ViewPager的Activity),並修改其中id爲我的PagerTabStrip的id。再試着運行了一下,終於顯示出來了。
    到的此時,問題已經解決,第一個回答中曾給出一個同樣的問題的連接,本着擴展視野的精神,點開瞧瞧,其實也有一點價值,可以對此問題的出現有更深的瞭解。
此問題大意翻譯如下:
在目標爲Android6.x(N)的app上,ViewPager中的PagerTabStrip不顯示


我一個現有的app,使用了ViewPager和PagerTabStrip,它們都是使用多版本的Android SDK 和  com.android.support:appcompat-v7都正常運行並且有一段時間了.但當我試圖去完善並且把目標 SDK版本從app的 23 改成 24 而且 com.android.support:appcompat-v7 版本也從 23.4.0 改成 24.0.0.的時候,我發現 PagerTabStrip 從 ViewPager消失不見了, (不僅僅是 PagerTabStrip中的titles ,.而是整個  PagerTabStrip 從 ViewPager 不見了.)

下面是聲明的 ViewPager 和 PagerTabStrip 的佈局文件:

<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>

這是代碼寫錯了還是 ViewPager 或者 PagerTabStrip的一個bug























解決辦法和上面已經出現的解決辦法是一樣的,看其中的一個回答:

這有一個臨時的解決方案,直到支持庫解決了這個 issue:

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

看這裏有這個 issue更全面的討論:https://code.google.com/p/android/issues/detail?id=213359

這個鏈接是個提交和跟蹤Android開源項目的問題的網站內容,打開此鏈接,此問題和比較有價值的話語的大意如下:

Issue 213359: PagerTabStrip在 com.android.support library 24.0.0上不工作

Reported by [email protected]Jun 16, 2016
我把項目sdk版本更新到 24,支持庫也同時更新了 and therefore upgraded my support libraries.

這時PagerTabStrip不出現了.

甚至把項目選項改 "Tabbed activity" 作爲默認的啓動activity ,在 ViewPager添加 PagerTabStrip 還是不顯示

e.g.

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >


        <android.support.v4.view.PagerTabStrip
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ff0000"
            android:layout_gravity="top"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:textColor="#000000" />


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


但是當支持庫再用 23.3.0 它又會顯示.

我是在 nexus 6 運行的 N preview 4 預覽版測試的.

Jun 19, 2016
#7 [email protected]
正確的解決方案:

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

先來說它的工作原理. 當把一個一個View添加到一個ViewPager的時候,它會獲取它的佈局參數並且更新他們 , 包括決定View是不是應該作爲裝飾顯示的 isDecor 參數. 先前,所有實現ViewPager.Decor接口的View都是由ViewPager 設置的. PagerTitleStrip 實現了它,而且繼承PagerTabStrip 擴展了它,運行正常.

在24.0.0 ,這個接口被這個annotation(註解) ViewPager.DecorView 取代了,不知道什麼原因,並且這個annotation需要由view的類型處理. 所以現在 PagerTitleStrip 是annotated(註解過的) ,而不是實現了這個接口, 但是 Android developers 忘記在PagerTabStrip添加相同的 annotation,並且這樣的 annotations也是不能由inherited(遺傳)得到的 , 就不再有作用了.

解決辦法是隻需要手動設置一下參數. 其實在 XML文件裏設置它會更好,但遺憾的是沒有相應的 XML 屬性, 所以我們只好在代碼中去設置.
Project Member #9 [email protected]
不完全是. Annotations 也可以inherited(遺傳), 但是我們沒有添加 @Inherited 標識到 這個annotation.
Jun 21, 2016
#10 [email protected]
我的意思是說默認是不能inherited .你提到的這個標識 (flag)當然也可以解決這個問題.
     
        此時,這個問題出現的原因,也已經知道了,它也許是官方的一個疏忽導致的,將來或者是過幾天就會被消滅,但現在記住解決辦法就ok了,就是在ViewPager所在的Activity的onCreadView()方法中,添加一句代碼就行了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章