Incapaz de reemplazar / navegar fragmentos
Estoy tratando de navegar de un fragmento a otro. Estos 2 fragmentos son 100% diferentes, no hay ViewPager
o algo así para conectarlos. Aquí está el código.
Fragment_view1.xml
- Triangle opengl en android
- Error de procesamiento de OpenGL ES
- En la estructura MVP, la clase responsable de mantener los elementos de la lista y la forma de notificar los cambios de datos en este
- Android compara cartas especiales
- ¿Por qué no aparece la fecha de lanzamiento de Android?
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/firstView"> <TextView android:id="@+id/viewOneText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="92dp" android:layout_marginTop="182dp" android:text="First View" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/viewOneBtn" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/viewOneText" android:layout_below="@+id/viewOneText" android:layout_marginTop="17dp" android:text="Click Here" /> <include layout = "@layout/drop_down" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_alignParentBottom="true"/> <FrameLayout android:id="@+id/fragment_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
Custom_view.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/customFragment" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="174dp" android:text="Custom Fragment" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>
FirstView.java (utiliza fragment_view1.xml)
package com.example.fragmenttest; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class FirstView extends DropDownMenu { private TextView firstText; private Button btn; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_view1,container,false); firstText = (TextView)view.findViewById(R.id.viewOneText); btn = (Button)view.findViewById(R.id.viewOneBtn); btn.setOnClickListener(new ButtonEvent()); return view; } private class ButtonEvent implements OnClickListener { @Override public void onClick(View v) { // Create new fragment and transaction Fragment newFragment = new CustomView(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_view, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } } }
CustomView.java (utiliza custom_view.xml)
package com.example.fragmenttest; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class CustomView extends Fragment { private TextView secondText; private Button secondViewBtn; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.custom_view,container,false); return view; } }
En FirstView.java
, cuando hago clic en el botón, necesito pasar al Fragmento de CustomView
. Pero en cambio, simplemente sustituye todo en CustomView
en la parte superior de FirstView
. Abajo las imágenes lo explicarán mejor.
FirstView
solo
CustomView
solo
Cuando se FirstView
clic en el botón FirstView
¿Por qué está sucediendo así? Por favor ayuda
- Servidor de acceso de emulador androide en red local
- Colocación de varias tablas / columnas en un archivo CSV
- ¿Es mejor práctica de código para registrar un receptor en manifiesto o en código?
- Necesito ayuda con Android y Java
- Android Game API: Leaderboard - ¿Obtener puntuación de jugador específico?
- Hace TimerTask ejecutando en nuevo hilo
- Cola sin bloqueo de solicitudes HTTP POST con persistencia
- Android - Mientras maneja una imagen es mejor almacenarla o usarla en memoria temporal?
Está intentando reemplazar firstView
con el nuevo fragmento, pero firstView
no es un fragmento, es un RelativeLayout
.
No puede reemplazar un fragmento definido estáticamente en el archivo de diseño. Sólo puede reemplazar los fragmentos que agregó dinámicamente a través de FragmentTransaction
.
Ninguno de su Layout contiene fragmento en el diseño.
<fragment android:id="@+id/fragment1" android:layout_width="march_parent" android:layout_height="match_parent"></fragment>
Un nuevo Fragmento reemplazará un Fragmento existente que se agregó previamente al contenedor.
Mira esto .
Si necesitas reemplazar un fragmento por otro, entonces debes mencionar:
TalkDetail fragment = new TalkDetail(); // TalkDetail is name of fragment which you need to put while replacing older one. Bundle bundle = new Bundle(); bundle.putString("title", m_ArrayList.get(arg2).title); bundle.putString("largeimg", m_ArrayList.get(arg2).largeimg); bundle.putString("excert", m_ArrayList.get(arg2).excert); bundle.putString("description", m_ArrayList.get(arg2).description); bundle.putString("cat", m_ArrayList.get(arg2).cat); bundle.putString("header_title", "Talk"); //bundle.putInt("postid", m_ArrayList.get(arg2).postid); fragment.setArguments(bundle); ((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);
Aquí está su clase BaseContainerFragment.java que administrará un montón de cosas para usted.
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.util.Log; import app.drugs.talksooner.R; public class BaseContainerFragment extends Fragment { public void replaceFragment(Fragment fragment, boolean addToBackStack) { FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); if (addToBackStack) { transaction.addToBackStack(null); } transaction.replace(R.id.container_framelayout, fragment); transaction.commit(); getChildFragmentManager().executePendingTransactions(); } public boolean popFragment() { Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount()); boolean isPop = false; if (getChildFragmentManager().getBackStackEntryCount() > 0) { isPop = true; getChildFragmentManager().popBackStack(); } return isPop; } }
Para más detalles específicos encontrar mi puesto completo aquí ..
Cambiando dinámicamente los fragmentos dentro de un host de fichas de fragmentos?