2017-01-17 30 views
9

工作我有使用「自定義佈局」偏好屏幕:偏好屏幕自定義佈局僅與第二點擊

android:layout="@layout/currencycodes" 

這個問題是它不能帶來了佈局上的首次嘗試。 正如您從下面的動畫gif中看到的那樣,我必須撤退並再次嘗試佈局才能出現。那爲什麼呢?

enter image description here

的preferences.xml

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:key="application_preferences"> 
    <PreferenceCategory> 
     <PreferenceScreen 
       android:key="url_settings" 
       android:title="@string/url_settings" 
       android:summary="@string/summary_url_settings"> 
     </PreferenceScreen> 

     <PreferenceScreen 
       android:key="currency_codes" 
       android:title="@string/left_handed" 
       android:summary="@string/summary_left_handed"> 
      <Preference 
        android:key="currency_exchanges" 
        android:layout="@layout/currencycodes"/> 
     </PreferenceScreen> 
     <EditTextPreference 
       android:key="url_exchange_rate" 
       android:title="@string/url_exchange_rate_settings" 
       android:summary="@string/summary_url_exchange_rate" 
       android:enabled = "false"/> 
    </PreferenceCategory> 
</PreferenceScreen> 

以下是使用自定義佈局。它包含一個GridView。

currencycodes.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     tools:context=".MainActivity"> 

    <GridView 
      android:layout_width="328dp" 
      android:layout_height="479dp" 
      android:layout_marginTop="16dp" 
      app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="16dp" 
      app:layout_constraintLeft_toLeftOf="parent" android:layout_marginEnd="16dp" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp" 
      android:id="@+id/grdExchanges" android:scrollbars="horizontal|vertical" 
      android:smoothScrollbar="true" tools:textAlignment="center" 
      android:verticalScrollbarPosition="defaultPosition"/> 
</android.support.constraint.ConstraintLayout> 
+0

請添加Java作爲以及 –

回答

1

我想出了一個解決方法。我注意到在充氣一個LayoutInflater.java佈局時,靜態HashMap的是用來收集視圖對象:

private static final HashMap<String, Constructor<? extends View>> sConstructorMap 

發生了什麼事是第一次點擊調用LayoutInflater,但第一次點擊只增加gridview的視圖中靜態hashmap。你會發現,與第一次點擊,命令,sConstructorMap.get(name),只返回NULL:

public final View createView(String name, String prefix, AttributeSet attrs) 
     throws ClassNotFoundException, InflateException { 

    Constructor<? extends View> constructor = sConstructorMap.get(name); 

    Class<? extends View> clazz = null; 

     try { 

      if (constructor == null) { 

在LayoutInflater,變量,mPrivateFactory,其實是你的主要活動。這是我的gridview將在mPrivateFactory.onCreateView()初始化。但是,通過第一次單擊,mPrivateFactory.onCreateView()返回null,因爲靜態哈希映射沒有gridview的視圖....但是。這是下面將進一步在命令行中,onCreateView(parent, name, attrs);在那裏最後補充說:

if (view == null && mPrivateFactory != null) { 
     view = mPrivateFactory.onCreateView(parent, name, context, attrs); 
    } 

    if (view == null) { 
     final Object lastContext = mConstructorArgs[0]; 
     mConstructorArgs[0] = context; 
     try { 
      if (-1 == name.indexOf('.')) { 
       view = onCreateView(parent, name, attrs); <-- ADD TO STATIC HASHMAP 
      } else { 
       view = createView(name, null, attrs); 
      } 
     } finally { 
      mConstructorArgs[0] = lastContext; 
     } 
    } 

所以直到我想出了一個更好的解決方案,我只是調用該方法,onCreateView(parent),兩次第一次,特別優選點擊上。第一次調用添加GridView控件的靜態HashMap中,第二個電話,然後實例化佈局來看(再次),但是,這一次,在我自己的活動自己的onCreateView方法初始化GridView控件,mPrivateFactory.onCreateView()

protected View onCreateView(ViewGroup parent) { 

View layout = super.onCreateView(parent); 

if(firstCall){ 

    firstCall = false; 

    layout = super.onCreateView(parent); 
} 

return layout; 
} 
1

嘗試從

(!mFragMngr.popBackStackImmediate(fragName, 0) 
       && mFragMngr.findFragmentByTag(fragName) == null) 

改變你if聲明

(mFragMngr.findFragmentByTag(fragName) == null) 
+0

這僅僅決定了設定檔位按鈕是否已被按下或片段已被顯示。坦率地說,這個問題沒有任何關係。 – Drawn

2

那麼首先你需要了解如何PreferenceScreen作品?

從[Android文檔] [1]:

當它出現另一個偏好層級內時,它被示出和 用作網關偏好的另一屏幕(或者通過 表示偏好的另一個屏幕作爲對話或通過getIntent()中的 startActivity(android.content.Intent))。這PreferenceScreen的 孩子在 這PreferenceScreen在顯示屏幕沒有顯示,而是被點擊此偏好時,一個單獨的屏幕上會顯示 。

簡單的話,

偏好框架處理顯示了從首層級這些其他屏幕。 此PreferenceScreen標記用作屏幕中斷(類似於文字處理中的分頁符)。與其他首選項類型一樣,我們在此處指定一個鍵,以便能夠保存和恢復其實例狀態。

對你來說,什麼是真正發生的是「application_preferences」將被用作層次結構的根,並給一個PreferenceActivity.The第一屏將顯示首選項「Url_Setting」和「貨幣代碼 」。該「貨幣代碼」是「second_preferencescreen」,並點擊將顯示首選項的另一個屏幕,這是「外幣兌換」的時候(因爲它的喜好爲「second_preferencescreen」標籤即貨幣代碼的孩子)。

所以,這就是爲什麼它沒有出現在第一次點擊....但在第二次點擊希望你有 偏好屏幕是如何工作的?

如果您還有任何疑問...讓我知道。 .. https://developer.android.com/reference/android/preference/PreferenceScreen.html

+0

您在背誦PreferenceScreen文檔時並未考慮到此特定問題。 「那麼,爲什麼它沒有顯示在第一次點擊...」 - 你沒有說明什麼解釋了爲什麼它沒有顯示在第一次點擊。 「貨幣代碼」不是「second_preferencescreen」,而是第三個。第二個是「url_settings」,它工作正常。 – Drawn

+0

@退出的貨幣代碼是第二優先屏幕。根據您的代碼,其中有另一個優先鍵,它是貨幣兌換看到你已經提到。優先其非優先屏幕對於貨幣兌換。我從文檔中引用了這一點,以便了解官方文檔對偏好等級的說明。 。它總是關於偏好層次和屏幕。根目錄層次結構不被視爲第一個,它是您的應用程序偏好。喜歡偏好活動。希望您明白我的觀點。這都是關於偏好層次結構。索引。內部在android.Where你的問題在於? – PN10

+0

URL_settings將正常工作,因爲它是第一優先選擇屏幕,但貨幣代碼是第二優先選擇屏幕,它具有另一個優先選項。對於那個screen.iecurrency交換...請參閱您的首選項屏幕的順序...這裏... – PN10