2017-02-28 8 views
0

2つのフラグメントとメインアクティビティがあります。フラグメントAのリストのアイテムがクリックされ、IDがメインアクティビティで取得され、2番目のフラグメントが最初のものに置き換えられます。これは、メインアクティビティ内の次のインタフェース方法で発生します。現在使用しているレイアウトアプリケーションを決定する

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

これは電話画面でうまくいきます。

これで、フラグメントが並んでいるタブレットの2番目のレイアウトを作成しました。私の新しいレイアウトは、私のリソースレイアウト-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に置き換えている場合は、このレイアウトが起動しますあたかもそれが電話に残っているかのように。私はこれを適切にコーディングすることができるので、プログラムでどのレイアウトImを決定する方法はありますか? Android Developer:Communicating with Other Fragments

が、運

+1

IDと 'FrameLayout'を想定するとは' containerDetail'は 'findViewById(R.id.containerDetail)は' nullを返した場合、あなたはしている、携帯電話のレイアウトではありません電話レイアウト。 –

+1

意味があります。それは動作します。あなたは答えることができ、私は受け入れます。ありがとう! –

答えて

0

レイアウトのいずれかにユニークだViewの存在を確認することですこれを行うための簡単な方法:私は、このソリューションを読んでみました。この場合、IDがcontainerDetailFrameLayoutはタブレットレイアウト内にあるため、findViewById(R.id.containerDetail)がnullを返すと、電話機レイアウトになります。例えば

if (findViewById(R.id.containerDetail) == null) { 
    // Phone layout 
} 
else { 
    // Tablet layout 
} 
関連する問題