I have an activity which adds a full screen fragment when a button inside it is clicked. This is the XML of my activity:
<?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>
this is the method which changes the fragment when btnShowInspectionDates
is clicked:
public void showInspectionDatesFragment(View v)
{
InspectionDatesFragment f = InspectionDatesFragment.newInstance(null);
Utilities.addFragment(R.id.addVehicleContainer, f, null, getSupportFragmentManager());
}
Utilities.addFragment
simply performs the fragment transaction (it creates a FragmentTransaction
object, calls add
method on it and executes the commit of the transaction).
This is the layout of my 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
simply contains a RecyclerView
inside a LinearLayout
, I use this as base layout in others layout which contains a RecyclerView
.
So, when I click on btnShowInspectionDates
the layout on my phone doesn't seem to change. I've used the debugger to check if my fragment is really created by putting a breakpoint in onCreatedView
, and yes, it's created successfully because it doesn't throw any exception. logcat doesn't seem to give useful things, so does anyone know how to solve my problem?
Thanks in advance!
EDIT
Now, I've created a fragment which contains the previous layout of my AddVehicleActivity
, and then I change the two fragment when btnShowInspectionDates
is clicked. But now I've got another problem: btnAddInspectionDate
isn't shown in my InspectionDatesFragment
: the view is totally white.
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>
You are adding it in wrong container.
In activity layout, just keep container for fragments.
Create one more fragment, that will have layout which activity is having currently and add it in activity's fragment container.
On button click, just replace/add fragment you want to launch.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.