android - RecyclerView is in the middle instead of top after orientation changes from landscape to portrait -
i have used recyclerview in fragment. if activity's configchanges includes orientation, recycler view appears in middle of screen after phone orientation changes landscape portrait. should on top always. if configchanges not include orientation, appear on top on orientation changes.
do have idea reason?
this activity layout
<android.support.design.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.butterfly.loginactivity"> <android.support.design.widget.appbarlayout android:layout_width="match_parent" android:theme="@style/themeoverlay.appcompat.dark.actionbar" android:layout_height="wrap_content"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" app:layout_scrollflags="scroll|enteralways" app:popuptheme="@style/themeoverlay.appcompat.light" /> </android.support.design.widget.appbarlayout> <framelayout app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/butterflypi_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="top|start" /> </android.support.design.widget.coordinatorlayout> this fragment layout replaces framelayout above.
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" xmlns:card_view="http://schemas.android.com/apk/res-auto" tools:context="com.butterfly.fragment.butterflypisfragment"> <android.support.v7.widget.recyclerview android:id="@+id/my_pi_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignparenttop="true" /> </relativelayout>
i found solution here.
basically creating own behavior class:
public class patchedscrollingviewbehavior extends appbarlayout.scrollingviewbehavior { public patchedscrollingviewbehavior() { super(); } public patchedscrollingviewbehavior(context context, attributeset attrs) { super(context, attrs); } @override public boolean onmeasurechild(coordinatorlayout parent, view child, int parentwidthmeasurespec, int widthused, int parentheightmeasurespec, int heightused) { if (child.getlayoutparams().height == -1) { list dependencies = parent.getdependencies(child); if (dependencies.isempty()) { return false; } appbarlayout appbar = findfirstappbarlayout(dependencies); if (appbar != null && viewcompat.islaidout(appbar)) { if (viewcompat.getfitssystemwindows(appbar)) { viewcompat.setfitssystemwindows(child, true); } int scrollrange = appbar.gettotalscrollrange(); // int height = parent.getheight() - appbar.getmeasuredheight() + scrollrange; int parentheight = view.measurespec.getsize(parentheightmeasurespec); int height = parentheight - appbar.getmeasuredheight() + scrollrange; int heightmeasurespec = view.measurespec.makemeasurespec(height, view.measurespec.at_most); parent.onmeasurechild(child, parentwidthmeasurespec, widthused, heightmeasurespec, heightused); return true; } } return false; } private static appbarlayout findfirstappbarlayout(list<view> views) { int = 0; (int z = views.size(); < z; ++i) { view view = (view) views.get(i); if (view instanceof appbarlayout) { return (appbarlayout) view; } } return null; } } and changing app:layout_behavior in framelayout:
<framelayout app:layout_behavior="your_package.patchedscrollingviewbehavior" android:id="@+id/butterflypi_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="top|start" /> update: checked - issue fixed in 22.2.1 library, please update library, if still using 22.2.0
Comments
Post a Comment