Fragment überlagert Default Layout nur teilweise

  • Antworten:7
elochai
  • Forum-Beiträge: 6

15.05.2015, 16:27:19 via Website

Hallo zusammen,

das Thema Fragments ist für mich noch relative neu, deshalb ist beim Problem vielleicht sehr trivial :)

Ich habe ein default-layout, welches ich beim Klick auf ein Icon mit einem Fragment überlagern/austauschen möchte.

Wenn ich jedoch den Replace durchführe, sind alle Elemente des default layouts bis auf die Buttons überdeckt. Ich habe schon gegoogelt, jedoch nichts nützliches finden können wieso die Buttons bestehen bleiben...weiß jemand Rat?

hier die entsprechenden Code-Snippes. Namespaces habe ich entfernt, da man als neues Mitglied hier wohl keine "externen Links" angeben darf

UPDATE: Ich habe nun herausgefunden, dass es wohl daran liegt, dass ich die Buttons in der XML anlegen, das Fragement jedoch erst zur Laufzeit. Wie füge ich das Fragement korrekt in die XML-Datei des Layouts ein damit diese den dort alles bis auf den NavigationDrawer überdeck?

das default-layout:

<android.support.v4.widget.DrawerLayout

android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout 
    android:id="@+id/fragment_placeholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/numberField"
        android:layout_width="fill_parent"
        android:layout_height="50sp"
        android:hint="@string/enter_number"
        android:inputType="number"
        android:maxLength="3"
        android:digits="0123456789" />

    <TextView
        android:id="@+id/staticText"
        android:layout_width="fill_parent"
        android:layout_height="35sp"
        android:text="@string/guess_info"
        android:layout_below="@+id/numberField"
        android:layout_marginTop="10sp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_marginTop="10sp"
        android:layout_below="@+id/staticText"
        android:layout_width="fill_parent"
        android:layout_height="130sp">
    </ImageSwitcher>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/submit_button"
        android:id="@+id/submit_button"
        android:layout_above="@+id/reset_button"
        android:background="@android:color/white"
        android:layout_marginBottom="10sp"
        android:onClick="handleGuess" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/reset_button"
        android:id="@+id/reset_button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@drawable/buttons_color"
        android:textColor="@drawable/button_text"
        android:onClick="restartGame" />

</RelativeLayout>

<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@drawable/nav_drawer_bg"/>

Das Layout des Fragments

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/custom_blue" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/history_headline"
    android:id="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10sp"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/history_list_view"
    android:layout_below="@+id/textView"
    android:choiceMode="singleChoice"></ListView>

Die Fragement Klasse

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class HistoryFragment extends Fragment {
OnItemSelectedListener mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnItemSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnItemSelectedListener");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_history, container, false);
}

public interface OnItemSelectedListener {
    public void setText(String text);
}

}`

der switch in der activity
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_placeholder, historyFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();`

Ich hoffe das reicht an Code aus

— geändert am 15.05.2015, 17:19:03

Antworten
Sven R.
  • Forum-Beiträge: 1.904

15.05.2015, 17:43:45 via App

Du ersetzt das RelativeLayout mit der Id fragmentPlaceholder mit deinem Fragment. Es wird so umgesetzt, wie du es angibst.
Wahrscheinlich willst du aber unters RelativeLayout dein Fragment setzen. Also erstell ein FrameLayout da wo das Fragment hin soll(unters RelativLayout) und benenn entsprechend die Ids um.

Wenn dir mein Beitrag gefällt, kannst dich einfach mit dem 👍 "Danke"-Button auf der Website dieses Forums bedanken. 😀

Why Java? - Because I can't C#

Antworten
elochai
  • Forum-Beiträge: 6

15.05.2015, 17:55:49 via Website

Vielen Dank für den Hinweis!

Ziel ist es, den Inhalt des RelativeLayouts komplett mit dem Fragment zu ersetzen.

Ich habe nun folgenden Aufbau:

