Recycleview in Coordinatorlayout

I am trying to create a RelativeLayout that has a CoordinatorLayout and LinearLayout at the bottom, and found a weird behavior that I cannot solve. This is my layout.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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.support.design.widget.CoordinatorLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/sender" android:background="@android:color/white" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" 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="?actionBarSize" android:background="?colorPrimary" app:layout_scrollFlags="scroll|enterAlways" /> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id="@+id/messages_list" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> <LinearLayout android:id="@+id/sender" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_vertical" android:background="@android:color/white"> <EditText android:id="@+id/inputText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="text" /> <Button android:fontFamily="sans-serif" style="?android:attr/borderlessButtonStyle" android:textAppearance="?android:textAppearanceButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send" android:textColor="?colorPrimary" android:id="@+id/send"/> </LinearLayout> </RelativeLayout> 

After changing the data in the adapter, I try to go to the last element (for example, recyclerView.smoothScrollToPosition (size);), and everything I see is part of the last view (not the full size). If recycleview doesn't nest in CoordinatorLayout - everything works as expected - I see the full size of the last element. How can I change the layout so that everything is correct?

+5
source share
4 answers

in your code, remove app:layout_scrollFlags="scroll|enterAlways" from the Toolbar and instead add it to the AppBarLayout so that it looks like

 <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|enterAlways"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?actionBarSize" android:background="?colorPrimary"/> </android.support.design.widget.AppBarLayout> 
0
source

Try as follows:

 <RelativeLayout 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:layout_marginBottom="16dp"> 

The main thing is to use to add android: layout_marginBottom to your activity

0
source

The last item is cropped because the RecyclerView is not displayed completely on the screen. This is accelerated by the advanced AppBar. Please note: when scrolling manually, the AppBar application will decrease as the last item is reached.

What was best in my case was to just collapse the AppBar before scrolling:

 AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar); appBarLayout.setExpanded(false, false); recyclerView.smoothScrollToPosition(position); 

I think you could improve this solution by folding it only when necessary.

If folding an AppBar is undesirable, you can add a bottom pad with the same height as the extended AppBar. However, there will be other crashes (for example, when scrolling to a position that is already in the RecyclerView, but only off the screen).

0
source

The problem is that smoothScrollToPosition() will silently scroll through the RecyclerView , not allowing the CoordinatorLayout about the scroll. This is what I came up with. The good thing is that it should scroll through the AppBarLayout if you have enough elements in the adapter.

  final AppBarLayout layout = (AppBarLayout) findViewById(R.id.appbar); layout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { lastVerticalOffset = verticalOffset; } }); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (tryCollapseAppbarOnNextScroll && lastVerticalOffset != -layout.getTotalScrollRange()) { layout.setExpanded(false); tryCollapseAppbarOnNextScroll = false; } } }); 

Now that you are sending a message, do the following:

 tryCollapseAppbarOnNextScroll = true; recyclerView.smoothScrollToPosition(adapter.getItemCount()-1); 
0
source

All Articles