Android: durch die Verwendung von Fragmenten programmgesteuert

Einige Gründe kann ich nicht verwenden, die xml-layout-Dateien.

Aber ich brauche zum erstellen einer android-tablet-app.

Beschloss ich, die Fragmente verwenden.

Ich will das gleiche layout, dass diese xml-generiert:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <fragment class="com.example.ItemsList"
            android:id="@+id/items_list" android:layout_weight="1"
            android:layout_width="0dp" android:layout_height="fill_parent" />

    <FrameLayout android:id="@+id/item_details" android:layout_weight="1"
            android:layout_width="0dp" android:layout_height="fill_parent"
            android:background="?android:attr/detailsElementBackground" />

</LinearLayout>

Aber ich habe Probleme mit dem hinzufügen von Fragmenten zu meinem linearLayout:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(createUI());
    }

    private View createUI() {
        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT));
        layout.setId(0x101);
        {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.add(0x101, new ItemsList());
            fragmentTransaction.add(0x101, new ItemDetails());
            fragmentTransaction.commit();
        }
        return layout;
    }

Eigentlich kann ich auch nicht erstellen LinearLayout mit zwei identischen Fragmente:

            ...
            fragmentTransaction.add(0x101, new ItemsList());
            fragmentTransaction.add(0x101, new ItemsList());
            ...

Bitte helfen

Btw ich kann nicht herausfinden, warum müssen wir erklären "FrameLayout" für itemDetails Fragment aber "fragment" ist genug für itemsList ListFragment?

UPD:

Zu tun, man sollte nur hinzufügen Dritte param:

            ...
            fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag1");
            fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag2");
            ...

Default-Wert für die tag-parameter null ist, also habe ich versucht zwei verschiedene Elemente mit identischen ids. Dank p-lo für seinen Kommentar.

  • Haben Sie versucht, die "Fragmente" z.B. auf Android-Entwickler?
  • Natürlich Tat ich das. Wiederhole: mein problem ist, dass ich kann nicht mit xml-Dateien an alle!
  • Ah, sorry, interpretierte ich Ihre Frage so, dass das layout wäre nicht aufblasen. So im wesentlichen, Sie wollen eine Reine Java-Implementierung des layout, richtig?
  • Yep. Zumindest habe ich eine Lösung gefunden, die für mich gearbeitet=) es ist auf dieser Seite höher.
InformationsquelleAutor leshka | 2011-07-29
Schreibe einen Kommentar