UPDATE:
The bottom navigationview that I am implementing is only for a single fragment and not inside any activity so as to switch between fragments. The purpose of the bottomnavigation is to provide the sort functionality on scroll behaviour of the listview;similar to what happens in flipkart/paytm.
I have a listview inside a fragment and I want a bottomnavigationview which should behave as:
1.Initially, when the listview is being loaded, the bottomnavigationview should appear at the bottom of the fragment.
I have referred to the answers on stackoverflow and tried their code but none of them works for me.Can anyone brief me one this?
If there are better ways to achieve this, please let me know. I am also doubtful whether the drawerlayout is necessary with this?
Here is my fragment code:
<?xml version="1.0" encoding="utf-8"?>
<!--<android.support.v4.widget.DrawerLayout-->
<!--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/drawer_layout"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="match_parent"-->
<!--tools:context=".AvailableFood">-->
<android.support.design.widget.CoordinatorLayout
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/coordinator_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AvailableFood">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<customfonts.MyEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:id="@+id/food_req"
android:hint="Your requirement(kg) today?"
android:textColorHint="#000000"
android:textColor="#000000"
android:background="@drawable/button_background"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:drawableRight="@drawable/ic_local_dining_black_24dp"
android:padding="16dp"
/>
<ListView
android:id="@+id/list_avl_food"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
<FrameLayout
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_menu" />
</android.support.design.widget.CoordinatorLayout>
<!--</android.support.v4.widget.DrawerLayout>-->
And here is how I apply the navigationview behavior:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_available_food, container, false);
mBottomNavigationView = rootview.findViewById(R.id.bottom_nav);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mBottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationBehavior());
}
BottomNavigationBehavior.java:
package com.example.student.doneate;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {
public BottomNavigationBehavior() {
super();
}
public BottomNavigationBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) {
boolean dependsOn = dependency instanceof FrameLayout;
return dependsOn;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dx, int dy, int[] consumed) {
if(dy < 0) {
showBottomNavigationView(child);
}
else if(dy > 0) {
hideBottomNavigationView(child);
}
}
private void hideBottomNavigationView(BottomNavigationView view) {
view.animate().translationY(view.getHeight());
}
private void showBottomNavigationView(BottomNavigationView view) {
view.animate().translationY(0);
}
}
EDIT 1: BottomNavigationView:
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_gravity="bottom"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_menu" />
I don't know why but, after nothing has helped, I found my own escape out of this.
Set the visibiity property of the bottomnavigation view using the onscrollListener on the ListView and it is working just fine.