I'm using bottomnavigationview with fragment ... the problem is that I turn on the togglebutton in "on" and when I select another item in bottomnavigationview and I select again the item where the togglebutton is hosted it goes "off"
Menu.java
public class Menu extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
BottomNavigationViewEx navigation = (BottomNavigationViewEx) findViewById(R.id.navigation_menu);
navigation.setOnNavigationItemSelectedListener(this);
loadFragment(new HomeFragment());
navigation.enableAnimation(false);
navigation.enableShiftingMode(false);
navigation.enableItemShiftingMode(false);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId())
{
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_sala_espera:
fragment = new SalaEsperaFragment();
break;
case R.id.navigation_consultorio:
fragment = new ConsultorioFragment();
break;
case R.id.navigation_ajustes:
fragment = new AjustesFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment)
{
if(fragment!=null)
{
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,fragment)
.commit();
return true;
}
return false;
} }
SalaEsperaFragment.java
public class SalaEsperaFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_sala_espera, null);
final LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.ll_sala_espera);
final ImageView imageView=(ImageView)view.findViewById(R.id.iv_sala_espera_foco);
ToggleButton tb_foco=(ToggleButton)view.findViewById(R.id.tb_sala_espera_foco);
tb_foco.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isCheked) {
if(isCheked)
{
linearLayout.setBackground(getResources().getDrawable(R.drawable.drawable_linearlayout_on));
imageView.setImageResource(R.drawable.ic_foco_encedido);
}else{
linearLayout.setBackground(getResources().getDrawable(R.drawable.drawable_linearlayout_off));
imageView.setImageResource(R.drawable.ic_foco_apagado);
}
}
});
return view;
} }
activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/navigation_menu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
</FrameLayout>
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/navigation_menu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation_menu" >
</com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>
fragment_sala_wait.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:id="@+id/ll_sala_espera"
android:background="@drawable/drawable_linearlayout_off"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="10dp">
<ImageView
android:id="@+id/iv_sala_espera_foco"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="50dp"
android:src="@drawable/ic_foco_apagado"/>
<ToggleButton
android:id="@+id/tb_sala_espera_foco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:background="@drawable/toggle_option"
android:textOff=""
android:textOn=""/>
<Button
android:id="@+id/btn_sala_espera_ajuste"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/drawable_button_ajuste"
/>
</LinearLayout>