簡體   English   中英

添加片段時,活動布局不會更改

[英]Activity layout doesn't change when a fragment is added

我有一個活動,當點擊其中的按鈕時,它會添加一個全屏碎片。 這是我的活動的XML:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="clyky.cartracker.activities.AddVehicleActivity"
    android:id="@+id/addVehicleContainer">
    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lblPlate"
            android:text="Targa"
            android:labelFor="@+id/txtPlate"/>
        <EditText
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/txtPlate"
            android:inputType="text"
            android:hint="Targa del veicolo"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lblKm"
            android:text="Chilometri"
            android:labelFor="@+id/txtPlate"/>
        <EditText
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/txtKm"
            android:inputType="number"
            android:hint="Chilometraggio"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblInUse"
            android:labelFor="@+id/chckbxInUse"
            android:text="In uso:"/>

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/chckbxInUse"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblInsuranceDate"
            android:labelFor="@+id/txtInsuranceDate"
            android:text="Data di assicurazione:"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtInsuranceDate"
            android:text=""/>

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/btnShowDatePickerForInsuranceDate"
            android:onClick="showDatePicker"
            android:text="Seleziona"
            android:tag="0"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblMatriculationDate"
            android:labelFor="@+id/txtMatriculationDate"
            android:text="Data d'immatricolazione:"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtMatriculationDate"
            android:text=""/>

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/btnShowDatePickerForMatriculationDate"
            android:onClick="showDatePicker"
            android:text="Seleziona"
            android:tag="1"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblEffectiveFuelEconomy"
            android:labelFor="@+id/txtEffectiveFuelEconomy"
            android:text="Consumo di carburante (km/l):"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtEffectiveFuelEconomy"
            android:inputType="number"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblMake"
            android:labelFor="@+id/txtLastInspectionDate"
            android:text="Produttore:"/>

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/spnrMake"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblModel"
            android:labelFor="@+id/spnrModel"
            android:text="Modello:"/>

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/spnrModel"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblTrim"
            android:labelFor="@+id/spnrTrim"
            android:text="Assetto:"/>

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/spnrTrim"/>
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnShowInspectionDates"
            android:text="Aggiungi date di revisione"
            android:onClick="showInspectionDatesFragment"/>
    </TableRow>
    <TableRow>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/btnSubmit"
            android:onClick="submit"
            android:text="Invia"/>
    </TableRow>
</TableLayout>

這是在單擊btnShowInspectionDates時更改片段的方法:

public void showInspectionDatesFragment(View v)
{
    InspectionDatesFragment f = InspectionDatesFragment.newInstance(null);
    Utilities.addFragment(R.id.addVehicleContainer, f, null, getSupportFragmentManager());
}

Utilities.addFragment只執行片段事務(它創建一個FragmentTransaction對象,在其上調用add方法並執行事務的提交)。

這是我的InspectionDatesFragment的布局:

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

    <include layout="@layout/recycler_view_fragment_layout"/>

    <Button
        android:id="@+id/btnAddInspectionDate"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="+"/>
</LinearLayout>

recycler_view_fragment_layout只是在LinearLayout包含一個RecyclerView ,我在其他布局中使用它作為基本布局,其中包含一個RecyclerView

所以,當我點擊btnShowInspectionDates ,手機上的布局似乎沒有改變。 我已經使用調試器檢查我的片段是否真的是通過在onCreatedView放置一個斷點來創建的,是的,它是成功創建的,因為它不會拋出任何異常。 logcat似乎沒有給出有用的東西,所以有誰知道如何解決我的問題?

提前致謝!

編輯

現在,我已經創建了一個包含我的AddVehicleActivity的先前布局的AddVehicleActivity ,然后在單擊btnShowInspectionDates時更改了這兩個片段。 但是現在我遇到了另一個問題:我的InspectionDatesFragment沒有顯示btnAddInspectionDate :視圖完全是白色的。

inspection_dates_fragment_layout:

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

    <include layout="@layout/recycler_view_fragment_layout"/>

    <Button
        android:id="@+id/btnAddInspectionDate"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="+"/>
</LinearLayout>

您將它添加到錯誤的容器中。

在活動布局中,只需保留容器以獲取碎片。

再創建一個片段,它將具有當前活動所具有的布局,並將其添加到activity的片段容器中。

單擊按鈕,只需替換/添加要啟動的片段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM