1

画面の左側にメニューエントリを含むListFragmentがあり、次にListFragmentの右側にある領域で、左側で選択したエントリに基づいてさまざまなフラグメントをプログラムでロードできるAndroidレイアウトを作成しようとしています。そこで、2つのフレームレイアウトを含む水平線形レイアウトを持つxmlレイアウトファイルを作成することから始めました。私の考えは、フラグメントをフレームレイアウトにロードできるということでした。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="0dp"
    android:layout_height="match_parent">

    <FrameLayout 
        android:id="@+id/fragment_container_left"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_weight="30"/>


    <FrameLayout 
        android:id="@+id/fragment_container_right"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_weight="70"/>

</LinearLayout>

次に、onCreateメソッドのMainViewActivityクラスで、左側のコンテナーにリストフラグメントを追加し、右側のコンテナーにリストフラグメントを追加します。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_items);

    if(findViewById(R.id.fragment_container_left) != null) {
        if(savedInstanceState == null){
            MenuListFragment menuListFragment = new MenuListFragment();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_container_left, menuListFragment);
            ft.commit();
        }
    } 

    if(findViewById(R.id.fragment_container_right) != null) {
        if(savedInstanceState == null){
            MoveListFragment moveListFragment = new MoveListFragment();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_container_right, moveListFragment);
            ft.commit();
        }
    }
}

したがって、MenuListFragmentのonCreateメソッドは次のようになります。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(ArrayAdapter.createFromResource(getActivity().getApplicationContext(),
            R.array.menu_items, R.layout.main_menu_item));
}

そして、MoveListFragmentのonCreateメソッドは次のとおりです。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] items = { "move 1", "move 2", "move 3", "move 4" };
    setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.move_list_items, items));
}

これがmain_menu_itemxmlです。

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:padding="6dp" 
    android:id="@+id/main_menu_item"/>

およびmove_list_itemsxml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:padding="6dp" 
    android:id="@+id/move_list_item"/>

このコードを実行すると、いかなる種類のエラーも発生しませんが、画面に白い背景しか表示されません。私は自分が間違っていることを理解していません。

4

1 に答える 1

4

呼び出すfindViewById(R.id.fragment_container_left)と、フラグメントではなくFrameLayoutが得られますが、これは明らかにnullにはなりません。を使用して、それぞれのフラグメントがすでに存在するかどうかを確認する必要がありますfindFragmentById

if(getFragmentManager().findFragmentById(R.id.fragment_container_left) == null) {
        MenuListFragment menuListFragment = new MenuListFragment();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container_left, menuListFragment);
        ft.commit();
}

別の注意点として、リストフラグメントを使用して、コンテンツフラグメントをリストフラグメントにリンクできますsetTargetFragment。これにより、フラグメントが再作成された場合にフラグメントが自動的に再接続されます。

于 2013-02-07T17:36:33.173 に答える