2017-02-28 104 views
0

我有2個片段和一個主要活動。片段A中的列表中的項目被點擊,ID在主活動中被檢索並且第二片段替​​換第一片段。這種情況發生在主要活動中的以下接口方法中:確定哪個佈局應用程序當前正在使用

@Override 
public void onCLicked(int id) { 
    //Launch Fragment B/Pass id to it 
} 

這在手機屏幕上運行良好。

我現在爲碎片並排的平板電腦做了第二個佈局。我的新佈局是在我的資源佈局sw720土地文件夾,如下所示:

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <FrameLayout 
    android:id="@+id/container" 
    android:layout_width="600dp" 
    android:layout_height="0dp" 
    tools:context="com.markf.popularmovies.activities.MainActivity" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintBottom_toBottomOf="parent"> 
</FrameLayout> 

<FrameLayout 
    android:id="@+id/containerDetail" 
    android:layout_width="600dp" 
    android:layout_height="0dp" 
    tools:context="com.markf.popularmovies.activities.MainActivity" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toRightOf="@id/container" 
    app:layout_constraintRight_toRightOf="parent"> 
</FrameLayout> 
</android.support.constraint.ConstraintLayout> 

當我用我的平板電腦,但是當我點擊一個項目,片段A被替換片段B這個佈局啓動就好像它還在電話中一樣。有沒有一種方法可以通過編程來確定哪個佈局,所以我可以適當地編寫代碼?我試着讀此解決方案:Android Developer:Communicating with Other Fragments

,但沒有運氣

+1

假設有ID的''FrameLayout'是containerDetail'不是在手機佈局,如果'findViewById(R.id.containerDetail)'返回null,則你在電話佈局。 –

+1

有道理。這樣可行。你可以回答,我會接受。謝謝! –

回答

0

一個簡單的方法來做到這將是檢查是否有View的存在,這就是獨特的佈局之一。在這種情況下,由於FrameLayout ID containerDetail只在平板電腦佈局,如果findViewById(R.id.containerDetail)返回null,那麼你在手機佈局。

例如:

if (findViewById(R.id.containerDetail) == null) { 
    // Phone layout 
} 
else { 
    // Tablet layout 
}