Skip to content

Instantly share code, notes, and snippets.

@webserveis
Last active April 27, 2017 18:13
Show Gist options
  • Save webserveis/695a7623b4b1a9b57d742bd12eda49b2 to your computer and use it in GitHub Desktop.
Save webserveis/695a7623b4b1a9b57d742bd12eda49b2 to your computer and use it in GitHub Desktop.
Patrón de navegación usando BottomNavigationView

Sistema de navegación usando BottomNavigationView

Primer paso

Añadir menus menu/navigation.xml Crear layouts del principal activity_main.xmly de cada vista derivar de fragment_view.xml.xml

Segundo paso

Definición de las vistas relacionando con sus fragments define_fragment.java

Tercer paso

Implementar sistema de carga de fragments y definir la navegación MainActivity.java y cargar fragments con openfragment(contenedor,fragment)

Mantener los Fragments en memoria

Si queremos que los fragments esten en memoria y siempre que volvemos a el, este como lo dejamos.

  private void openFragment(int frameContainer, Fragment newFragment) {

  String loadFragmentName = newFragment.getClass().getName();
  FragmentManager fm = getSupportFragmentManager();
  FragmentTransaction ft = fm.beginTransaction();

  //ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);

  Fragment currentFragment = getSupportFragmentManager().findFragmentById(frameContainer);

  if (currentFragment == null) {
      Log.d(TAG, "openFragment: ADD" + loadFragmentName + " in stack");
      ft.add(frameContainer, newFragment, loadFragmentName);
      ft.commit();
      oldFragmentTAG = loadFragmentName;
  } else {

      if (!oldFragmentTAG.equalsIgnoreCase(loadFragmentName)) {
          Log.d(TAG, "openFragment: REPLACE" + loadFragmentName + " in stack");

          if (fm.findFragmentByTag(loadFragmentName) == null) {
              ft.add(frameContainer, newFragment, loadFragmentName);
              ft.commit();
          }

          showHideFragment(fm.findFragmentByTag(oldFragmentTAG));
          showHideFragment(fm.findFragmentByTag(loadFragmentName));

          //ft.addToBackStack(newFragment.getClass().getName());
          oldFragmentTAG = loadFragmentName;
      } else {
          Log.w(TAG, "openFragment: NO REPLACE");
      }

  }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.webserveis.app.imagestudio.MainActivity">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_light"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</LinearLayout>
public class FirstFragment extends Fragment {
private static final String TAG = FirstFragment.class.getSimpleName();
private final String SAVED_BUNDLE_TAG = "saved_bundle";
private boolean isChecked;
public static FirstFragment newInstance() {
FirstFragment fragment = new FirstFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
Log.i(TAG, "onSaveInstanceState: ");
outState.putBoolean(SAVED_BUNDLE_TAG, isChecked);
super.onViewStateRestored(outState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: FirstFragment");
View view = inflater.inflate(R.layout.fragment_one, container, false);
final CheckBox cbCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
if (savedInstanceState != null) {
Log.d(TAG, "savedInstanceState: true");
isChecked = savedInstanceState.getBoolean(SAVED_BUNDLE_TAG, false);
cbCheckBox.setChecked(isChecked);
}
cbCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Log.d(TAG, "onCheckedChanged: " + b);
isChecked = b;
}
});
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Primer fragmento" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
public class MainActivity extends AppCompatActivity
implements BottomNavigationView.OnNavigationItemSelectedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private Fragment selectedFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
if (savedInstanceState == null) {
onNavigationItemSelected(navigation.getMenu().findItem(R.id.navigation_home));
} else {
Log.w(TAG, "savedInstanceState is not null ");
selectedFragment = getSupportFragmentManager().getFragment(savedInstanceState, "main_content");
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = FirstFragment.newInstance();
break;
case R.id.navigation_dashboard:
selectedFragment = SecondFragment.newInstance();
break;
case R.id.navigation_notifications:
selectedFragment = ThirdFragment.newInstance();
break;
}
if (selectedFragment != null) {
Log.d(TAG, "onNavigationItemSelected: ");
openFragment(R.id.content_frame, selectedFragment);
return true;
} else return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" />
</menu>
private void openFragment(int frameContainer, Fragment newFragment) {
String loadFragmentName = newFragment.getClass().getName();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment currentFragment = getSupportFragmentManager().findFragmentById(frameContainer);
if (currentFragment == null) {
Log.d(TAG, "openFragment: ADD" + loadFragmentName + " in stack");
ft.add(frameContainer, newFragment);
} else {
if (!currentFragment.getClass().getName().equalsIgnoreCase(loadFragmentName)) {
Log.d(TAG, "openFragment: REPLACE" + loadFragmentName + " in stack");
ft.replace(frameContainer, newFragment);
//ft.addToBackStack(newFragment.getClass().getName());
} else {
Log.w(TAG, "openFragment: NO REPLACE");
}
}
ft.commit();
}
public void showHideFragment(final Fragment fragment) {
if (fragment != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
if (fragment.isHidden()) {
ft.show(fragment);
} else {
ft.hide(fragment);
}
ft.commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment