tengo en una clase Fragment puesto unos botones, donde intento que, al pulsar sobre un botón, unos botones se oculten y otros se muestren.
Tengo los botones llamados Línea 1 y Línea 2, que al pulsarlos tendría que mostrar debajo los botones que hay ocultos, y en el caso de tener los botones a la vista se tendrían que ocular.
Pero no consigo que el código funcione, ¿me podéis ayudar?
Esta es la clase Fragment (Estaci2.java)
public class Estaci2 extends Fragment {
ImageButton boton_linea1;
ImageButton boton_linea2;
ImageButton boton_s_ga;
ImageButton boton_s_vi;
public Estaci2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate( R.layout.estacio2, container, false );
boton_linea1 = rootView.findViewById(R.id.bt_Linea_1);
boton_linea2 = rootView.findViewById(R.id.bt_Linea_2);
boton_s_ga = rootView.findViewById(R.id.bt_s_ga);
boton_s_vi = rootView.findViewById(R.id.bt_s_vi);
boton_linea1.setOnClickListener(this);
boton_linea2.setOnClickListener(this);
boton_s_ga.setOnClickListener(this);
boton_s_ga.setOnClickListener(this);
boton_s_ga.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Esta_S_Ga fragment = new Esta_S_Ga();
FragmentTransaction ftEs = getFragmentManager().beginTransaction();
//FragmentTransaction ftEs = getChildFragmentManager().beginTransaction();
//ftEs.replace(R.id.frame_container, new Esta_S_Ga());
ftEs.replace(R.id.frame_container, fragment);
ftEs.addToBackStack(null);
ftEs.commit();
}
});
boton_s_vi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Esta_S_Vi fragment = new Esta_S_Vi();
FragmentTransaction ftEs = getFragmentManager().beginTransaction();
//FragmentTransaction ftEs = getChildFragmentManager().beginTransaction();
//ftEs.replace(R.id.frame_container, new Esta_S_Vi());
ftEs.replace(R.id.frame_container, fragment);
ftEs.addToBackStack(null);
ftEs.commit();
}
});
return rootView;
}
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_Linea_1:
if(boton_linea1.getVisibility() == View.GONE)
{
boton_s_ga.setVisibility(View.VISIBLE);
boton_s_vi.setVisibility(View.GONE);
}else
{
boton_s_ga.setVisibility(View.GONE);
}
break;
case R.id.bt_Linea_2:
if(boton_s_vi.getVisibility() == View.GONE)
{
boton_s_vi.setVisibility(View.VISIBLE);
boton_s_ga.setVisibility(View.GONE);
}else
{
boton_s_vi.setVisibility(View.GONE);
}
break;
}
}
}
Y este su MXL (estaci2.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.darwindeveloper.horizontalscrollmenu.Estaci2">
<ImageButton
android:id="@+id/bt_Linea_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:background="@android:color/transparent"
android:contentDescription="@string/descri_linea1"
app:srcCompat="@drawable/linea_1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<ImageButton
android:id="@+id/bt_s_ga"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:background="@android:color/transparent"
android:contentDescription="@string/descri_s_ga"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bt_Linea_1"
app:srcCompat="@drawable/s_ga"
android:visibility="gone"/>
<ImageButton
android:id="@+id/bt_Linea_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="44dp"
android:background="@android:color/transparent"
android:contentDescription="@string/descri_linea2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bt_s_ga"
app:srcCompat="@drawable/linea_2" />
<ImageButton
android:id="@+id/bt_s_vi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:background="@android:color/transparent"
android:contentDescription="@string/descri_s_vi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bt_Linea_2"
app:srcCompat="@drawable/s_vi"
android:visibility="gone"/>
</android.support.constraint.ConstraintLayout>
Error que se muestra al pulsar los botones S Ga y S Vi para llamar a otro Fragment
2018-12-04 11:08:44.915 19253-19253/com.darwindeveloper.horizontalscrollmenu E/FragmentManager: No view found for id 0x7f070046 (com.darwindeveloper.horizontalscrollmenu:id/frame_container) for fragment Esta_S_Vi{b91057b #0 id=0x7f070046}
2018-12-04 11:08:44.915 19253-19253/com.darwindeveloper.horizontalscrollmenu E/FragmentManager: Activity state:
2018-12-04 11:08:44.964 19253-19253/com.darwindeveloper.horizontalscrollmenu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.darwindeveloper.horizontalscrollmenu, PID: 19253
java.lang.IllegalArgumentException: No view found for id 0x7f070046 (com.darwindeveloper.horizontalscrollmenu:id/frame_container) for fragment Esta_S_Vi{b91057b #0 id=0x7f070046}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1413)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Gracias.