Eine neue Aktivität wird nicht gestartet oder die untere Navigationsleiste verschwindet für Sekundenbruchteile. Die Grundidee ist, eine Aktivität zu haben, die Ansichten oder Fragmente in einen Container aufbläht, basierend darauf, welcher Artikel ausgewählt ist. Ich würde empfehlen, Ansichten über Fragmente zu verwenden, da bestimmte FragmentTransaction-Fehler besonders schwer aufzuspüren sind und Sie dem FragmentManager ausgeliefert sind, während Sie vollständige Kontrolle über benutzerdefinierte Ansichten haben. Das Beispiel-GIF legt die Sichtbarkeit des unteren Navigationsbereichs auf View.GONE fest und animiert das untere Navigationsfenster während des Scrollens.
MainActivity.java
public class MainActivity extends Activity implements
YourBottomNavView.OnItemClickListener {
ViewGroup viewContainer;
YourBottomNavView bottomNav;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewContainer = findViewById(R.id.view_container);
bottomNav = findViewById(R.id.bottom_nav);
bottomNav.setItemClickListener(this);
}
@Override
public void onItemClicked(int item) {
viewContainer.removeAllViews();
View nextView = getView(item);
viewContainer.addView(nextView);
}
private View getView(int item) {
//Insert logic
}
}
R.layout.activity_main
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/header"/>
<YourBottomNavView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Alternativ können Sie Fragmente verwenden, um in Ihrem viewContainer zu injizieren, aber ich finde, mit einfachen alten Ansichten viel zuverlässiger als die zufälligen Bugfragmente auf Sie werfen können. Hier
ist ein Beispiel für eine benutzerdefinierte Ansicht
public class YourAwesomeView extends LinearLayout {
public YourAwesomeView(Context context) {
super(context);
init();
}
public YourAwesomeView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
inflate(getContext(), R.layout.YOUR_AWESOME_VIEW, this);
}
}
YourBottomNavView.java
public class YourBottomNavView extends LinearLayout {
View button1, button2, button3;
View root;
OnItemClickListener onItemClickListener;
public YourBottomNavView(Context context) {
super(context);
init();
}
public YourBottomNavView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
inflate(getContext(), R.layout.view_bottom_nav, this);
root = findViewById(R.id.root);
button1 = findViewById(R.id.button1_container);
button2 = findViewById(R.id.button2_container);
button3 = findViewById(R.id.button3_container);
//The button clicks need to communicate to something like the
//activity to inflate your new view/fragment. I personally
//define an OnItemClickedListener interface in the
//YourBottomNavView class that the MainActivity implements
//and I have the activity decide which view to inflate into
//its frame layout. This is also where you can do cool
//animations like we saw from the GIFs.
//This is also where you can swap out drawables to color the
//ImageViews differently.
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClicked(0);
root.setBackgroundColor(0xFF0000);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClicked(1);
root.setBackgroundColor(0x00FF00);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClicked(2);
root.setBackgroundColor(0x0000FF);
}
});
}
public void setItemClickListener(OnItemClickListener listener) {
onItemClickListener = listener;
}
public interface OnItemClickListener {
void onItemClicked(int item);
}
}
R.layout.view_bottom_nav
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/default_color"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/button1_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:weight="1">
<ImageView android:id="@+id/image1"
android:layout_width="24dp"
android:layout_height="24dp"
src="@drawable/icon1"/>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/button2_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:weight="1">
<ImageView android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
src="@drawable/icon1"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/button3_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:weight="1">
<ImageView android:id="@+id/button3"
android:layout_width="24dp"
android:layout_height="24dp"
src="@drawable/icon3"/>
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Durch die Verwendung von Fragmenten denke ich, anstelle von Aktivität und halten Sie die "untere Leiste" auf dem übergeordneten Aktivitätslayout. [Dieses Beispiel] (http://blog.grafixartist.com/bottom-navigation-bar-android-tutorial/) sollte Ihnen den Hinweis geben. – Fllo