Yet more problem in using CoordinatorLayout and AppBarLayout.
I'm trying to achieve the basic functionality of having the Toolbar scroll off screen when scrolling down and coming back on screen when scrolling up.
However, my current set up is showing a problem: Not only is the Toolbar not scrolling off, the ListView seems to be rendering off screen at the bottom. It's almost as if it's been offset by the AppBarLayout height.
Here is a gif describing the issue, note that the final item is cut off also the ScrollBar is off screen:
My layout is pretty standard:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="@color/orange"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeToRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ExpandableListView
android:id="@+id/listView"
android:groupIndicator="@android:color/transparent"
android:layout_width="match_parent"
android:dividerHeight="0px"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
Note: AppBarLayout currently expects to be the direct child nested within a CoordinatorLayout according to the official Google docs. Next, we need to define an association between the AppBarLayout and the View that will be scrolled. Add an app:layout_behavior to a RecyclerView or any other View capable of nested scrolling such as NestedScrollView.
AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures. Children should provide their desired scrolling behavior through AppBarLayout.LayoutParams.setScrollFlags (int) and the associated layout xml attribute: app:layout_scrollFlags .
public AppBarLayout (Context context, AttributeSet attrs, int defStyleAttr) Add a listener that will be called when the offset of this AppBarLayout changes. BaseOnOffsetChangedListener: The listener that will be called when the offset changes.] Returns the id of the view that the AppBarLayout should use to determine whether it should be lifted.
The CoordinatorLayout is a new layout, introduced with the Android Design Support Library. The CoordinatorLayout is a super-powered FrameLayout (according to the official documentation ). If you have used a FrameLayout before, you should be very comfortable using CoordinatorLayout.
CoordinatorLayout only works with RecyclerView or NestedScrollView.Try Wrapping your ExapandableListView inside NestedScrollView or use the below code to make NestedScrollingEnable for ExpandableListView.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
expandablelistView.setNestedScrollingEnabled(true);
}else {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mSwipeLayout.getLayoutParams();
params.bottomMargin = heightOfAppBarCompat;
mSwipeLayout.setLayoutParams(params);
}
Edit You can make scrolling work as expected pre-21 with the else statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With