layout:

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout 
    android:id="@+id/guess_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/numberField"
        android:layout_width="fill_parent"
        android:layout_height="50sp"
        android:hint="@string/enter_number"
        android:inputType="number"
        android:maxLength="3"
        android:digits="0123456789" />

    <TextView
        android:id="@+id/staticText"
        android:layout_width="fill_parent"
        android:layout_height="35sp"
        android:text="@string/guess_info"
        android:layout_below="@+id/numberField"
        android:layout_marginTop="10sp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_marginTop="10sp"
        android:layout_below="@+id/staticText"
        android:layout_width="fill_parent"
        android:layout_height="130sp">
    </ImageSwitcher>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/submit_button"
        android:id="@+id/submit_button"
        android:layout_above="@+id/reset_button"
        android:background="@android:color/white"
        android:layout_marginBottom="10sp"
        android:onClick="handleGuess" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/reset_button"
        android:id="@+id/reset_button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@drawable/buttons_color"
        android:textColor="@drawable/button_text"
        android:onClick="restartGame" />

</RelativeLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/fragment_placeholder"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@drawable/nav_drawer_bg"/>

`

jedoch wirft er mir jetzt einen Fehler beim Setzen der View bei:
setContentView(R.layout.activity_main);
in der onCreate in der MainActivity.

android.view.InflateException: Binary XML file line #69: Error inflating class fragment

Zeile 69 in der xml ist der Beginn des fragements. Die MainActivity erbt von ActionBarActivity, welche ja die FragmentActivity vererbt bekommt.

Wo liegt hier der Fehler?

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

15.05.2015, 18:15:18 via Website

Fragment existiert nicht als XML Tag. Du nimmst ein normales Layout (Relative/Frame/Linear) und lässt dir das per FragmentManager da rein setzen/austauschen

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Sven R.
  • Forum-Beiträge: 1.904

15.05.2015, 23:51:53 via App

elochai

[...] Wenn ich jedoch den Replace durchführe, sind alle Elemente des default layouts bis auf die Buttons überdeckt. [...]

elochai

[...] Ziel ist es, den Inhalt des RelativeLayouts komplett mit dem Fragment zu ersetzen. [...]

Wasn jetzt?

Jedenfalls ein FrameLayout oder das View, das du überdecken möchtest und ein fragmentTransaction.replace().commit() brauchst du.

— geändert am 15.05.2015, 23:57:26

Wenn dir mein Beitrag gefällt, kannst dich einfach mit dem 👍 "Danke"-Button auf der Website dieses Forums bedanken. 😀

Why Java? - Because I can't C#

Antworten
elochai
  • Forum-Beiträge: 6

16.05.2015, 14:19:02 via Website

Sven R.

elochai

[...] Wenn ich jedoch den Replace durchführe, sind alle Elemente des default layouts bis auf die Buttons überdeckt. [...]

elochai

[...] Ziel ist es, den Inhalt des RelativeLayouts komplett mit dem Fragment zu ersetzen. [...]

Wasn jetzt?

Jedenfalls ein FrameLayout oder das View, das du überdecken möchtest und ein fragmentTransaction.replace().commit() brauchst du.

Das zweitere :) Habe es mit nun hinbekommen. Jetzt hat sich eine weitere Frage aufgetan:
Wie kann ich im NavigationDrawer nach einem Klick auf den physischen Back-Button, den aktiven Listenpunkt in meiner Navigation aktualisieren?

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

16.05.2015, 14:39:01 via Website

Kommt darauf an wie du deinen Navigation Drawer steuerst, ob du dafür eine eigene Klasse hast oder nicht.
Aber eigenlich ist der Drawer an sich nur eine Vie bs. eine ListView da kannst du dann setSelected auf den richtiggen intex machen.

LG

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
elochai
  • Forum-Beiträge: 6

16.05.2015, 14:56:29 via Website

Danke für den Tipp! setItemChecked() hat das Problem gelöst :)

Antworten