2017-05-03 5 views
0

私は以下を読んでいますAndroid include layout dynamically with data-binding library質問。私の問題は少し違う。アクティビティのフレームレイアウトをデータバインディングで動的に拡張する方法はありますか?

私は2つのXML活動のための1つ持っている:私はデータバインディングとでframeLayout活動に動的にframe.xmlを膨らまたい

<layout 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <data> 
     <variable name="item" type="myapplication.FrameViewModel"/> 
    </data> 
<FrameLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="TEST"/> 

</FrameLayout> 
</layout> 

:フレームの

<?xml version="1.0" encoding="utf-8"?> 
<layout 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <data> 
     <variable name="item" type="myapplication.MainActivityViewModel"/> 
    </data> 
<android.support.constraint.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="myapplication.MainActivity"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/main"> 
    </FrameLayout> 

</android.support.constraint.ConstraintLayout> 
</layout> 

し、別の。言い換えれば、フラグメントのように、断片のない、さまざまなフレームをアクティビティに展開したいと思います。私が活動に示す両方のレイアウトを得ることができるコードで

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); 

     ViewGroup view = activityMainBinding.main; 
     FrameBinding frameBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.frame,view, false); 
} 

: は私が活動中で次のことを試してみました。私はアクティビティレイアウトしか見ることができません。アクティビティのレイアウトビュー要素とフレームビュー要素をアクティビティでも表示できるようにするには、コードをどのように変更する必要がありますか?

答えて

1

あなたが見ているエラーは明確ではありませんが、私の推測が正しい場合は、フレームレイアウトのインクルードを実行する必要があります。データバインディングで行う方法の詳細については、"That include thing"を参照してください。

更新 私の最初の推測は間違っていました。別の試みがあります:

Fragmentsを使用したくない場合は、簡単にViewを使用して見てください。次のコードは、データバインディングが中断せず、レイアウトに残りの元のビューが引き続き表示されることを示しながら、FrameLayoutを別のコードに置き換えます。メイン画面が表示されたら、ボタンをクリックしてFrameLayoutを別のものに置き換えます。

ここでは短いdemonstrationです。

MainActivity.java

import android.databinding.DataBindingUtil; 
import android.os.Bundle; 
import android.support.constraint.ConstraintLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.ViewGroup; 

import com.example.databindingreplaceview.databinding.ActivityMainBinding; 
import com.example.databindingreplaceview.databinding.FrameBinding; 

public class MainActivity extends AppCompatActivity { 

    private String replacementText = "This is the replacement frame."; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ActivityMainBinding activityMainBinding = 
       DataBindingUtil.setContentView(this, R.layout.activity_main); 

     final ViewGroup view = activityMainBinding.main; 
     final FrameBinding frameBinding = 
       DataBindingUtil.inflate(getLayoutInflater(), R.layout.frame, view, false); 
     frameBinding.setReplacementText(replacementText); 
     view.getRootView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ConstraintLayout layout; 

       v.setEnabled(false); 
       layout = (ConstraintLayout) view.getRootView().findViewById(R.id.constraintLayout); 
       layout.removeView(view.findViewById(R.id.main)); 
       layout.addView(frameBinding.getRoot()); 
      } 
     }); 

    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<layout 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.support.constraint.ConstraintLayout 
     android:id="@+id/constraintLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="This is the top text." 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent" /> 

     <FrameLayout 
      android:id="@+id/main" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="64dp" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent"> 

      <TextView 
       android:id="@+id/textView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:text="This is the frame to replace." /> 

     </FrameLayout> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="112dp" 
      android:text="This is the bottom text." 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent" /> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="51dp" 
      android:text="Replace Frame" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toBottomOf="@+id/textView2" /> 


    </android.support.constraint.ConstraintLayout> 
</layout> 

frame.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"> 

    <data> 
     <variable 
      name="replacementText" 
      type="String" /> 
    </data> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="64dp" 
      android:text="@{replacementText}" /> 

    </FrameLayout> 
</layout> 
+0

私はダイナミック表示したいと思いフレーム。 – codeme

+0

@codeme更新された回答を参照してください。私はこれが役立つことを願っています – Cheticamp

+0

ありがとう、非常に便利です。 – codeme

関連する問題