AndroidのFragmentのreplace

最近またAndroidやってます。ノリで書いてたら詰まったのでちょっとしたメモです。

動的なFragment

Google developersに載ってるようなタグを使ってxmlを使うと動的にFragmentを差し替えることができない。

解決法

googleの画面を1:2に配分してそれぞれに動的なフラグメントを割り当てたいとき、googleのコードのを適当なViewGroupに変更する。つまり、

<?xml version="1.0" encoding="utf-8"?>
<!-- activity_main.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/fragment_container"
    >
    <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/fragment_list_container"
            />
    <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:id="@+id/fragment_content_container"
            />
</LinearLayout>
/*
MainActivity.java
*/
package com.b1u3dev.fragmentandlayout;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity {
    private FragmentManager mFragmentManager;
    private String className=MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmentManager=getSupportFragmentManager();
        mFragmentManager.beginTransaction().add(R.id.fragment_content_container,new ContentFragment()).commit();
        mFragmentManager.beginTransaction().add(R.id.fragment_list_container,new ListFragment()).commit();

    }

 
    public void onClick(View view){
        Log.d(className,"Fragment Size:"+mFragmentManager.getFragments().size());
        switch (view.getId()){
            case R.id.content_button:
                Log.d(className,"content_button was clicked");
                mFragmentManager.beginTransaction().replace(R.id.fragment_content_container,new SecondContentFragment()).commit();
                break;
            case R.id.list_button:
                Log.d(className,"list_button was clicked");
                break;
        }

    }
}

こんな感じで。
xxxFragmentは全部FragmentのサブクラスでonCreateViewをオーバーライドしてinflateしてる。
f:id:b1u3:20180615030417p:plain

その他のファイル

package com.b1u3dev.fragmentandlayout;
/*
ContentFragment.java
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ContentFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_content,container,false);
    }
}
package com.b1u3dev.fragmentandlayout;
/*
ListFragment.java
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list,container,false);
    }
}
package com.b1u3dev.fragmentandlayout;
/*
SecondContentFragment.java
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SecondContentFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_content_second,container,false);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!-- fragment_content.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              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 Content"/>
    <Button
            android:id="@+id/content_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Content Fragment Button"
            android:onClick="onClick"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<!-- fragment_content_second.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              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="SECOND FRAGMENT CONTENT BUTTON"/>
    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SECOND FRAGMENT CONTENT BUTTON"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<!-- fragment_list.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  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="List Activity"/>
        <Button
                android:id="@+id/list_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="List Activity Button"
                android:onClick="onClick"/>

</LinearLayout>

あ、ボタンの名前修正するの忘れてた。